using Nop.Services.Common;
using Nop.Services.Configuration;
using Nop.Services.Security;
using Nop.Web.Framework.Menu;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Nop.Services.Localization;
using Nop.Core.Domain.Security;
using Nop.Core.Data;
using Nop.Services.Customers;
using Nop.Services.Cms;
using Nop.Core;
using Nop.Plugin.SigmaSolve.StyleEditor.Data;
using Nop.Services.Plugins;
using Microsoft.AspNetCore.Routing;
using Nop.Core.Infrastructure;

namespace Nop.Plugin.SigmaSolve.StyleEditor
{
    class PluginConfigurations : BasePlugin, IAdminMenuPlugin
    {
        private StyleEditorContext _context;

        #region Fields

        private readonly ISettingService _settingService;
        private readonly IPermissionService _permissionService;
        private readonly IStoreContext _storeContext;
        private readonly ICustomerService _customerService;
        private readonly PluginSettings _pluginSettings;
        private readonly IWorkContext workContext;

        #endregion

        #region Ctor

        public PluginConfigurations(IStoreContext storeContext,
            StyleEditorContext context,
            ISettingService settingService,
            IPermissionService permissionService,
            ICustomerService customerService,
            IWorkContext _workContext)
        {
            this._context = EngineContext.Current.Resolve<StyleEditorContext>();
            this._storeContext = storeContext;
            this._settingService = settingService;
            this._permissionService = permissionService;
            this._customerService = customerService;
            this._pluginSettings = _settingService.LoadSetting<PluginSettings>(_storeContext.CurrentStore.Id);
            this.workContext = _workContext;
        }

        #endregion


        #region Methods
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public bool Authenticate()
        {
            return true;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public void ManageSiteMap(SiteMapNode rootNode)
        {
            var menuItem = new SiteMapNode()
            {
                SystemName = "StyleEditor",
                Title = "Style Editor",
                ControllerName = "StyleEditor",
                ActionName = "Index",
                Visible = true,
                RouteValues = new RouteValueDictionary() { { "Area", "Admin" } },
            };
            bool customerHasRoleX = EngineContext.Current.Resolve<IWorkContext>().CurrentCustomer.CustomerRoles.Any(x => x.Name.Contains("Admin") || x.Name.Contains("Store"));
            if (customerHasRoleX)
            {
                var pluginNode = rootNode.ChildNodes.FirstOrDefault(x => x.SystemName == "Content Management");
                if (pluginNode != null)
                    pluginNode.ChildNodes.Add(menuItem);
                else
                    rootNode.ChildNodes.Add(menuItem);
            }
        }

        public void AddPermissionToCustomerRole(string systemName, string customerRoleName)
        {
            var role = _customerService.GetCustomerRoleBySystemName(customerRoleName);
            if (role != null)
            {
                var permission = _permissionService.GetPermissionRecordBySystemName(systemName);
                if (permission != null)
                {
                    if (permission.PermissionRecordCustomerRoleMappings.Where(x => x.CustomerRoleId == role.Id).Count() == 0)
                    {
                        permission.PermissionRecordCustomerRoleMappings.Add(new PermissionRecordCustomerRoleMapping { CustomerRole = role });
                        _permissionService.UpdatePermissionRecord(permission);
                    }
                }
            }

        }
        public void AddPermission(string systemName, string Name, string caregory)
        {
            PermissionRecord permission = new PermissionRecord();
            permission.SystemName = systemName;
            permission.Name = Name;
            permission.Category = caregory;
            _permissionService.InsertPermissionRecord(permission);
            AddPermissionToCustomerRole(systemName, "Administrators");
        }
        public void DeletePermission(string category, string systemName)
        {
            var permissions = _permissionService.GetAllPermissionRecords().Where(x => x.Category == category && x.SystemName == systemName).ToList();
            foreach (var permission in permissions)
                _permissionService.DeletePermissionRecord(permission);
        }
        /// <summary>
        /// Install plugin
        /// </summary>
        public override void Install()
        {
            _context.Install();
            base.Install();
        }

        public override void Uninstall()
        {
            _context.Uninstall();
            base.Uninstall();
        }
        #endregion
    }
}