using AntDesign;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Authorization;
using System.Net.Http;
using System.Net.Http.Json;
using wispro.sp.share.webViewObject;
using wispro.sp.web.Services;
using System.IdentityModel.Tokens.Jwt;
using Microsoft.IdentityModel.Tokens;
using System.Linq;
using System.Security.Claims;
using Blazored.LocalStorage;

namespace wispro.sp.web.Pages
{
    public partial class LoginPages
    {
        [Inject] public HttpClient Http { get; set; }
        [Inject] public MessageService MsgSvr { get; set; }
        //[Inject] public AuthenticationStateProvider AuthProvider { get; set; }
        [Inject] public IUserService _UserService { get; set; }
        [Inject] public IAuthService authService { get; set; }

        [Inject] public NavigationManager navigation { get; set; }

        loginDto model = new loginDto();
        bool isLoading;

        async void OnLogin()
        {
            isLoading = true;

            //var httpResponse = await Http.PostAsJsonAsync<loginDto>($"http://localhost:39476/api/account/Login", model);
            //userToken result = await httpResponse.Content.ReadFromJsonAsync<userToken>();
            bool result = await authService.LoginAsync(model);

            if (result)
            {
                navigation.NavigateTo("/Home");
            }
            else
            {
                _ = MsgSvr.Error($"用户名或密码错误");
                //await js.InvokeVoidAsync("alert", "登入失敗");
            }

            //if (string.IsNullOrWhiteSpace(result?.Token) == false)
            //{
            //    _ = MsgSvr.Success($"登录成功");
            //    //((AuthProvider)AuthProvider).MarkUserAsAuthenticated(result);
            //}
            //else
            //{
            //    _ = MsgSvr.Error($"用户名或密码错误");
            //}
            isLoading = false;
            //await InvokeAsync(StateHasChanged);
        }
    }
}
