public static void SeedData(UserManager <AppUser> userManager, AppDbContext context, string prefix)
        {
            Screen screen;

            if (context.Screens.Any())
            {
                screen = context.Screens.First();
                prefix = screen.Prefix;
                var user = userManager.FindByNameAsync(UserName).Result;
                if (user == null)
                {
                    return;
                }

                if (!context.AppUsersScreens.Any(s => s.ScreenId == screen.Id && s.AppUserId == user.Id))
                {
                    context.AppUsersScreens.Add(new AppUsersScreen
                    {
                        CreatedAt = DateTime.Now,
                        AppUserId = userManager.FindByNameAsync(UserName).Result.Id,
                        ScreenId  = screen.Id
                    });
                    context.SaveChanges();
                }
            }
            else
            {
                if (userManager.FindByNameAsync(UserName).Result == null)
                {
                    return;
                }

                screen = new Screen
                {
                    CreatedAt        = DateTime.Now,
                    Prefix           = prefix,
                    IsActive         = false,
                    UniqueIdentifier = Guid.NewGuid().ToString()
                };

                context.Screens.Add(screen);

                context.SaveChanges();

                if (context.Screens.Any(s => s.Prefix.Equals(prefix)))
                {
                    context.AppUsersScreens.Add(new AppUsersScreen()
                    {
                        CreatedAt = DateTime.Now,
                        AppUserId = userManager.FindByNameAsync(UserName).Result.Id,
                        ScreenId  = screen.Id
                    });
                }
                else
                {
                    return;
                }
            }

            var scheduleFromDb = context.Schedules.FirstOrDefault(s => s.Date == DateTime.Today && s.Prefix == prefix);

            if (scheduleFromDb != null)
            {
                if (context.ScheduleInScreens.Any(s =>
                                                  s.ScheduleId == scheduleFromDb.Id && s.ScreenId == screen.Id))
                {
                    return;
                }

                context.ScheduleInScreens.Add(new ScheduleInScreen
                {
                    CreatedAt  = DateTime.Now,
                    ScreenId   = screen.Id,
                    ScheduleId = scheduleFromDb.Id
                });

                context.SaveChanges();

                return;
            }

            IGetTimePlanFromInformationSystem timeplanGettingSystem = new GetTimePlanFromInformationSystem(prefix);
            var schedule = timeplanGettingSystem.GetScheduleForToday();

            if (schedule == null)
            {
                return;
            }

            schedule.Prefix = prefix;

            if (schedule.SubjectsInSchedules != null)
            {
                var subjectsInSchedule = schedule.SubjectsInSchedules.ToList();
                for (var i = 0; i < subjectsInSchedule.Count; i++)
                {
                    var subjectInScheduleFromDb = context.SubjectInSchedules.FirstOrDefault(s =>
                                                                                            s.UniqueIdentifier.Equals(subjectsInSchedule[i].UniqueIdentifier));
                    if (subjectInScheduleFromDb != null)
                    {
                        subjectsInSchedule[i] = subjectInScheduleFromDb;
                        continue;
                    }

                    var subject = context.Subjects.FirstOrDefault(t => t.SubjectCode == subjectsInSchedule[i].Subject.SubjectCode &&
                                                                  t.SubjectName == subjectsInSchedule[i].Subject.SubjectName);
                    if (subject != null)
                    {
                        subjectsInSchedule[i].Subject   = subject;
                        subjectsInSchedule[i].SubjectId = subject.Id;
                    }
                    else
                    {
                        context.Subjects.Add(subjectsInSchedule[i].Subject);
                    }

                    if (subjectsInSchedule[i].TeacherInSubjectEvents == null)
                    {
                        continue;
                    }

                    foreach (var teacherInSubjectEvent in subjectsInSchedule[i].TeacherInSubjectEvents !)
                    {
                        var teacher = context.Teachers.FirstOrDefault(
                            t => t.FullName == teacherInSubjectEvent.Teacher.FullName &&
                            t.Role == teacherInSubjectEvent.Teacher.Role);

                        if (teacher != null)
                        {
                            teacherInSubjectEvent.Teacher   = teacher;
                            teacherInSubjectEvent.TeacherId = teacher.Id;
                        }
                        else
                        {
                            context.Teachers.Add(teacherInSubjectEvent.Teacher);
                        }
                    }

                    context.SaveChanges();
                }

                schedule.SubjectsInSchedules = subjectsInSchedule;
            }

            context.Schedules.Add(schedule);
            context.SaveChanges();

            context.ScheduleInScreens.Add(new ScheduleInScreen
            {
                CreatedAt  = DateTime.Now,
                ScreenId   = screen.Id,
                ScheduleId = schedule.Id
            });

            var futureEvents =
                context.Events.Where(e => e.ShowStartDateTime <= DateTime.Now && e.ShowEndDateTime > DateTime.Now);

            if (futureEvents != null && futureEvents.Any())
            {
                foreach (var futureEvent in futureEvents)
                {
                    context.EventInSchedules.Add(new EventInSchedule
                    {
                        CreatedAt  = DateTime.Now,
                        EventId    = futureEvent.Id,
                        ScheduleId = schedule.Id
                    });
                }
            }

            context.SaveChanges();
        }
        public static void UpdateScheduleForTimeplan(AppDbContext context)
        {
            if (!context.Screens.Any())
            {
                return;
            }

            var screen = context.Screens.First();
            var prefix = screen.Prefix;

            var scheduleFromDb = context.Schedules.FirstOrDefault(s => s.Date == DateTime.Today && s.Prefix == prefix);

            if (scheduleFromDb != null)
            {
                if (context.ScheduleInScreens.Any(s =>
                                                  s.ScheduleId == scheduleFromDb.Id && s.ScreenId == screen.Id))
                {
                    return;
                }

                context.ScheduleInScreens.Add(new ScheduleInScreen
                {
                    CreatedAt  = DateTime.Now,
                    ChangedAt  = DateTime.Now,
                    ScreenId   = screen.Id,
                    ScheduleId = scheduleFromDb.Id
                });
                context.SaveChanges();
                return;
            }

            IGetTimePlanFromInformationSystem timeplanGettingSystem = new GetTimePlanFromInformationSystem(prefix);
            var schedule = timeplanGettingSystem.GetScheduleForToday();

            if (schedule == null)
            {
                return;
            }

            if (schedule.SubjectsInSchedules != null)
            {
                var subjectsInSchedule = schedule.SubjectsInSchedules.ToList();
                for (var i = 0; i < subjectsInSchedule.Count; i++)
                {
                    var subjectInScheduleFromDb = context.SubjectInSchedules.FirstOrDefault(s =>
                                                                                            s.UniqueIdentifier.Equals(subjectsInSchedule[i].UniqueIdentifier));

                    if (subjectInScheduleFromDb != null)
                    {
                        context.SubjectInSchedules.Remove(subjectInScheduleFromDb);
                        context.SaveChanges();
                    }

                    var subject = context.Subjects.FirstOrDefault(t => t.SubjectCode == subjectsInSchedule[i].Subject.SubjectCode &&
                                                                  t.SubjectName == subjectsInSchedule[i].Subject.SubjectName);
                    if (subject != null)
                    {
                        subjectsInSchedule[i].Subject   = subject;
                        subjectsInSchedule[i].SubjectId = subject.Id;
                    }
                    else
                    {
                        context.Subjects.Add(subjectsInSchedule[i].Subject);
                    }

                    if (subjectsInSchedule[i]?.TeacherInSubjectEvents == null)
                    {
                        continue;
                    }

                    foreach (var teacherInSubjectEvent in subjectsInSchedule[i].TeacherInSubjectEvents !)
                    {
                        var teacher = context.Teachers.FirstOrDefault(
                            t => t.FullName == teacherInSubjectEvent.Teacher.FullName &&
                            t.Role == teacherInSubjectEvent.Teacher.Role);

                        if (teacher != null)
                        {
                            teacherInSubjectEvent.Teacher   = teacher;
                            teacherInSubjectEvent.TeacherId = teacher.Id;
                        }
                        else
                        {
                            context.Teachers.Add(teacherInSubjectEvent.Teacher);
                        }
                    }

                    context.SaveChanges();
                }

                schedule.SubjectsInSchedules = subjectsInSchedule;
            }

            schedule.Prefix = prefix;
            context.Schedules.Add(schedule);
            context.SaveChanges();

            context.ScheduleInScreens.Add(new ScheduleInScreen
            {
                CreatedAt  = DateTime.Now,
                ChangedAt  = DateTime.Now,
                ScreenId   = screen.Id,
                ScheduleId = schedule.Id
            });

            var futureEvents =
                context.Events.Where(e => e.ShowStartDateTime <= DateTime.Now && e.EndDateTime >= DateTime.Now);

            foreach (var futureEvent in futureEvents)
            {
                context.EventInSchedules.Add(new EventInSchedule
                {
                    CreatedAt  = DateTime.Now,
                    ScheduleId = schedule.Id,
                    EventId    = futureEvent.Id
                });
            }

            context.SaveChanges();
        }
        public static async Task GetAndSaveScheduleForScreen(IBLLApp bll, string userId, Screen screen)
        {
            var timeplanGettingSystem = new GetTimePlanFromInformationSystem(screen.Prefix);
            var schedule = timeplanGettingSystem.GetScheduleForToday();

            var bllSchedule =
                ScheduleMapper.MapFromInternal(DAL.App.Mappers.ScheduleMapper.MapFromDomain(schedule));

            bllSchedule.Prefix = screen.Prefix;

            var scheduleGuid = await bll.Schedules.AddAsync(bllSchedule);

            await bll.SaveChangesAsync();

            var scheduleIdAfterSaveChanges = bll.Schedules.GetUpdatesAfterUowSaveChanges(scheduleGuid).Id;

            var subjects = schedule.SubjectsInSchedules;

            if (subjects != null)
            {
                foreach (var subjectInSchedule in subjects)
                {
                    int subjectInScheduleId;
                    var subjectInScheduleThatAlreadyExists =
                        await bll.SubjectInSchedules.FindByUniqueIdentifierAsync(subjectInSchedule.UniqueIdentifier);

                    if (subjectInScheduleThatAlreadyExists != null)
                    {
                        subjectInScheduleThatAlreadyExists.ScheduleId = scheduleIdAfterSaveChanges;
                        bll.SubjectInSchedules.Update(subjectInScheduleThatAlreadyExists);
                        await bll.SaveChangesAsync();

                        subjectInScheduleId = subjectInScheduleThatAlreadyExists.Id;
                    }
                    else
                    {
                        var bllSubjectInSchedule = new SubjectInSchedule
                        {
                            CreatedAt        = DateTime.Now,
                            CreatedBy        = userId,
                            Rooms            = subjectInSchedule.Rooms,
                            Groups           = subjectInSchedule.Groups,
                            UniqueIdentifier = subjectInSchedule.UniqueIdentifier,
                            StartDateTime    = subjectInSchedule.StartDateTime,
                            EndDateTime      = subjectInSchedule.EndDateTime,
                            SubjectType      = subjectInSchedule.SubjectType,
                            ScheduleId       = scheduleIdAfterSaveChanges
                        };

                        var subject = await bll.Subjects
                                      .FindBySubjectNameAndCodeAsync(subjectInSchedule.Subject.SubjectName,
                                                                     subjectInSchedule.Subject.SubjectCode);

                        if (subject != null)
                        {
                            bllSubjectInSchedule.SubjectId = subject.Id;
                            bllSubjectInSchedule.Subject   = null;
                        }
                        else
                        {
                            var bllSubject = new Subject
                            {
                                CreatedAt   = DateTime.Now,
                                CreatedBy   = userId,
                                SubjectCode = subjectInSchedule.Subject.SubjectCode,
                                SubjectName = subjectInSchedule.Subject.SubjectName
                            };
                            var subjectGuid = await bll.Subjects.AddAsync(bllSubject);

                            await bll.SaveChangesAsync();

                            bllSubjectInSchedule.SubjectId = bll.Subjects.GetUpdatesAfterUowSaveChanges(subjectGuid).Id;
                        }
                        var subjInScheduleGuid = await bll.SubjectInSchedules.AddAsync(bllSubjectInSchedule);

                        await bll.SaveChangesAsync();

                        subjectInScheduleId =
                            bll.SubjectInSchedules.GetUpdatesAfterUowSaveChanges(subjInScheduleGuid).Id;
                    }

                    var teachers = new List <Teacher>();

                    if (subjectInSchedule.TeacherInSubjectEvents != null)
                    {
                        foreach (var teacherInSubjectEvent in subjectInSchedule.TeacherInSubjectEvents)
                        {
                            var teacher = await bll.Teachers
                                          .FindTeacherByNameAndRoleAsync(teacherInSubjectEvent.Teacher.FullName,
                                                                         teacherInSubjectEvent.Teacher.Role);

                            if (teacher != null)
                            {
                                teachers.Add(teacher);
                            }
                            else
                            {
                                var newTeacher = new Teacher
                                {
                                    CreatedAt   = DateTime.Now,
                                    CreatedBy   = userId,
                                    TeacherName = teacherInSubjectEvent.Teacher.FullName,
                                    TeacherRole = teacherInSubjectEvent.Teacher.Role
                                };
                                var teacherGuid = await bll.Teachers.AddAsync(newTeacher);

                                await bll.SaveChangesAsync();

                                teachers.Add(bll.Teachers.GetUpdatesAfterUowSaveChanges(teacherGuid));
                            }
                        }
                    }

                    foreach (var teacher in teachers)
                    {
                        bll.TeacherInSubjectEvents.Add(new TeacherInSubjectEvent
                        {
                            CreatedAt           = DateTime.Now,
                            CreatedBy           = userId,
                            TeacherId           = teacher.Id,
                            SubjectInScheduleId = subjectInScheduleId
                        });
                    }
                    await bll.SaveChangesAsync();
                }
            }

            await bll.ScheduleInScreens.AddAsync(new ScheduleInScreen
            {
                CreatedAt  = DateTime.Now,
                CreatedBy  = userId,
                ScreenId   = screen.Id,
                ScheduleId = scheduleIdAfterSaveChanges
            });

            var futureEvents = await bll.Events.GetAllFutureEventsAsync(DateTime.Now);

            foreach (var futureEvent in futureEvents)
            {
                if (futureEvent.ShowStartDateTime <= DateTime.Now && futureEvent.ShowEndDateTime > DateTime.Now)
                {
                    await bll.EventInSchedules.AddAsync(new EventInSchedule
                    {
                        CreatedAt  = DateTime.Now,
                        CreatedBy  = userId,
                        ScheduleId = scheduleIdAfterSaveChanges,
                        EventId    = futureEvent.Id
                    });
                }
            }

            await bll.SaveChangesAsync();
        }