using Microsoft.AspNetCore.Mvc;
using Nop.Plugin.Misc.AuctionPlugin;
using Nop.Plugin.Misc.AuctionPlugin.Areas.Admin.Models;
using Nop.Plugin.Misc.AuctionPlugin.Domains.Entities;
using Nop.Plugin.Misc.AuctionPlugin.Services;
using Nop.Services.Catalog;
using Nop.Services.Cms;
using Nop.Services.Common;
using Nop.Services.Logging;
using Nop.Web.Areas.Admin.Components;
using Nop.Web.Areas.Admin.Models.Catalog;
using Nop.Web.Areas.Admin.Models.Orders;
using Nop.Web.Framework.Components;
using Nop.Web.Framework.Factories;
using Nop.Web.Framework.Infrastructure;
using Nop.Web.Models.Catalog;
using System.Linq.Expressions;


namespace Nop.Plugin.Misc.AuctionPlugin.Areas.Admin.Components
{
    [ViewComponent(Name = "AuctionAdmin")]
    public class AuctionAdminViewComponent : AdminWidgetViewComponent
    {
        #region Fields
        protected readonly ILogger _logger;
        protected readonly IProductAttributeService _productAttributeService;
        protected readonly IWidgetPluginManager _widgetPluginManager;
        protected readonly AuctionSettings _auctionSettings;
        protected readonly AuctionService _auctionService;
        protected readonly MyProductModelFactory _productModelFactory;



        #endregion

        #region Ctor

        public AuctionAdminViewComponent(
            IWidgetModelFactory widgetModelFactory,
            ILogger logger,
            IProductAttributeService productAttributeService,
            IWidgetPluginManager widgetPluginManager,
            AuctionSettings auctionSettings,
            AuctionService auctionService,
            MyProductModelFactory productModelFactory) : base(widgetModelFactory)
        {
            _logger = logger;
            _productAttributeService = productAttributeService;
            _widgetPluginManager = widgetPluginManager;
            _auctionSettings = auctionSettings;
            _auctionService = auctionService;
            _productModelFactory = productModelFactory;
        }

        #endregion

        #region Methods

        /// <summary>
        /// Invoke the widget view component
        /// </summary>
        /// <param name="widgetZone">Widget zone</param>
        /// <param name="additionalData">Additional parameters</param>
        /// <returns>
        /// A task that represents the asynchronous operation
        /// The task result contains the view component result
        /// </returns>
        public override async Task<IViewComponentResult> InvokeAsync(string widgetZone, object additionalData)
        {

            //await base.InvokeAsync(widgetZone, additionalData);
            //await _logger.InformationAsync($"AuctionAdminViewComponent.InvokeAsync(); Admin auction widget called from {widgetZone}!");

            if (!await _widgetPluginManager.IsPluginActiveAsync(AuctionDefaults.SystemName))
                return Content(string.Empty);

            //if (!_auctionSettings.Enabled)
            //    return Content(string.Empty);

            //if (additionalData is not ProductDetailsModel model)
            //    return Content(string.Empty);

            var productId = 0;
            if (!widgetZone.Equals(AdminWidgetZones.ProductDetailsButtons))
            {
                await _logger.InformationAsync($"AuctionAdminViewComponent.InvokeAsync(); Admin auction widget not in the right widgetzone {widgetZone}!");
                return Content(string.Empty);
            }

            //ProductDetailsModel detailsModel = await _productModelFactory.PrepareProductDetailsModelAsync(product.Price);

            if (additionalData is not ProductModel productModel)
            {
                await _logger.ErrorAsync($"AuctionAdminViewComponent.InvokeAsync(); (productModel == null); widgetZone: {widgetZone};");
                return Content(string.Empty);
            }

            productId = productModel.Id;

            var auctions = await _auctionService.GetAllAuctionsAsync() ?? [];

            // Assign the auctions to the ViewBag
            ViewBag.Auctions = auctions;
            var viewModel = new ProductAssignToAuctionViewModel();
            viewModel.ProductId = productId;
            viewModel.StartingPrice = productModel.OldPrice;
            viewModel.BidPrice = productModel.OldPrice;
            viewModel.SortIndex = 0;
            //viewModel.InAuctions = await _auctionService.GetAllAuctionsAsync()
            viewModel.InAuctions = [];

            //await _logger.InformationAsync($"AuctionAdminViewComponent.InvokeAsync(); Admin auction widget called from {widgetZone}! II. ");
            return View("~/Plugins/Misc.AuctionPlugin/Areas/Admin/Views/AdminProductAuctionSettingsBox.cshtml", viewModel);
        }

        #endregion
    }

}
