using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Microsoft.AspNetCore.Mvc.Routing;
using Microsoft.AspNetCore.Routing;
using Nop.Core.Domain.Media;
using Nop.Services.Common;
using Nop.Services.Configuration;
using Nop.Services.Localization;
using Nop.Services.Plugins;
using Nop.Services.ScheduleTasks;
using Nop.Services.Security;
using Nop.Web.Framework;
using Nop.Web.Framework.Menu;

namespace Nop.Plugin.Misc.SignalRApi
{
    /// <summary>
    /// Rename this file and change to the correct type
    /// </summary>
    public class MiscSignalRApiPlugin : BasePlugin, IAdminMenuPlugin, IMiscPlugin
    {
        protected readonly IActionContextAccessor _actionContextAccessor;
        protected readonly IPermissionService _permissionService;
        protected readonly ILocalizationService _localizationService;
        protected readonly IUrlHelperFactory _urlHelperFactory;
        private readonly IAdminMenu _adminMenu;

        public MiscSignalRApiPlugin(IAdminMenu adminMenu, IActionContextAccessor actionContextAccessor, ILocalizationService localizationService, IPermissionService permissionService, IUrlHelperFactory urlHelperFactory)
        {
            _adminMenu = adminMenu;
            _actionContextAccessor = actionContextAccessor;
            _localizationService = localizationService;
            _permissionService = permissionService;            
            _urlHelperFactory = urlHelperFactory;            
        }

        public override async Task InstallAsync()
        {
            //await _settingService.SaveSettingAsync(new PayPalStandardPaymentSettings
            //{
            //    UseSandbox = true
            //});

            //await _localizationService.AddOrUpdateLocaleResourceAsync(new Dictionary<string, string>
            //{
            //    //add custom translation
            //});
            await base.InstallAsync();
        }

        public override async Task UninstallAsync()
        {            
            await base.UninstallAsync();
        }

        //public Task ManageSiteMapAsync(AdminMenuItem rootNode)
        //{
        //    throw new NotImplementedException();
        //}

        public async Task ManageSiteMapAsync(AdminMenuItem rootNode)
        {
            if (!await _permissionService.AuthorizeAsync(StandardPermission.Configuration.MANAGE_PLUGINS))
                return;

            var configurationItem = rootNode.ChildNodes.FirstOrDefault(node => node.SystemName.Equals("Configuration"));
            if (configurationItem is null)
                return;

            var shippingItem = configurationItem.ChildNodes.FirstOrDefault(node => node.SystemName.Equals("Shipping"));
            var widgetsItem = configurationItem.ChildNodes.FirstOrDefault(node => node.SystemName.Equals("Widgets"));
            if (shippingItem is null && widgetsItem is null)
                return;

            var index = shippingItem is not null ? configurationItem.ChildNodes.IndexOf(shippingItem) : -1;
            if (index < 0)
                index = widgetsItem is not null ? configurationItem.ChildNodes.IndexOf(widgetsItem) : -1;
            if (index < 0)
                return;

            configurationItem.ChildNodes.Insert(index + 1, new AdminMenuItem
            {
                Visible = true,
                SystemName = "API plugins",
                Title = await _localizationService.GetResourceAsync("Plugins.Misc.SignalRApi.Menu.Api"),
                IconClass = "far fa-dot-circle",
                ChildNodes = new List<AdminMenuItem>
            {
                new()
                {
                    Visible = true,
                    SystemName = PluginDescriptor.SystemName,
                    Title = PluginDescriptor.FriendlyName,
                    IconClass = "far fa-circle",
                    Url = _adminMenu.GetMenuItemUrl("SignalRApi", "Configure"),
                    //Url = "Admin/SignalRApi/Configure",
                    //ControllerName = "SignalRApi",
                    //ActionName = "Configure",                    
                    //RouteValues = new RouteValueDictionary { { "area", AreaNames.ADMIN } }
                }
            }
            });
        }

        public override string GetConfigurationPageUrl()
        {
            return _urlHelperFactory.GetUrlHelper(_actionContextAccessor.ActionContext).RouteUrl("Plugin.Misc.SignalRApi.Configure");
        }
    }
}