private static List <Composition> GetCompositionsByTrain(SgfContext sgfContext, string OS)
        {
            List <Composition> compositions = new List <Composition>();

            try
            {
                compositions = sgfContext.Compositions.Where(x => x.OS == OS).ToList();
            }
            catch (Exception e)
            {
                DebugLog.Logar("GetCompositionsByTrain: " + e.Message);
            }

            return(compositions);
        }
        public static List <TrainMovSegments> GetTrainsBySegment(SgfContext sgfContext, int location, DateTime initialDate,
                                                                 DateTime finalDate) // return the list of train movement by segments from the DB
        {
            List <TrainMovSegments> trainMovSegments = new List <TrainMovSegments>();

            try
            {
                trainMovSegments = sgfContext.TrainMovSegments.Include(x => x.Train).
                                   Where(x => x.Segment == "WT" && x.OcupationDate >= initialDate && x.OcupationDate <= finalDate &&
                                         x.Train.TrainName.StartsWith("M") && x.Location == location).ToList();
            }
            catch (Exception e)
            {
                DebugLog.Logar("GetTrainsBySegment: " + e.Message);
            }
            return(trainMovSegments);
        }
        public static List <TU> GetTu(DateTime initialDate, DateTime finalDate, int initialKM = 0, int finalKM = 892) //Return the list of TUs
        {
            var sgfContext = new SgfContext();
            var segments   = sgfContext.Segments.Where(x => x.Segment == "WT" && x.Location >= initialKM &&
                                                       x.Location <= finalKM).OrderBy(x => x.Location).ToList();
            var tu = new List <TU>();

            foreach (var segment in segments)
            {
                var trainBySegmentObjects = GetTrainsBySegment(sgfContext, segment.Location, initialDate, finalDate);
                int trainBySegment        = trainBySegmentObjects.Count;
                var trains = trainBySegmentObjects.Select(x => x.TrainID).Distinct();
                Dictionary <string, Tuple <List <TrainMovSegments>, List <TrainMovSegments> > > keyValuePairs = new Dictionary <string, Tuple <List <TrainMovSegments>, List <TrainMovSegments> > >();
                Dictionary <int, double> weightByAmv = new Dictionary <int, double>();
                machines.ToList().ForEach(x => keyValuePairs.Add(x, new Tuple <List <TrainMovSegments>, List <TrainMovSegments> >(new List <TrainMovSegments>(), new List <TrainMovSegments>())));
                for (int i = 1; i <= 4; i++)
                {
                    weightByAmv.Add(i, new Double());
                }

                foreach (var train in trains)
                {
                    var          trainMov     = trainBySegmentObjects.Where(x => x.TrainID == train).OrderBy(x => x.OcupationDate);
                    var          trainOS      = trainMov.FirstOrDefault().Train.OS;
                    var          direction    = trainMov.FirstOrDefault().Direction;
                    var          tracks       = trainMov.Select(x => x.Track).ToList();
                    TU.TypesOfTU type         = Constants.TUTypeA.Contains(segment.Location) ? TU.TypesOfTU.A : TU.TypesOfTU.V;
                    var          mchs         = GetMchByOccupations(tracks, direction, type);
                    var          amvs         = GetAmvByOccupations(tracks, direction, type);
                    var          compositions = GetCompositionsByTrain(sgfContext, trainOS);
                    var          weight       = compositions.Sum(x => x.IndividualWeight) / Math.Pow(10, 6);
                    //DebugLog.Logar(compositions.Count.ToString());
                    amvs.ForEach(x =>
                    {
                        weightByAmv[x] += weight;
                    });
                    mchs.ForEach(x =>
                    {
                        var emptyTrains = keyValuePairs[x].Item1;
                        emptyTrains.AddRange(trainMov.Where(y => y.Direction == 1));
                        var loadedTrains = keyValuePairs[x].Item2;
                        loadedTrains.AddRange(trainMov.Where(y => y.Direction == -1));
                        keyValuePairs[x] = new Tuple <List <TrainMovSegments>, List <TrainMovSegments> >(emptyTrains, loadedTrains);
                    });
                }

                tu.Add(
                    new TU
                {
                    Km       = segment.Location,
                    TypeOfTU = Constants.TUTypeA.Contains(segment.Location) ? TU.TypesOfTU.A : TU.TypesOfTU.V,
                    AmvsInTU = new List <Amv>()
                    {
                        new Amv
                        {
                            AmvNumber = 1,
                            MchsInAmv = new List <Mch>()
                            { //AMV1
                                new Mch
                                {
                                    InstallLocation = "W11A",
                                    MTBT            = weightByAmv[1],
                                    LoadedTrains    = keyValuePairs["W11A"].Item2,
                                    EmptyTrains     = keyValuePairs["W11A"].Item1
                                },
                                new Mch
                                {
                                    InstallLocation = "W11B",
                                    MTBT            = weightByAmv[1],
                                    LoadedTrains    = keyValuePairs["W11B"].Item2,
                                    EmptyTrains     = keyValuePairs["W11B"].Item1
                                },
                                new Mch
                                {
                                    InstallLocation = "W11C",
                                    MTBT            = weightByAmv[1],
                                    LoadedTrains    = keyValuePairs["W11C"].Item2,
                                    EmptyTrains     = keyValuePairs["W11C"].Item1
                                }
                            }
                        },
                        new Amv
                        {
                            AmvNumber = 2,
                            MchsInAmv = new List <Mch>()
                            { //AMV2
                                new Mch
                                {
                                    InstallLocation = "W21A",
                                    MTBT            = weightByAmv[2],
                                    LoadedTrains    = keyValuePairs["W21A"].Item2,
                                    EmptyTrains     = keyValuePairs["W21A"].Item1
                                },
                                new Mch
                                {
                                    InstallLocation = "W21B",
                                    MTBT            = weightByAmv[2],
                                    LoadedTrains    = keyValuePairs["W21B"].Item2,
                                    EmptyTrains     = keyValuePairs["W21B"].Item1
                                },
                                new Mch
                                {
                                    InstallLocation = "W21C",
                                    MTBT            = weightByAmv[2],
                                    LoadedTrains    = keyValuePairs["W21C"].Item2,
                                    EmptyTrains     = keyValuePairs["W21C"].Item1
                                }
                            }
                        },
                        new Amv
                        {
                            AmvNumber = 3,
                            MchsInAmv = new List <Mch>()
                            { //AMV3
                                new Mch
                                {
                                    InstallLocation = "W12A",
                                    MTBT            = weightByAmv[3],
                                    LoadedTrains    = keyValuePairs["W12A"].Item2,
                                    EmptyTrains     = keyValuePairs["W12A"].Item1
                                },
                                new Mch
                                {
                                    InstallLocation = "W12B",
                                    MTBT            = weightByAmv[3],
                                    LoadedTrains    = keyValuePairs["W12B"].Item2,
                                    EmptyTrains     = keyValuePairs["W12B"].Item1
                                },
                                new Mch
                                {
                                    InstallLocation = "W12C",
                                    MTBT            = weightByAmv[3],
                                    LoadedTrains    = keyValuePairs["W12C"].Item2,
                                    EmptyTrains     = keyValuePairs["W12C"].Item1
                                }
                            }
                        },
                        new Amv
                        {
                            AmvNumber = 4,
                            MchsInAmv = new List <Mch>()
                            { //AMV4
                                new Mch
                                {
                                    InstallLocation = "W22A",
                                    MTBT            = weightByAmv[4],
                                    LoadedTrains    = keyValuePairs["W22A"].Item2,
                                    EmptyTrains     = keyValuePairs["W22A"].Item1
                                },
                                new Mch
                                {
                                    InstallLocation = "W22B",
                                    MTBT            = weightByAmv[4],
                                    LoadedTrains    = keyValuePairs["W22B"].Item2,
                                    EmptyTrains     = keyValuePairs["W22B"].Item1
                                },
                                new Mch
                                {
                                    InstallLocation = "W22C",
                                    MTBT            = weightByAmv[4],
                                    LoadedTrains    = keyValuePairs["W22C"].Item2,
                                    EmptyTrains     = keyValuePairs["W22C"].Item1
                                }
                            }
                        }
                    }
                });
            }

            sgfContext.Dispose();
            return(tu);
        }