using AyCode.Core.Extensions;
using AyCode.Utils.Extensions;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.SignalR;
using Nop.Core;
using Nop.Plugin.Misc.AuctionPlugin.Domains.Dtos;
using Nop.Plugin.Misc.AuctionPlugin.Domains.Enums;
using Nop.Plugin.Misc.AuctionPlugin.Hubs;
using Nop.Plugin.Misc.AuctionPlugin.Hubs.Messages;
using Nop.Plugin.Misc.AuctionPlugin.Models;
using Nop.Plugin.Misc.AuctionPlugin.Services;
using Nop.Services.Cms;
using Nop.Services.Customers;
using Nop.Services.Logging;
using Nop.Web.Framework.Components;
using Nop.Web.Framework.Infrastructure;

namespace Nop.Plugin.Misc.AuctionPlugin.Components
{

    [ViewComponent(Name = "LiveAnnouncement")]
    public class LiveAnnouncementViewComponent(ILogger logger, ILockService lockService, IWorkContext workContext, IWidgetPluginManager widgetPluginManager, AuctionService auctionService, IHubContext<AuctionHub> auctionHubContext, ICustomerService customerService)
        : NopViewComponent
    {
        public async Task<IViewComponentResult> InvokeAsync(string widgetZone, object additionalData)
        {
            var customer = await workContext.GetCurrentCustomerAsync();
            var currency = await workContext.GetWorkingCurrencyAsync();
            await logger.InformationAsync($"LiveAnnouncementViewComponent.InvokeAsync(); Before lock; widgetZone: {widgetZone}", null, customer);

            //using (await lockService.SemaphoreSlim.UseWaitAsync())
            //{
            //    await logger.InformationAsync($"LiveAnnouncementViewComponent.InvokeAsync(); Enter lock; widgetZone: {widgetZone}", null, customer);

            //    await Task.Delay(1000);

            //    var auctions = await auctionService.GetAllCurrentAutoOpenAndClosedAuctionsAsync();
            //    if (auctions.Count > 0)
            //    {
            //        await logger.InformationAsync($"LiveAnnouncementViewComponent.InvokeAsync(); auctions.Count > 0; count: {auctions.Count}; names: {string.Join("; ", auctions.Select(x => x.AuctionName))}");

            //        var statusChangedMessageWrapper = new MessageWrapper
            //        {
            //            MessageType = nameof(ProductToAuctionStatusNotification),
            //            SenderId = 0,
            //            ResponseType = ResponseType.ToAllClients
            //        };

            //        foreach (var auction in auctions)
            //        {
            //            auction.Closed = false;
            //            await auctionService.UpdateAuctionAsync(auction);

            //            var auctionDto = new AuctionDto(auction);
            //            var productToAuctions = (await auctionService.GetProductToAuctionsByAuctionIdAsync(auction.Id, false)).Where(x => x.AuctionStatus == AuctionStatus.None).ToList();

            //            foreach (var productToAuction in productToAuctions)
            //            {
            //                productToAuction.AuctionStatus = AuctionStatus.Active;
            //                auctionDto.ProductToAuctionDtos.Add(new ProductToAuctionDto(productToAuction));

            //                ////TEMPOPRARY - J.
            //                //statusChangedMessageWrapper.Data = new ProductToAuctionStatusNotification(auctionDto, 0, $"Az aukciót megnyitottuk: {auction.AuctionName}").ToJson();
            //                //await _auctionHubContext.Clients.All.SendAsync("send", statusChangedMessageWrapper.ToJson());
            //                ////TEMPOPRARY - J.
            //            }

            //            await auctionService.UpdateProductToAuctionMappingAsync(productToAuctions);

            //            statusChangedMessageWrapper.Data = new ProductToAuctionStatusNotification(auctionDto, 0, $"Az aukciót megnyitottuk: {auction.AuctionName}").ToJson();
            //            await auctionHubContext.Clients.All.SendAsync("send", statusChangedMessageWrapper.ToJson());
            //        }
            //    }

            //    await logger.InformationAsync($"LiveAnnouncementViewComponent.InvokeAsync(); Exit lock; widgetZone: {widgetZone}", null, customer);
            //}

            await logger.InformationAsync($"SignalR Widget called customer: {customer.Email}");

            if (!await widgetPluginManager.IsPluginActiveAsync(AuctionDefaults.SystemName, customer))
                return Content(string.Empty);

            await logger.InformationAsync("SignalR Widget: widget active");

            if (!widgetZone.Equals(PublicWidgetZones.BodyStartHtmlTagAfter))

                return Content(string.Empty);

            LiveAnnouncementViewModel myModel = new LiveAnnouncementViewModel();

            myModel.CustomerId = customer.Id;
            myModel.StoreId = customer.RegisteredInStoreId; //Temporary - A.
            myModel.WorkingCurrency = currency;
            myModel.IsAdmin = await customerService.IsAdminAsync(customer);
            myModel.IsGuest = await customerService.IsGuestAsync(customer);
            return View("~/Plugins/Misc.AuctionPlugin/Views/LiveAnnouncement.cshtml", myModel);
        }
    }
}
