using System;
using System.Collections.Generic;
using System.Data.Entity;
using DataClasses;
using DataRepositories;
using WebApplication.Models;

namespace WebApplication
{
    public class DatabaseInitializer : DropCreateDatabaseAlways<DataContext>
    {
        /// <summary>
        /// Seeds default values into database tables.
        /// </summary>
        /// <param name="context">DbContext</param>
        protected override void Seed(DataContext context)
        {
            // Create User, Roles and Membership tables - SimpleMembership provider 
            try
            {
                SimpleMemberShipConfig.SimpleMembershipInitializer();
                SimpleMemberShipConfig.Initialize();
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException("The ASP.NET Simple Membership database could not be initialized. For more information, please see http://go.microsoft.com/fwlink/?LinkId=256588", ex);
            }

            // Create sample roles into table
            foreach (RoleEnum role in Enum.GetValues(typeof(RoleEnum)))
            {
                SimpleMemberShipUser.CreateRole(role);
            }


            // Crate a sample student and assign him to student role
            var student = new SimpleMemberShipUser
            {
                Profile = new UserProfile
                {
                    FirstName = "Robert",
                    LastName = "Šamaj",
                    Email = "xsama00@feec.vutbr.cz",
                    Login = "student"
                },
                Password = "student"
            };
            student.SaveAsRegisteredUser();
            student.AddUserToRole(RoleEnum.Students);


            // Crate a sample teacher and assign him to teacher role
            var teacher = new SimpleMemberShipUser
            {
                Profile = new UserProfile
                {
                    FirstName = "Alfréd",
                    LastName = "Neumayer",
                    Email = "xneum00@feec.vutbr.cz",
                    Login = "ucitel"
                },
                Password = "ucitel"
            };
            teacher.SaveAsRegisteredUser();
            teacher.AddUserToRole(RoleEnum.Teachers);


            // Crate a sample teacher and assign him to teacher role
            var admin = new SimpleMemberShipUser
            {
                Profile = new UserProfile
                {
                    FirstName = "Ján",
                    LastName = "Vysoký",
                    Email = "xvysok00@feec.vutbr.cz",
                    Login = "admin"
                },
                Password = "admin"
            };
            admin.SaveAsRegisteredUser();
            admin.AddUserToRole(RoleEnum.Administrators);


            // Create sample rooms
            var roomRepository = new Repository<Room>(context);
            var room1 = new Room { Location = "T-312" };
            var room2 = new Room { Location = "SE5.113" };
            var room3 = new Room { Location = "SE5.114" };
            roomRepository.Insert(room1);
            roomRepository.Insert(room2);
            roomRepository.Insert(room3);
            context.SaveChanges();


            // Create sample lessons
            var lessonRepository = new Repository<Lesson>(context);
            var lesson1 = new Lesson { ShortName = "XXXX", };
            var lesson2 = new Lesson { ShortName = "BSOS", };
            var lesson3 = new Lesson { ShortName = "BOOP", };
            var lesson4 = new Lesson { ShortName = "BFY2", FullName = "Skúška z Fyziky", SingleLessonDate = DateTime.Parse("2013/06/19")};
            var lesson5 = new Lesson { ShortName = "BPC2", };
            var lesson6 = new Lesson { ShortName = "BEL1", };
            var lesson7 = new Lesson { ShortName = "BEL2", };
            var lesson8 = new Lesson { ShortName = "BVKS", FullName = "Skúška z BVKS", SingleLessonDate = DateTime.Parse("2013/06/05") };
            var lesson9 = new Lesson { ShortName = "BDAK", FullName = "Skúška z BDAK", SingleLessonDate = DateTime.Parse("2013/06/18")};
            var lesson10 = new Lesson { ShortName = "XCA1", };
            var lesson11 = new Lesson { ShortName = "XCA2", };
            var lesson12 = new Lesson { ShortName = "XCA3", };
            lessonRepository.Insert(lesson1);
            lessonRepository.Insert(lesson2);
            lessonRepository.Insert(lesson3);
            lessonRepository.Insert(lesson4);
            lessonRepository.Insert(lesson5);
            lessonRepository.Insert(lesson6);
            lessonRepository.Insert(lesson7);
            lessonRepository.Insert(lesson8);
            lessonRepository.Insert(lesson9);
            lessonRepository.Insert(lesson10);
            lessonRepository.Insert(lesson11);
            lessonRepository.Insert(lesson12);
            context.SaveChanges();


            // Create a sample timetable
            var schooldayRepository = new Repository<SchoolDayRoom>(context);
            var schoolday1 = new SchoolDayRoom { LessonRoom = roomRepository.GetById(1), Day = DayOfWeekEnum.Monday, Time8_00 = lessonRepository.GetById(2), Time9_00 = lessonRepository.GetById(2), Time10_00 = lessonRepository.GetById(2), Time13_00 = lessonRepository.GetById(3), Time14_00 = lessonRepository.GetById(3) };
            var schoolday2 = new SchoolDayRoom { LessonRoom = roomRepository.GetById(1), Day = DayOfWeekEnum.Tuesday, Time9_00 = lessonRepository.GetById(5), Time10_00 = lessonRepository.GetById(5)};
            var schoolday3 = new SchoolDayRoom { LessonRoom = roomRepository.GetById(1), Day = DayOfWeekEnum.Wednesday, Time14_00 = lessonRepository.GetById(6), Time15_00 = lessonRepository.GetById(6), Time16_00 = lessonRepository.GetById(6), Time10_00 = lessonRepository.GetById(8), Time11_00 = lessonRepository.GetById(8) };
            var schoolday4 = new SchoolDayRoom { LessonRoom = roomRepository.GetById(1), Day = DayOfWeekEnum.Thursday, Time9_00 = lessonRepository.GetById(7), Time10_00 = lessonRepository.GetById(7), Time11_00 = lessonRepository.GetById(7), Time15_00 = lessonRepository.GetById(9), Time16_00 = lessonRepository.GetById(9) };
            var schoolday5 = new SchoolDayRoom { LessonRoom = roomRepository.GetById(1), Day = DayOfWeekEnum.Friday, Time15_00 = lessonRepository.GetById(4), Time16_00 = lessonRepository.GetById(4), Time17_00 = lessonRepository.GetById(4)  };

            var schoolday6 = new SchoolDayRoom { LessonRoom = roomRepository.GetById(2), Day = DayOfWeekEnum.Friday, Time8_00 = lessonRepository.GetById(10), Time9_00 = lessonRepository.GetById(10), Time10_00 = lessonRepository.GetById(10) };
            var schoolday7 = new SchoolDayRoom { LessonRoom = roomRepository.GetById(2), Day = DayOfWeekEnum.Thursday, Time16_00 = lessonRepository.GetById(11), Time17_00 = lessonRepository.GetById(11), Time18_00 = lessonRepository.GetById(11) };
            var schoolday8 = new SchoolDayRoom { LessonRoom = roomRepository.GetById(2), Day = DayOfWeekEnum.Wednesday, Time12_00 = lessonRepository.GetById(12), Time13_00 = lessonRepository.GetById(12), Time14_00 = lessonRepository.GetById(12) };


            lessonRepository.GetById(2).SchoolDay = schoolday1;
            lessonRepository.GetById(3).SchoolDay = schoolday1;
            lessonRepository.GetById(4).SchoolDay = schoolday5;
            lessonRepository.GetById(5).SchoolDay = schoolday2;
            lessonRepository.GetById(6).SchoolDay = schoolday3;
            lessonRepository.GetById(7).SchoolDay = schoolday4;
            lessonRepository.GetById(8).SchoolDay = schoolday3;
            lessonRepository.GetById(9).SchoolDay = schoolday4;
            lessonRepository.GetById(10).SchoolDay = schoolday6;
            lessonRepository.GetById(11).SchoolDay = schoolday7;
            lessonRepository.GetById(12).SchoolDay = schoolday8;

            schooldayRepository.Insert(schoolday1);
            schooldayRepository.Insert(schoolday2);
            schooldayRepository.Insert(schoolday3);
            schooldayRepository.Insert(schoolday4);
            schooldayRepository.Insert(schoolday5);
            schooldayRepository.Insert(schoolday6);
            schooldayRepository.Insert(schoolday7);
            schooldayRepository.Insert(schoolday8);

            context.SaveChanges();


            // Assign sample student to lessons
            var studentRepository = new Repository<UserProfile>(context);
            var selectedStudent = studentRepository.GetById(1);
            selectedStudent.Lessons = new List<Lesson>();
            selectedStudent.Lessons.Add(lessonRepository.GetById(3));
            selectedStudent.Lessons.Add(lessonRepository.GetById(5));
            selectedStudent.Lessons.Add(lessonRepository.GetById(8));
            selectedStudent.Lessons.Add(lessonRepository.GetById(12));
            selectedStudent.Lessons.Add(lessonRepository.GetById(9));
            selectedStudent.Lessons.Add(lessonRepository.GetById(10));
            selectedStudent.Lessons.Add(lessonRepository.GetById(4));
            selectedStudent.Lessons.Add(lessonRepository.GetById(8));
            selectedStudent.Lessons.Add(lessonRepository.GetById(9));

            base.Seed(context);
        }
    }
}