
using FruitBank.Common.Server;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Microsoft.AspNetCore.Mvc.Routing;
using Nop.Plugin.Misc.FruitBankPlugin.Components;
using Nop.Services.Cms;
using Nop.Services.Configuration;
using Nop.Services.Localization;
using Nop.Services.Plugins;
using Nop.Services.Security;
using Nop.Web.Framework.Infrastructure;
using Nop.Web.Framework.Menu;


namespace Nop.Plugin.Misc.FruitBankPlugin
{
    /// <summary>
    /// Main plugin class
    /// </summary>
    public class FruitBankPlugin : BasePlugin, IWidgetPlugin
    {
        protected readonly IActionContextAccessor _actionContextAccessor;

        private readonly ISettingService _settingService;

        //private readonly IWebHelper _webHelper;
        protected readonly IPermissionService _permissionService;
        protected readonly ILocalizationService _localizationService;
        protected readonly IUrlHelperFactory _urlHelperFactory;
        private readonly IAdminMenu _adminMenu;

        //handle AdminMenuCreatedEvent


        public FruitBankPlugin(IActionContextAccessor actionContextAccessor,
            ISettingService settingService,
            //IWebHelper webHelper,
            ILocalizationService localizationService,
            IPermissionService permissionService,
            IUrlHelperFactory urlHelperFactory,
            IAdminMenu adminMenu)
        {
            _actionContextAccessor = actionContextAccessor;
            _settingService = settingService;
            //_webHelper = webHelper;
            _localizationService = localizationService;
            _urlHelperFactory = urlHelperFactory;
            _adminMenu = adminMenu;
            _permissionService = permissionService;
        }

        // --- INSTALL ---
        public override async Task InstallAsync()
        {
            //TODO: Add "Measuring" role - FruitBankConst.MeasuringRoleSystemName
            //TODO: Add "MeasuringRevisor" role - FruitBankConst.MeasuringRevisorRoleSystemName

            //TODO: Add "IsMeasurable" product attribute - FruitBankConst.IsMeasurableAttributeName
            //TODO: Add "NeedsToBeMeasured" product attribute if not exists

            //TODO: Add unique index to GenericAttribute[EntityId, KeyGroup, Key]???!? ÁTGONDOLNI, mert lehet esetleg lista is a visszaadott, de a nopcommerce kódban felülírja ha azonos key-el vannak! - J.

            // Default settings
            var settings = new FruitBankSettings
            {
                ApiKey = string.Empty
            };
            await _settingService.SaveSettingAsync(settings);
            await _localizationService.AddOrUpdateLocaleResourceAsync("Plugins.Misc.FruitBankPlugin.Menu.ShipmentsList", "Shipment", "EN");
            await _localizationService.AddOrUpdateLocaleResourceAsync("Plugins.Misc.FruitBankPlugin.Menu.ShipmentsList", "Szállítmányok", "HU");
            await base.InstallAsync();
        }

        // --- UNINSTALL ---
        public override async Task UninstallAsync()
        {
            await _settingService.DeleteSettingAsync<FruitBankSettings>();
            await base.UninstallAsync();
        }

        // --- WIDGETS ---
        public bool HideInWidgetList => false;

        public Task<IList<string>> GetWidgetZonesAsync()
        {
            return Task.FromResult<IList<string>>(new List<string> { PublicWidgetZones.ProductBoxAddinfoBefore, PublicWidgetZones.ProductDetailsBottom, AdminWidgetZones.ProductDetailsBlock, AdminWidgetZones.OrderDetailsBlock });
        }

        //public string GetWidgetViewComponentName(string widgetZone)
        //{
        //    return "ProductAIWidget"; // A ViewComponent neve
        //}

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

        //    var pluginNode = new AdminMenuItem
        //    {
        //        SystemName = "FruitBankPlugin.Configure",
        //        Title = "AI Assistant",
        //        Url = $"{_webHelper.GetStoreLocation()}Admin/FruitBankPluginAdmin/Configure",
        //        Visible = true
        //    };
        //    rootNode.ChildNodes.Add(pluginNode);
        //    //return Task.CompletedTask;
        //}

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

        }

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

        public Type GetWidgetViewComponent(string widgetZone)
        {
            if (widgetZone is null)
                throw new ArgumentNullException(nameof(widgetZone));

            var zones = GetWidgetZonesAsync().Result;

            if (zones.Any(widgetZone.Equals))
            {
                if (widgetZone == PublicWidgetZones.ProductBoxAddinfoBefore || widgetZone == PublicWidgetZones.ProductDetailsBottom)
                {
                    return zones.Any(widgetZone.Equals) ? typeof(ProductAIWidgetViewComponent) : null;
                }
                else if (widgetZone == AdminWidgetZones.ProductDetailsBlock)
                {
                    return zones.Any(widgetZone.Equals) ? typeof(ProductAttributesViewComponent) : null;
                }

                else if (widgetZone == AdminWidgetZones.OrderDetailsBlock)
                {
                    return zones.Any(widgetZone.Equals) ? typeof(OrderAttributesViewComponent) : null;
                }
            }
                           
            return null;
            
        }
    }
}
