using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using Microsoft.AspNet.Identity.Owin;
using ResourcesManager.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;


using System.Globalization;

using System.Security.Claims;
using System.Threading.Tasks;

using Microsoft.Owin.Security;
using System.IO;
using System.Net;

namespace ResourcesManager
{
    public class MvcApplication : System.Web.HttpApplication
    {
        readonly log4net.ILog logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        protected void Application_Start()
        {
            log4net.Config.XmlConfigurator.Configure(new FileInfo(Server.MapPath("~/Web.config")));

            AreaRegistration.RegisterAllAreas();
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                string adminRole = "Admin";
                string adminUser = "admin@admin.hu";

                string userRole = "User";
                try
                {
                    if (!(db.Users.Any(u => u.UserName == adminUser)))
                    {
                        //Role létrehozása
                        var roleStore = new RoleStore<IdentityRole>(db);
                        var roleManager = new RoleManager<IdentityRole>(roleStore);
                        var role = new IdentityRole
                        {
                            Name = adminRole
                        };
                        roleManager.Create(role);

                        //Admin létrehozása
                        var userStore = new UserStore<ApplicationUser>(db);
                        var userManager = new UserManager<ApplicationUser>(userStore);
                        var user = new ApplicationUser
                        {
                            UserName = adminUser,
                            Email = adminUser
                        };
                        userManager.Create(user, "P@ssword1");
                        userManager.AddToRole(user.Id, adminRole);
                        db.SaveChanges();
                    }

                    //if (!(db.Roles.Any(r => r.Name == userRole)))
                    //{
                    //    var roleStore = new RoleStore<IdentityRole>(db);
                    //    var roleManager = new RoleManager<IdentityRole>(roleStore);
                    //    var role = new IdentityRole
                    //    {
                    //        Name = userRole
                    //    };
                    //    roleManager.Create(role);

                    //}
                }
                catch (Exception e )
                {
                    logger.Error("Admin User, Role create", e);
                }

            }
        }


    }




}


