@page "/"
@using System.Net.Http.Headers
@using Spacebar.AdminApi.TestClient.Services
@inject Config Config
@inject ILocalStorageService LocalStorage

<PageTitle>Home</PageTitle>

<span style="@($"color: {(IsApiUrlValid ? "green" : "red")};")">Spacebar API URL: </span>
<InputText @bind-Value:get="Config.ApiUrl" @bind-Value:set="@(async (v) => {
                                                                Config.ApiUrl = v!;
                                                                await ValidateAndSaveConfig();
                                                            })"/>
<br/>

<!-- <span style="@($"color: {(IsGatewayUrlValid ? "green" : "red")};")">Spacebar Gateway URL: </span> -->
<!-- <InputText @bind-Value="GatewayUrl" /> -->
<!-- <br /> -->

<span style="@($"color: {(IsCdnUrlValid ? "green" : "red")};")">Spacebar CDN URL: </span>
<InputText @bind-Value:get="Config.CdnUrl" @bind-Value:set="@(async (v) => {
                                                                Config.CdnUrl = v!;
                                                                await ValidateAndSaveConfig();
                                                            })"/>
<br/>

<span style="@($"color: {(IsAdminApiUrlValid ? "green" : "red")};")">Spacebar Admin API URL: </span>
<InputText @bind-Value:get="Config.AdminUrl" @bind-Value:set="@(async (v) => {
                                                                  Config.AdminUrl = v!;
                                                                  await ValidateAndSaveConfig();
                                                              })"/>
<br/>

<span style="@($"color: {(IsAccessTokenValid ? "green" : "red")};")">Access Token: </span>
<InputText @bind-Value:get="Config.AccessToken" @bind-Value:set="@(async (v) => {
                                                                     Config.AccessToken = v!;
                                                                     await ValidateAndSaveConfig();
                                                                 })"/>
<a href="/login">New access token</a>
<br/>

@code {

    private bool IsApiUrlValid { get; set; }

    // private bool IsGatewayUrlValid { get; set; }
    private bool IsCdnUrlValid { get; set; }
    private bool IsAdminApiUrlValid { get; set; }
    private bool IsAccessTokenValid { get; set; }

    protected override async Task OnInitializedAsync() {
        await ValidateAndSaveConfig();
    }

    private async Task ValidateAndSaveConfig() {
        await LocalStorage.SetItemAsync("sb_admin_tc_config", Config);

        using var hc = new HttpClient();
        HttpResponseMessage response;
        try {
            response = await hc.GetAsync(Config.ApiUrl + "/api/v9/policies/instance/domains");
            IsApiUrlValid = response.IsSuccessStatusCode;
        }
        catch {
            IsApiUrlValid = false;
        }

        StateHasChanged();

        // response = await hc.GetAsync(Config.GatewayUrl + "/api/v9/policies/instance");
        // IsGatewayUrlValid = response.IsSuccessStatusCode;
        // StateHasChanged();

        try {
            response = await hc.GetAsync(Config.CdnUrl + "/ping");
            IsCdnUrlValid = response.IsSuccessStatusCode;
        }
        catch {
            IsCdnUrlValid = false;
        }

        StateHasChanged();

        try {
            response = await hc.GetAsync(Config.AdminUrl + "/_spacebar/admin/ping");
            IsAdminApiUrlValid = response.IsSuccessStatusCode;
        }
        catch {
            IsAdminApiUrlValid = false;
        }

        StateHasChanged();

        try {
            var request = new HttpRequestMessage(HttpMethod.Get, Config.AdminUrl + "/_spacebar/admin/whoami");
            request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", Config.AccessToken);
            response = await hc.SendAsync(request);
            IsAccessTokenValid = response.IsSuccessStatusCode;
        }
        catch {
            IsAccessTokenValid = false;
        }

        StateHasChanged();
    }

}