using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Microsoft.AspNetCore.Mvc.Routing;
using Nop.Core;
using Nop.Core.Domain.Catalog;
using Nop.Data;
using Nop.Plugin.Misc.AuctionPlugin.Areas.Admin.Components;
using Nop.Plugin.Misc.AuctionPlugin.Components;

//using Nop.Plugin.Misc.AuctionPlugin.Components;
using Nop.Services.Catalog;
using Nop.Services.Cms;
using Nop.Services.Common;
using Nop.Services.Configuration;
using Nop.Services.Localization;
using Nop.Services.Plugins;
using Nop.Web.Framework.Infrastructure;
using Nop.Web.Framework.Menu;

namespace Nop.Plugin.Misc.AuctionPlugin
{
    /// <summary>
    /// Rename this file and change to the correct type
    /// </summary>
    public class AuctionPlugin : BasePlugin, IWidgetPlugin, IMiscPlugin, IAdminMenuPlugin
    {

        #region Fields

        private readonly IWorkContext _context;
        private readonly ILocalizationService _localizationService;
        private readonly ISettingService _settingService;
        private readonly IProductAttributeService _productAttributeService;
        protected readonly IUrlHelperFactory _urlHelperFactory;
        protected readonly IActionContextAccessor _actionContextAccessor;


        #endregion


        #region Ctr

        public AuctionPlugin(IWorkContext context, 
            ILocalizationService localizationService, 
            ISettingService settingService, 
            IProductAttributeService productAttributeService, 
            UrlHelperFactory urlHelperFactory,
            IActionContextAccessor actionContextAccessor)
        {
            _context = context;
            _localizationService = localizationService;
            _settingService = settingService;
            _productAttributeService = productAttributeService;
            _urlHelperFactory = urlHelperFactory;
            _actionContextAccessor = actionContextAccessor;
        }

        #endregion


        public override string GetConfigurationPageUrl()
        {
            return _urlHelperFactory.GetUrlHelper(_actionContextAccessor.ActionContext).RouteUrl(AuctionDefaults.ConfigurationRouteName);
        }

        public override async Task InstallAsync()
        {
            await _settingService.SaveSettingAsync(new AuctionSettings
            {
                SomeId = 1,
                SomeText = "Hello",
            });

            var auctionAttribute = new ProductAttribute 
            { 
                Name = AuctionDefaults.AuctionAttributeName, 
                Description = "wether the product is on auction" 
            };

            await _productAttributeService.InsertProductAttributeAsync(auctionAttribute);

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

        public override async Task UninstallAsync()
        {
            var result = await _productAttributeService.GetAllProductAttributesAsync();
            var thisAttribute = result.FirstOrDefault(x => x.Name == AuctionDefaults.AuctionAttributeName);
            await _productAttributeService.DeleteProductAttributeAsync(thisAttribute);
            await base.UninstallAsync();
        }


        public bool HideInWidgetList => false;

        public Type GetWidgetViewComponent(string widgetZone)
        {
            ArgumentNullException.ThrowIfNull(widgetZone);

            if (widgetZone.Equals(PublicWidgetZones.ProductDetailsOverviewTop) || widgetZone.Equals(PublicWidgetZones.ProductBoxAddinfoAfter))
            {
                return typeof(AuctionPublicViewComponent);
            }

            if (widgetZone.Equals(AdminWidgetZones.ProductDetailsButtons))
            {
                return typeof(AuctionAdminViewComponent);
            }

            if (widgetZone.Equals(PublicWidgetZones.BodyStartHtmlTagAfter))
            {
                return typeof(LiveAnnouncementViewComponent);
            }

            if (widgetZone.Equals(PublicWidgetZones.ProductBoxAddinfoBefore))
            {
                return typeof(AuctionViewComponent);
            }

            return typeof(AuctionViewComponent);
        }

        public Task<IList<string>> GetWidgetZonesAsync()
        {
            return Task.FromResult<IList<string>>(new List<string>
            {
                PublicWidgetZones.ProductDetailsOverviewTop,
                PublicWidgetZones.ProductBoxAddinfoBefore,
                PublicWidgetZones.ProductBoxAddinfoAfter,
                PublicWidgetZones.BodyStartHtmlTagAfter,
                AdminWidgetZones.ProductDetailsButtons

                //AdminWidgetZones.OrderBillingAddressDetailsBottom,
                //AdminWidgetZones.OrderShippingAddressDetailsBottom
            });
        }

        public async Task ManageSiteMapAsync(SiteMapNode rootNode)
        {
            var liveAnnouncementPluginNode = rootNode.ChildNodes.FirstOrDefault(x => x.SystemName == "Auction");

            if (liveAnnouncementPluginNode == null)
            {
                liveAnnouncementPluginNode = new SiteMapNode()
                {
                    SystemName = "Auction",
                    Title = "Live Auction",
                    Visible = true,
                    IconClass = "fa-gear"
                };
                rootNode.ChildNodes.Add(liveAnnouncementPluginNode);
            }



            liveAnnouncementPluginNode.ChildNodes.Add(new SiteMapNode()
            {
                Title = await _localizationService.GetResourceAsync("Admin.Auction.Configure"),
                Visible = true,
                IconClass = "fa-dot-circle-o",
                Url = "~/Admin/AuctionPlugin/Configure"

            });

            liveAnnouncementPluginNode.ChildNodes.Add(new SiteMapNode()
            {
                Title = await _localizationService.GetResourceAsync("Misc.Auction"),
                Visible = true,
                IconClass = "fa-dot-circle-o",
                Url = "~/Admin/AuctionPluginAdmin/GetAuctionViewModel"
            });

            liveAnnouncementPluginNode.ChildNodes.Add(new SiteMapNode()
            {
                Title = await _localizationService.GetResourceAsync("Misc.AuctionList"),
                Visible = true,
                IconClass = "fa-dot-circle-o",
                Url = "~/Admin/AuctionPluginAdmin/AuctionList"
            });

            liveAnnouncementPluginNode.ChildNodes.Add(new SiteMapNode()
            {
                Title = await _localizationService.GetResourceAsync("Misc.Announcement"),
                Visible = true,
                IconClass = "fa-dot-circle-o",
                Url = "~/Admin/Announcement/GetAnnouncementViewModel"

            });



            liveAnnouncementPluginNode.ChildNodes.Add(new SiteMapNode()
            {
                Title = await _localizationService.GetResourceAsync("Misc.AnnouncementList"),
                Visible = true,
                IconClass = "fa-dot-circle-o",
                Url = "~/Admin/Announcement/AnnouncementList"
            });

            liveAnnouncementPluginNode.ChildNodes.Add(new SiteMapNode()
            {
                Title = await _localizationService.GetResourceAsync("Misc.TestPage"),
                Visible = true,
                IconClass = "fa-dot-circle-o",
                Url = "~/Admin/AuctionPluginAdmin/TestPage"
            });

            liveAnnouncementPluginNode.ChildNodes.Add(new SiteMapNode()
            {
                Title = await _localizationService.GetResourceAsync("Misc.SendBid"),
                Visible = true,
                IconClass = "fa-dot-circle-o",
                Url = "~/Admin/Announcement/SendBidNotificationViewModel"
            });

        }
    }
}