using Marco.Pms.DataAccess.Data;
using Marco.Pms.Model.Employees;
using Marco.Pms.Model.Entitlements;
using Marco.Pms.Model.ViewModels.Projects;
using Marco.Pms.Utility;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;

namespace Marco.Pms.DataAccess.Initializer
{
    public class DBInitializer : IDBInitializer
    {

        private readonly ApplicationDbContext _db;
        private readonly UserManager<IdentityUser> _userManager;
        private readonly RoleManager<IdentityRole> _roleManager;
        //private readonly IUnitOfWork _unitOfWork;

        public DBInitializer(UserManager<IdentityUser> userManager, RoleManager<IdentityRole> roleManager, ApplicationDbContext db)
        {
            _userManager = userManager;
            _roleManager = roleManager;
            _db = db;
            //_unitOfWork = unitOfWork;

        }
        public void Initialize()
        {
            // Migration those are not applied
            try
            {
                if (_db.Database.GetPendingMigrations().Count() > 0)
                {
                    _db.Database.Migrate();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }

            // Create roes if not created

            CreateProjectInfra();

            if (!_roleManager.RoleExistsAsync(APP_ROLES.Admin.ToString()).GetAwaiter().GetResult())
            {
                _roleManager.CreateAsync(new IdentityRole(APP_ROLES.Admin.ToString())).GetAwaiter().GetResult();
                _roleManager.CreateAsync(new IdentityRole(APP_ROLES.Management.ToString())).GetAwaiter().GetResult();
                _roleManager.CreateAsync(new IdentityRole(APP_ROLES.SiteEngineer.ToString())).GetAwaiter().GetResult();
                _roleManager.CreateAsync(new IdentityRole(APP_ROLES.SkilledWorker.ToString())).GetAwaiter().GetResult();
                _roleManager.CreateAsync(new IdentityRole(APP_ROLES.UnskilledWorker.ToString())).GetAwaiter().GetResult();


                // Create admin user if roles are not created
                _userManager.CreateAsync(new ApplicationUser
                {
                    UserName = "admin@marcobms.com",
                    Email = "admin@marcobms.com",
                    PhoneNumber = "1234567890",
                    // Street = "Street 1",
                    // State = "State",
                    // Postalcode = "1234567890",
                    //  City = "City",
                    IsRootUser = true,

                }, "User@123").GetAwaiter().GetResult();

                ApplicationUser user = _db.ApplicationUsers.FirstOrDefault(u => u.Email == "admin@marcobms.com") ?? new ApplicationUser();
                _userManager.AddToRoleAsync(user, APP_ROLES.Admin.ToString()).GetAwaiter().GetResult();

                Employee emp = new Employee()
                {
                    FirstName = "Administrator",
                    LastName = "",
                    MiddleName = "",
                    Gender = "",
                    EmergencyPhoneNumber = "1234567890",
                    CurrentAddress = "",
                    ApplicationUserId = user.Id,
                    BirthDate = DateTime.MinValue,
                    PermanentAddress = "",
                    PhoneNumber = ""
                };
                _db.Employees.Add(emp);

                _db.SaveChanges();

            }
        }

        private static void CreateProjectInfra()
        {
            var jsonData = File.ReadAllText("wwwroot/assets/data/buildings.json");

            var entities1 = Newtonsoft.Json.JsonConvert.DeserializeObject<ProjectDetailsVM>(jsonData);

            //foreach (Building item in entities1.buildings)
            //{
            //    _unitOfWork.Building.Add(item);
            //}
            //_unitOfWork.Save();


            //foreach (Floor item in entities1.floors)
            //{
            //    _unitOfWork.Floor.Add(item);
            //}
            //_unitOfWork.Save();

            //foreach (WorkArea item in entities1.workAreas)
            //{
            //    _unitOfWork.WorkArea.Add(item);
            //}
            //_unitOfWork.Save();

            //foreach (WorkItem item in entities1.workItems)
            //{
            //    _unitOfWork.WorkItem.Add(item);
            //}
            //_unitOfWork.Save();
        }
    }

}
