using BlazorBootstrap;
using Common.Models.Auth;
using Common.Models.Auth.CompanySide;
using Common.Models.Auth.UserSide;
using HushianWebApp.Services;
using Microsoft.AspNetCore.Components;
using System.Net.Http.Json;

namespace HushianWebApp.Service
{
    public class AuthService
    {
        private readonly BaseController _baseController;
        private readonly NavigationManager _NavigationManager;
        private readonly ILocalStorageService _localStorage;
        private readonly ToastService _ToastService;
        public AuthService(BaseController baseController, NavigationManager navigationManager, ILocalStorageService localStorage, ToastService toastService)
        {
            _baseController = baseController;
            _NavigationManager = navigationManager;
            _localStorage = localStorage;
            _ToastService = toastService;
        }

        public async Task<bool> IsOnline()
        {
            return await _baseController.IsOnline();
        }
        public async Task<string> login(AuthRequestFromCompanySide authRequest)
        {
            var response = await _baseController.PostLogin("v1/Auth/loginCompanySide", authRequest);
            if (response.Item2.IsSuccessStatusCode)
            {
                var responsevalue = await response.Item2.Content.ReadFromJsonAsync<AuthResponse>();

             
                await _localStorage.RemoveItem("C/ID");
                await _localStorage.SetItem("C/ID", responsevalue.Id);
                await _localStorage.RemoveItem("C/CompanyId");
                await _localStorage.SetItem("C/CompanyId", responsevalue.CompanyId.GetValueOrDefault());
                await _localStorage.RemoveItem("C/Fullname");
                await _localStorage.SetItem("C/Fullname", responsevalue.Fullname);
                await _localStorage.RemoveItem("C/MobileOrUserName");
                await _localStorage.SetItem("C/MobileOrUserName", responsevalue.MobileOrUserName);
                await _localStorage.RemoveItem("C/Role");
                await _localStorage.SetItem("C/Role", responsevalue.Role);
                await _localStorage.RemoveItem("C/img");
                await _localStorage.SetItem("C/img", responsevalue.img);
                await _localStorage.RemoveItem("C/key");
                await _localStorage.SetItem("C/key", responsevalue.Token);
                
                _NavigationManager.NavigateTo("/");
            }
            return response.Item1;


        }
        public async Task<int> login(AuthRequestFromUserSide model)
        {
            var response = await _baseController.Post($"v1/Auth/AuthenticationFromUser",model);
            if (response.IsSuccessStatusCode)
                return await response.Content.ReadFromJsonAsync<int>();
            else
            {

                _ToastService.Notify(new ToastMessage(ToastType.Danger, "کاربر یافت نشد"));
            }

            return 0;
        }
        public async Task<bool> Verificationlogin(int ID,string code)
        {
            var response = await _baseController.Put($"v1/Auth/UserLoginVerification/{ID}/{code}");

            if (response.IsSuccessStatusCode)
            {
                var responsevalue = await response.Content.ReadFromJsonAsync<AuthResponse>();
                await _localStorage.RemoveItem("U/ID");
                await _localStorage.SetItem("U/ID", responsevalue.Id);
                await _localStorage.RemoveItem("U/CompanyId");
                await _localStorage.SetItem("U/CompanyId", responsevalue.CompanyId.GetValueOrDefault());
                await _localStorage.RemoveItem("U/Fullname");
                await _localStorage.SetItem("U/Fullname", responsevalue.Fullname);
                await _localStorage.RemoveItem("U/MobileOrUserName");
                await _localStorage.SetItem("U/MobileOrUserName", responsevalue.MobileOrUserName);
                await _localStorage.RemoveItem("U/Role");
                await _localStorage.SetItem("U/Role", responsevalue.Role);
                await _localStorage.RemoveItem("U/img");
                await _localStorage.SetItem("U/img", responsevalue.img);
                await _localStorage.RemoveItem("U/key");
                await _localStorage.SetItem("U/key", responsevalue.Token);

                await _baseController.RemoveToken();
                await _baseController.SetToken(responsevalue.Token);

                return true;
            }
          
            return false;


        }
        public async Task Logout ()
        {
            await _localStorage.RemoveItem("C/ID");
            await _localStorage.RemoveItem("C/CompanyId");
            await _localStorage.RemoveItem("C/Fullname");
            await _localStorage.RemoveItem("C/MobileOrUserName");
            await _localStorage.RemoveItem("C/Role");
            await _localStorage.RemoveItem("C/img");
            await _localStorage.RemoveItem("C/key");

            await _localStorage.RemoveItem("U/ID");
            await _localStorage.RemoveItem("U/CompanyId");
            await _localStorage.RemoveItem("U/Fullname");
            await _localStorage.RemoveItem("U/MobileOrUserName");
            await _localStorage.RemoveItem("U/Role");
            await _localStorage.RemoveItem("U/img");
            await _localStorage.RemoveItem("U/key");

        }
    }
}
