@inherits LayoutComponentBase
@using Components
@using Newtonsoft.Json
@using Models.Identity
@inject ILoadingSpinner _LoadingSpinner


<RoundContainerContainer Padding="0" BackroundImage="true">
    @Body
</RoundContainerContainer>
@code
{


    protected override async void OnAfterRender(bool firstRender)
    {
        if (firstRender)
            await CheckLogin();
        base.OnAfterRender(firstRender);
    }

    async Task CheckLogin()
    {
        Console.WriteLine("" + Statics.LastCheck);
        Console.WriteLine("checking login...");
        Statics.LastCheck = DateTime.Now;
        var hasToken = await LocalStorage.ContainKeyAsync(Statics.LoginTokenKey);
        if (hasToken)
        {
            var token = await LocalStorage.GetItemAsStringAsync(Statics.LoginTokenKey);
            var res = await _userUtils.CheckLogin(goToLogin: false);
            if (res.IsSuccessStatusCode)
            {
                var result =
                JsonConvert.DeserializeObject<CheckLoginResponse>(
                    await res.Content.ReadAsStringAsync());
                await _LoadingSpinner.HideLoading();
                Statics.CurrentLoginInfo = result;
                if (!result.HasName)
                    NavManager.NavigateTo("/NameAndEmail");
                else
                    NavManager.NavigateTo("/dashboard");
            }
            await _LoadingSpinner.HideLoading();
            StateHasChanged();
        }
    }

    private async void SignOut()
    {
        await LocalStorage.RemoveItemAsync(Statics.LoginTokenKey);
        await LocalStorage.RemoveItemAsync(Statics.RefreshTokenKey);
        NavManager.NavigateTo("/login");
    }

}