using Infrastructure.DataAccess;
using Infrastructure.Interfaces;
using Infrastructure.Models;
using Infrastructure.Services;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using System;
using System.Collections.Generic;
using System.Linq;

namespace TestCasePlanner.App_Start
{
    public class DatabaseInitializer
    {
        public DatabaseInitializer() : this(new AppDbContext())
        {
        }

		public DatabaseInitializer(AppDbContext context)
		{
			_context = context;
		}

        private readonly AppDbContext _context;

        public void Start()
        {
            // configure
            _context.Configuration.LazyLoadingEnabled = true;

            // delete database - for debugging purposes
            // System.Data.Entity.Database.Delete("DefaultConnection");

            // add roles
            var adminRole = _context.Roles.FirstOrDefault(r => r.Name == "Admin") ?? _context.Roles.Add(new IdentityRole("Admin"));
            var managerRole = _context.Roles.FirstOrDefault(r => r.Name == "Manager") ?? _context.Roles.Add(new IdentityRole("Manager"));
            var testerRole = _context.Roles.FirstOrDefault(r => r.Name == "Tester") ?? _context.Roles.Add(new IdentityRole("Tester"));
            _context.SaveChanges();

            var userManager = new UserManager<AppUser>(new UserStore<AppUser>(_context));
            if(_context.Users.Count() == 0)
            {
                // add users
                var admin = new AppUser() { UserName = "admin", Email = "joe@admin.web" };
                userManager.Create(admin);
                userManager.AddPassword(admin.Id, "password");
                userManager.AddToRole(admin.Id, adminRole.Name);

                var manager = new AppUser() { UserName = "manager", Email = "joe@manager.web" };
                userManager.Create(manager);
                userManager.AddPassword(manager.Id, "password");
                userManager.AddToRole(manager.Id, managerRole.Name);

                var tester = new AppUser() { UserName = "tester", Email = "joe@tester.web" };
                userManager.Create(tester);
                userManager.AddPassword(tester.Id, "password");
                userManager.AddToRole(tester.Id, testerRole.Name);
                _context.SaveChanges();

                // add projects
                var p1 = new TestProject() { Name = "Default", Description = "Default project containing example setup of application.", ShortName = "DEF", ExternalIDCounter = 3 };
                var p2 = new TestProject() { Name = "Empty", Description = "Empty project.", ShortName = "EMP", ExternalIDCounter = 0 };
                var testProjects = new List<TestProject>() { p1, p2 };
                foreach (var p in testProjects)
                {
                    _context.TestProjects.Add(p);
                }
                _context.SaveChanges();

                // add test suites to default project
                var ts1 = new TestSuite() { Name = "Deployment", ProjectID = p1.ID };
                var ts2 = new TestSuite() { Name = "Configuration", ProjectID = p1.ID };
                var ts3 = new TestSuite() { Name = "Integration", ProjectID = p1.ID };
                var ts4 = new TestSuite() { Name = "Regression", ProjectID = p1.ID };
                var ts5 = new TestSuite() { Name = "Smoke", ProjectID = p1.ID };
                var ts6 = new TestSuite() { Name = "Features", ProjectID = p1.ID };
                var testSuites = new List<TestSuite>() { ts1, ts2, ts3, ts4, ts5, ts6 };
                foreach (var ts in testSuites)
                {
                    _context.TestSuites.Add(ts);
                }
                _context.SaveChanges();

                // add test cases to default project
                var tc1 = new TestCase() { Title = "Example test case",
                    Description = "Example test case description",
                    Prerequsity = "Example test case prerequisties",
                    CreatorID = tester.Id,
                    ProjectID = p1.ID,
                    TestSuiteID = ts1.ID,
                    ExternalID = "DEF1",
                    DateCreated = Convert.ToDateTime("12-4-2014 11:00:00"),
                };
                var tc2 = new TestCase()
                {
                    Title = "Deploy with IIS installed",
                    Description = "Deploy product and verify installer steps.",
                    Prerequsity = "Virtual machine with IIS installed.",
                    CreatorID = tester.Id,
                    ProjectID = p1.ID,
                    TestSuiteID = ts1.ID,
                    ExternalID = "DEF2",
                    DateCreated = Convert.ToDateTime("12-4-2014 12:00:00")
                };
                var tc3 = new TestCase()
                {
                    Title = "Deploy without IIS installed",
                    Description = "Deploy product and verify that IIS is installed in process.",
                    Prerequsity = "Virtual machine without IIS.",
                    CreatorID = tester.Id,
                    ProjectID = p1.ID,
                    TestSuiteID = ts1.ID,
                    ExternalID = "DEF3",
                    DateCreated = Convert.ToDateTime("12-4-2014 13:00:00")
                };
                var testCases = new List<TestCase>() { tc1, tc2, tc3 };
                foreach (var tc in testCases)
                {
                    _context.TestCases.Add(tc);
                }
                _context.SaveChanges();

                // add test plan
                var tp1 = new TestPlan() { Name = "Regression 1/1/2014", CreatorID = admin.Id, ProjectID = p1.ID, DateCreated = Convert.ToDateTime("12-4-2014 12:00:00") };
            }
        }
    }
}