@using BootStrapComponents
@using Components
@using Models.Identity
@using Newtonsoft.Json
@using BlazorPages.Tools
@page "/"
@layout LayoutWithoutMenu

<img src="/assets/images/logo.png" class="@BT.CenterBlock m-2" style="max-width:30%;" />
@if (DataGotten)
{
    <button class="@BT.CenterBlock @BT.Button(ColorType.primary,outline:true) @BT.Background(ColorType.light) w-50 h-1"
            style="margin-top:calc(50vh - 10em);"
            @onclick="@(()=>NavManager.NavigateTo("/login"))">
        ورود و ثبت نام
    </button>
}
<div class="@BT.Row @BT.TextColor(ColorType.white) @BT.TextAlign(TextAlignment.center)" style="position:sticky;height:200px; top:calc(100vh - 200px);">
    <hr class="@BT.TextColor(ColorType.white)" />
    <p>
        © تمامی حقوق برای حقتو محفوظ میباشد
    </p>
    <br />
    <p>
        ورژن 1.0
    </p>
</div>

@code {
    public bool DataGotten { set; get; }

    protected override async void OnInitialized()
    {
        var hasToken = await LocalStorage.ContainKeyAsync(Statics.LoginTokenKey);
        if (hasToken)
        {
            var token = await LocalStorage.GetItemAsStringAsync(Statics.LoginTokenKey);
            var res = await _userUtils.CheckLogin(goToLogin: false);
            var result =
                JsonConvert.DeserializeObject<CheckLoginResponse>(
                    await res.Content.ReadAsStringAsync());
            if (res.IsSuccessStatusCode)
            {
                await LoadingSpinner.HideLoading();
                Statics.CurrentLoginInfo = result;
                if (!result.HasName)
                    NavManager.NavigateTo("/NameAndEmail");
                else
                    NavManager.NavigateTo("/dashboard");
            }
        }
        DataGotten = true;
        StateHasChanged();
    }

}
