/// <summary>
        /// משנה עונה לעונה מוגדרת כאשר יש מספיק מידע המוכיח את השוני המשמעותי בעונה
        /// </summary>
        /// <param name="seasonType"></param>
        /// <param name="num"></param>
        public static void DefineSeasonInfo(int seasonType, int num)
        {
            Hospital_DBEntities2 db          = new Hospital_DBEntities2();
            List <Seasons>       tempSeasons = new List <Seasons>();
            Seasons tempLongSeasons          = new Seasons();

            switch (seasonType)
            {
            case 1: { db.Seasons.Add(new Seasons {
                        Date = DateTime.Today.Date, IsDefined = true, NumOfDays = 1
                    }); db.SaveChanges(); tempSeasons = BLManager.GetCurrentDate(false); tempLongSeasons = BLManager.GetLongDateSeason(); BLManager.CurrentDate = db.Seasons.Last(); } break;

            case 2: { db.Seasons.Add(new Seasons {
                        HebrewDate = DateTime.Today.Date, IsDefined = true, NumOfDays = 1
                    }); db.SaveChanges(); tempSeasons = BLManager.GetCurrentHeberewDate(false); tempLongSeasons = BLManager.GetLongHeberewDateSeason(); BLManager.CurrentHeberewDate = db.Seasons.Last(); } break;

            case 3: { db.Seasons.Add(new Seasons {
                        DayOfWeek = (int)DateTime.Today.DayOfWeek, IsDefined = true, NumOfDays = 1
                    }); tempSeasons = BLManager.GetCurrentDayOfWeek(false); BLManager.CurrentDayOfWeek = db.Seasons.Last(); } break;
            }

            db.SaveChanges();
            List <SeasonInformation> seasonTempInfo = new List <SeasonInformation>();

            if (tempLongSeasons == null)
            {
                //מקבץ את כל המידע לתקופה של תקופת יום בשבוע מסויימת לא מוגדרות
                CombineSeasons(GetInfoForSeasonList(tempSeasons), num, db.Seasons.ToList().Last().SeasonCode);
            }
            else
            {
                if (seasonType == 1)
                {
                    BLManager.CurrentDate = tempLongSeasons;
                }
                else
                {
                    BLManager.CurrentHeberewDate = tempLongSeasons;
                }
                CombineSeasons(GetInfoForSeasonList(tempSeasons), 5, tempLongSeasons.SeasonCode);
                db.Seasons.Remove(db.Seasons.Last());
            }

            db.SeasonInformation.ToList().ForEach(se => tempSeasons.ForEach(s => { if (se.SeasonCode == s.SeasonCode)
                                                                                   {
                                                                                       seasonTempInfo.Add(se);
                                                                                   }
                                                                            }));
            db.SeasonInformation.RemoveRange(seasonTempInfo);
            db.SaveChanges();
            List <int> listCode = tempSeasons.Select(i => i.SeasonCode).ToList();

            db.Seasons.RemoveRange(db.Seasons.Where(item => listCode.Any(item2 => item2 == item.SeasonCode)));
        }
        /// <summary>
        /// אם הממוצע היומי שונה משמעותית מהמידע הכללי ולא מופיע לתאריך זה מידע שהוגדר
        /// נוצר מופע חדש של עונה לא מוגדרת המכילה את כל הנתונים- תאריך עברי לועזי ויום בשבוע לצורך בדיקה
        /// </summary>
        public void AddSeason()
        {
            List <Seasons>       seasonToCheck = new List <Seasons>();
            Hospital_DBEntities2 db            = new Hospital_DBEntities2();

            if (BLManager.CurrentDate != null)
            {
                seasonToCheck.Add(BLManager.CurrentDate);
            }
            if (BLManager.CurrentHeberewDate != null)
            {
                seasonToCheck.Add(BLManager.CurrentHeberewDate);
            }
            else if (BLManager.CurrentDate == null)
            {
                seasonToCheck.Add(new Seasons {
                    Date = DateTime.Today, DayOfWeek = ((int)DateTime.Today.DayOfWeek) + 1, HebrewDate = DateTime.Today, IsDefined = false, NumOfDays = 1
                });
                db.Seasons.Add(seasonToCheck[0]);
                db.SaveChanges();
            }
            seasonToCheck.ForEach(s =>
            {
                db.SeasonInformation.Add(new SeasonInformation
                {
                    AddingDate       = DateTime.Today,
                    DepartmentCode   = DepartmentCode,
                    Ishistory        = false,
                    StaffAvg         = StaffAvg,
                    WaitersAvg       = WaitersAvg,
                    WaitingTimeAvg   = DayWaitingAvg,
                    Whidnesstochange = 1,
                    SeasonCode       = s.SeasonCode
                });
            });
            db.SaveChanges();
            if (BLManager.GetCurrentDate(false).Count() == 10)
            {
                DefineSeasonInfo(1, 5);
            }

            if (BLManager.GetCurrentHeberewDate(false).Count() == 10)
            {
                DefineSeasonInfo(2, 5);
            }

            if (BLManager.GetCurrentDayOfWeek(false).Count() == 100)
            {
                DefineSeasonInfo(3, 52);
            }
        }
        /// <summary>
        /// שומר/משכלל את הממוצעים היומיים במסד הנתונים
        /// </summary>
        /// <param name="information"></param>
        public void SetSeasonInfoInDB(SeasonInformation information)
        {
            Hospital_DBEntities2 db = new Hospital_DBEntities2();

            information                = db.SeasonInformation.Where(i => i.InfoCode == information.InfoCode).First();
            information.WaitersAvg     = (information.WaitersAvg * information.Whidnesstochange + WaitersAvg) / ++information.Whidnesstochange;
            information.StaffAvg       = (information.StaffAvg * (information.Whidnesstochange - 1) + StaffAvg) / information.Whidnesstochange;
            information.WaitingTimeAvg = (int)(information.WaitingTimeAvg * (information.Whidnesstochange - 1) + DayWaitingAvg) / information.Whidnesstochange;
            db.SaveChanges();
            //האם להעביר מידע לתקופה להיסטורי לפי מרחב המדגם
            if ((information.Seasons.DayOfWeek != null && information.Whidnesstochange == 52 /*(one year)*/) || (information.Seasons.DayOfWeek == null && information.Whidnesstochange == 5 /*(5 years)*/))
            {
                information.Ishistory = true;
                db.SaveChanges();
                db.SeasonInformation.Add(new SeasonInformation {
                    Ishistory      = false,
                    DepartmentCode = information.DepartmentCode,
                    SeasonCode     = information.SeasonCode,
                    AddingDate     = DateTime.Today
                });
                db.SaveChanges();
            }
        }
        /// <summary>
        ///צירוף מס' עונות לעונה אחת וקישור כל יחידות המידע של העונות לעונה החדשה שהוגדרה
        ///או כאשר רוצים ליצור עונה מוגדרת ארוכה מ-2 עונות מוגדרות רצופות)
        ///(או ליצור ממס' עונות לא מוגדרות של אותו תאריך עונה מוגדרת אחת
        /// </summary>
        /// <param name="seasonInfo">רשימת העונות שרוצים לצרף</param>
        /// <param name="num">המס' לפיו יוחלט האם המידע לעונה הוא היסטורי</param>
        public static void CombineSeasons(List <SeasonInformation> seasonInfo, int num, int seasonCode)
        {
            Hospital_DBEntities2 db = new Hospital_DBEntities2();

            seasonInfo
            .GroupBy(p => p.DepartmentCode)
            .Select(g => g.ToList())
            .ToList()
            .ForEach(i =>
            {
                db.SeasonInformation.Add(new SeasonInformation
                {
                    SeasonCode       = seasonCode,
                    StaffAvg         = (int)i.Average(s => s.StaffAvg),
                    WaitersAvg       = (int)i.Average(s => s.WaitersAvg),
                    WaitingTimeAvg   = (int)i.Average(s => s.WaitingTimeAvg),
                    Whidnesstochange = i.Count(),
                    DepartmentCode   = i.First().DepartmentCode,
                    Ishistory        = (i.Count >= num)
                });
                db.SaveChanges();
            });
        }