using DocumentFormat.OpenXml.EMMA;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.SignalR;
using Nop.Core.Domain.Customers;
using Nop.Plugin.Misc.SignalRApi.Services;
using Nop.Services.Customers;
using Nop.Services.Localization;
using Nop.Web.Models.Customer;

namespace Nop.Plugin.Misc.SignalRApi.Hubs
{

    public class MainHub : Hub<IMainHubClient>
    {

        private readonly SignalRservice _signalRservice;
        private readonly CustomerService _customerService;
        private readonly CustomerRegistrationService _customerRegistrationService;
        private readonly LocalizationService _localizationService;

        public MainHub(SignalRservice signalRservice, CustomerRegistrationService customerRegistrationService, CustomerService customerService, LocalizationService localizationService)
        {
            _signalRservice = signalRservice;
            _customerRegistrationService = customerRegistrationService;
            _customerService = customerService;
            _localizationService = localizationService; 
        }

        // Existing method to send offers to clients
        public async Task SendOffersToUser(List<string> message)
        {
            await Clients.All.SendOffersToUser(message);
        }

        // New method to handle incoming messages from clients
        public async Task ReceiveMessageFromClient(string message)
        {
            // Broadcast the message received from the client to all clients
            Console.Write($"Received message: {message}");
            await Clients.All.SendOffersToUser(new List<string> { message });
            //await _signalRservice.TestHub();
        }

        public async Task ValidateUser(LoginModel loginModel)
        {
            string result = "";
            Console.Write($"Received login request");
            var returnUrl = "/signalrtest";
            if (loginModel!=null)
            {                
                var customerEmail = loginModel.Email;                

                var loginResult = await _customerRegistrationService.ValidateCustomerAsync(customerEmail, loginModel.Password);
                switch (loginResult)
                {
                    case CustomerLoginResults.Successful:
                        {
                            var customer = await _customerService.GetCustomerByEmailAsync(customerEmail);

                            //var valami = await _customerRegistrationService.SignInCustomerAsync(customer, returnUrl, loginModel.RememberMe);

                            result = customer.FirstName;

                            break;
                        }
                    
                    case CustomerLoginResults.CustomerNotExist:
                        result = await _localizationService.GetResourceAsync("Account.Login.WrongCredentials.CustomerNotExist");
                        break;
                    case CustomerLoginResults.Deleted:
                        result = await _localizationService.GetResourceAsync("Account.Login.WrongCredentials.Deleted");
                        break;
                    case CustomerLoginResults.NotActive:
                        result = await _localizationService.GetResourceAsync("Account.Login.WrongCredentials.NotActive");
                        break;
                    case CustomerLoginResults.NotRegistered:
                        result = await _localizationService.GetResourceAsync("Account.Login.WrongCredentials.NotRegistered");
                        break;
                    case CustomerLoginResults.LockedOut:
                        result = await _localizationService.GetResourceAsync("Account.Login.WrongCredentials.LockedOut");
                        break;
                    case CustomerLoginResults.WrongPassword:
                    default:
                        result = await _localizationService.GetResourceAsync("Account.Login.WrongCredentials");
                        break;
                }
            }
            await Clients.Caller.SendOffersToUser(new List<string> { result });
        }

    }

}
