using Microsoft.AspNetCore.Mvc;
using Nop.Core;
using Nop.Plugin.Misc.AuctionPlugin.Domains.Entities;
using Nop.Plugin.Misc.AuctionPlugin.Domains.Enums;
using Nop.Plugin.Misc.AuctionPlugin.Models;
using Nop.Plugin.Misc.AuctionPlugin.Services;
using Nop.Services.Cms;
using Nop.Services.Logging;
using Nop.Web.Framework.Components;
using Nop.Web.Framework.Infrastructure;
using Nop.Web.Models.Catalog;

namespace Nop.Plugin.Misc.AuctionPlugin.Components;

public class AuctionViewComponent : NopViewComponent
{
    #region Fields

    protected readonly ILogger _logger;
    protected readonly IWidgetPluginManager _widgetPluginManager;
    protected readonly IWorkContext _workContext;
    AuctionService _auctionService;
    protected readonly AuctionSettings _auctionSettings;

    #endregion

    #region Ctor

    public AuctionViewComponent(ILogger logger,
        IWidgetPluginManager widgetPluginManager,
        IWorkContext workContext,
        AuctionService auctionService,
        AuctionSettings auctionSettings)
    {
        _logger = logger;
        _widgetPluginManager = widgetPluginManager;
        _workContext = workContext;
        _auctionService = auctionService;
        _auctionSettings = auctionSettings;
    }

    #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 async Task<IViewComponentResult> InvokeAsync(string widgetZone, object additionalData)
    {
        //await _logger.InformationAsync("Auction widget called");

        //ensure that a widget is active and enabled
        var customer = await _workContext.GetCurrentCustomerAsync();
        var currency = await _workContext.GetWorkingCurrencyAsync();
        //if (!await _widgetPluginManager.IsPluginActiveAsync(AuctionDefaults.SystemName, customer))
        //    return Content(string.Empty);

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

        if (string.IsNullOrEmpty(_auctionSettings.SomeText))
        {
            await _logger.ErrorAsync("Message error: message is not set", customer: customer);
            return Content(string.Empty);
        }

        ////display this on the checkout pages only
        //if (ViewData.TemplateInfo.HtmlFieldPrefix != What3wordsDefaults.BillingAddressPrefix &&
        //    ViewData.TemplateInfo.HtmlFieldPrefix != What3wordsDefaults.ShippingAddressPrefix)
        //{
        //    return Content(string.Empty);
        //}
        
        if (!widgetZone.Equals(PublicWidgetZones.ProductBoxAddinfoBefore))
        {
            return Content(string.Empty);
        }

        var productId = ((ProductOverviewModel)additionalData).Id;


        //model.ProductId = productId;

        //TODO: itt a ProductToAuctionMapping.Id-t kellene használni! - J.
        var productToAuctionDtoMappings = await _auctionService.GetProductToAuctionDtosByProductIdAsync(productId);
        if (productToAuctionDtoMappings.Count == 0)
        {
            return Content(string.Empty);
        }

        //TODO: itt a ProductToAuctionMapping.Id-t kellene használni! - J.
        var auctionDto = (await _auctionService.GetAuctionDtoByIdAsync(productToAuctionDtoMappings.FirstOrDefault()!.AuctionId, false, false));
        auctionDto.ProductToAuctionDtos.AddRange(productToAuctionDtoMappings);

        var model = new AuctionPublicInfoModel(auctionDto);
        model.WorkingCurrencyCode = currency.CurrencyCode;
        model.WorkingCurrencyRate = currency.Rate;
        //model.ProductToAuctionMappingId = productToAuctionMapping.Id;
        //var auction = await _auctionService.GetAuctionDtoByProductToAuctionIdAsync(productToAuctionMapping.Id);
        //model.StartDate = auction.StartDateUtc;
        //AuctionStatus status = productToAuctionMapping.AuctionStatus;
        //model.IsActive = status.HasFlag(AuctionStatus.Active);

        //bool isFirstWarning = status.HasFlag(AuctionStatus.FirstWarning);

        return View("~/Plugins/Misc.AuctionPlugin/Views/PublicInfo.cshtml", model);

    }

    #endregion
}