public DetachResultInfo RemoveStudentFromAllStations(int studentPk)
        {
            var res         = new DetachResultInfo();
            var stations    = DB.StudentsToLines.Where(z => z.StudentId == studentPk).ToList();
            var stationsIds = stations.Select(s => s.StationId).Distinct().ToList();
            var linesIds    = stations.Select(s => s.LineId).Distinct().ToList();

            DB.StudentsToLines.RemoveRange(stations);
            DB.SaveChanges();
            using (var lineLogic = new LineLogic())
            {
                lineLogic.UpdateStudentCount();
            }
            res.Stations = DB.Stations.Where(s => stationsIds.Contains(s.Id)).ToList();
            res.Lines    = DB.Lines.Where(l => linesIds.Contains(l.Id)).ToList();
            return(res);
        }
        public bool DeleteAttach(int id)
        {
            var res = false;

            try
            {
                var att = GetAttachInfo(id);
                if (att != null)
                {
                    DB.StudentsToLines.Remove(att);
                    DB.SaveChanges();
                    using (var logic = new LineLogic())
                    {
                        logic.UpdateStudentCount();
                    }
                    res = true;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
            return(res);
        }
        public bool DeleteFromLine(int stationId, int lineId)
        {
            var res = false;

            try
            {
                var itm = DB.StationsToLines.FirstOrDefault(z => z.LineId == lineId && z.StationId == stationId);
                if (itm != null)
                {
                    DB.StationsToLines.Remove(itm);
                    DB.SaveChanges();
                    var p = 1;
                    foreach (var station in DB.StationsToLines.Where(z => z.LineId == lineId).OrderBy(z => z.Position))
                    {
                        station.Position = p;
                        p++;
                    }
                    var students = DB.StudentsToLines.Where(z => z.StationId == stationId && z.LineId == lineId);
                    foreach (var student in students)
                    {
                        student.LineId = -1;
                    }
                    DB.SaveChanges();
                    using (var logic = new LineLogic())
                    {
                        logic.UpdateStudentCount();
                    }
                    res = true;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
            return(res);
        }
        public bool AddToLine(int stationId, int lineId, TimeSpan arrivalTime, int position, int positionMode, bool changeColor)
        {
            var res = false;

            try
            {
                //Remove old value if it exists
                var itm = DB.StationsToLines.FirstOrDefault(z => z.LineId == lineId && z.StationId == stationId);
                if (itm != null)
                {
                    DB.StationsToLines.Remove(itm);
                    DB.SaveChanges();
                }

                var stationsOnLine = DB.StationsToLines.Where(z => z.LineId == lineId).OrderBy(z => z.Position).ToList(); //All station of line exclude  new

                if (positionMode == 1)
                {
                    stationsOnLine.Where(s => s.PositionMode == 1).ToList().ForEach(s => s.PositionMode = 0);
                    position = 1;
                }
                else if (positionMode == 2)
                {
                    stationsOnLine.Where(s => s.PositionMode == 2).ToList().ForEach(s => s.PositionMode = 0);
                    position = stationsOnLine.Count() + 1;
                }
                else if (position == 1)
                {
                    if (stationsOnLine.Any(s => s.PositionMode == 1))
                    {
                        position = 2;
                    }
                }
                else if (position == stationsOnLine.Count + 1)
                {
                    if (stationsOnLine.Any(s => s.PositionMode == 2))
                    {
                        position = stationsOnLine.Count;
                    }
                }

                foreach (var station in stationsOnLine)
                {
                    //If position of station equals ore more that new station position then move to one position
                    if (station.Position >= position)
                    {
                        station.Position++;
                    }
                }
                itm = new StationsToLine
                {
                    LineId       = lineId,
                    StationId    = stationId,
                    ArrivalDate  = arrivalTime,
                    Position     = position,
                    PositionMode = positionMode
                };
                DB.StationsToLines.Add(itm);
                var line     = DB.Lines.FirstOrDefault(z => z.Id == lineId);
                var c        = "";
                var oldColor = "";
                if (changeColor)
                {
                    var station = DB.Stations.FirstOrDefault(z => z.Id == stationId);

                    if (station != null && line != null)
                    {
                        oldColor      = station.color;
                        station.color = line.HexColor;
                        c             = line.HexColor;
                    }
                }
                //Students attached to stations without line
                var students = DB.StudentsToLines.Where(z => z.StationId == stationId && z.LineId == -1);
                foreach (var student in students)
                {
                    if (student.LineId == -1)
                    {
                        student.LineId = lineId;
                        if (line != null)
                        {
                            student.Direction = line.Direction;
                        }
                    }
                    if (!string.IsNullOrEmpty(c))
                    {
                        var st = DB.tblStudents.FirstOrDefault(z => z.pk == student.StudentId);
                        if (st != null)
                        {
                            //if (student.LineId==-1 || st.Color==oldColor) st.Color = c; //Change student color if student didn't have lines before or same color like station
                            st.Color = c;
                        }
                    }
                }


                DB.SaveChanges();
                using (var logic = new LineLogic())
                {
                    logic.UpdateStudentCount();
                }
                res = true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
            return(res);
        }
        public bool AttachStudent(int studentId, int stationId, int?lineId, int distance, ColorMode colorMode, DateTime?date, ConflictActions action, WeekDays weekdays)
        {
            var res = false;

            try
            {
                using (var logic = new LineLogic())
                {
                    //Remove duplicate
                    var duplicates = DB.StudentsToLines
                                     .Where(z => z.StudentId == studentId && z.Date == null && z.mon != true && z.tue != true && z.wed != true && z.thu != true && z.fri != true && z.sat != true && z.sun != true)
                                     .ToList();

                    DateTime?dt = null;
                    if (duplicates.Any())
                    {
                        if (action == ConflictActions.Replace || (date == null && weekdays.NothingSelected))
                        {
                            DB.StudentsToLines.RemoveRange(duplicates);
                            DB.SaveChanges();
                        }
                        else
                        {
                            dt = date;
                        }
                    }



                    var student = DB.tblStudents.FirstOrDefault(z => z.pk == studentId);
                    if (student == null)
                    {
                        return(false);
                    }
                    var station = DB.Stations.FirstOrDefault(z => z.Id == stationId);
                    if (station == null)
                    {
                        return(false);
                    }
                    var line = DB.Lines.FirstOrDefault(z => z.Id == lineId);

                    var direction = 0;
                    if (line != null)
                    {
                        direction = line.Direction;
                    }

                    if (colorMode == ColorMode.Station)
                    {
                        student.Color = station.color;
                    }

                    if (line != null && colorMode == ColorMode.Line)
                    {
                        student.Color = line.HexColor;
                    }

                    var item = new StudentsToLine
                    {
                        StudentId           = studentId,
                        StationId           = stationId,
                        LineId              = lineId ?? -1,
                        color               = student.Color,
                        Date                = dt,
                        Direction           = direction,
                        distanceFromStation = distance,
                        mon = weekdays.Monday,
                        tue = weekdays.Tuesday,
                        wed = weekdays.Wednesday,
                        thu = weekdays.Thursday,
                        fri = weekdays.Friday,
                        sat = weekdays.Saturday,
                        sun = weekdays.Sunday
                    };
                    DB.StudentsToLines.Add(item);
                    DB.SaveChanges();

                    logic.UpdateStudentCount();
                }
                res = true;
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
            return(res);
        }
        public bool SaveOnLine(int stationId, int lineId, TimeSpan arrivalTime, int position, int positionMode, bool changeColor)
        {
            var res = false;

            try
            {
                var itm = DB.StationsToLines.FirstOrDefault(z => z.LineId == lineId && z.StationId == stationId);
                if (itm != null)
                {
                    if (itm.Position != position || itm.PositionMode != positionMode) //reorder
                    {
                        var stations =
                            DB.StationsToLines.Where(z => z.LineId == lineId).OrderBy(z => z.Position).ToList();

                        if (positionMode == 1)
                        {
                            stations.Where(s => s.PositionMode == 1).ToList().ForEach(s => s.PositionMode = 0);
                            position = 1;
                        }
                        else if (positionMode == 2)
                        {
                            stations.Where(s => s.PositionMode == 2).ToList().ForEach(s => s.PositionMode = 0);
                            position = stations.Count;
                        }
                        else if (position == 1)
                        {
                            if (stations.Any(s => s.PositionMode == 1))
                            {
                                position = 2;
                            }
                        }
                        else if (position == stations.Count)
                        {
                            if (stations.Any(s => s.PositionMode == 2))
                            {
                                position = stations.Count - 1;
                            }
                        }

                        var p = 1;
                        foreach (var station in stations)
                        {
                            if (station.StationId != stationId)
                            {
                                if (p == position)
                                {
                                    p++;
                                }
                                station.Position = p;
                                p++;
                            }
                        }
                        itm.Position     = position;
                        itm.PositionMode = positionMode;
                    }
                    itm.ArrivalDate = arrivalTime;
                    //var c = "";
                    if (changeColor)
                    {
                        var station = DB.Stations.FirstOrDefault(z => z.Id == stationId);
                        var line    = DB.Lines.FirstOrDefault(z => z.Id == lineId);
                        if (station != null && line != null)
                        {
                            station.color = line.HexColor;
                        }
                    }


                    DB.SaveChanges();
                    using (var logic = new LineLogic())
                    {
                        logic.UpdateStudentCount();
                    }
                    res = true;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
            return(res);
        }