@page "/variables"

@using Newtonsoft.Json
@using TestingRoom_NN_Kajk_UI.Models

@inject HttpClient HttpClient

<PageTitle>Variables</PageTitle>
<div class="row">
    <div class="text-center align-content-center align-items-center align-self-center justify-content-center">
        <table class="table table-striped">
            <thead>
                <tr>
                    <th>Proměnná</th>
                    <th>Hodnota</th>
                    <th>Ulož</th>
                    <th>Výchozí?</th>
                </tr>
            </thead>
            <tbody>
                @foreach (VariablesModel s in VariableDetails)
                {
                    <tr class="text-center align-content-center align-items-center align-self-center justify-content-center">
                        <td>@s.Name</td>

                        <td>
                            @if (float.TryParse(s.Value.Replace(".", ","), out float parsed2))
                            {
                                <input type="number" step="0.001" class="form-control text-center" value="@s.Value" @onchange="(x) => s.Value = x.Value.ToString()" />
                            }
                            else
                            {
                                <input type="text" class="form-control text-center" @bind="s.Value" />
                            }
                        </td>

                        <td>
                            <button class="btn btn-primary" @onclick="() => PostVariableData(s)">Ulož</button>
                        </td>

                        <td>
                            <div class="form-check form-switch">
                                <input type="checkbox" class="form-check-input" role="switch" @bind="s.IsDefault" />
                            </div>
                        </td>
                    </tr>
                }
            </tbody>
        </table>
    </div>
</div>


@code {
    private List<VariablesModel> VariableDetails = new List<VariablesModel>();

    protected override void OnInitialized()
    {
        base.OnInitialized();

        GetVariablesDerails();
    }

    private async Task<List<string>> GetVariablesNames()
    {
        string str = await HttpClient.GetStringAsync("/api/variables/");
        Console.WriteLine(str);
        List<string> list = JsonConvert.DeserializeObject<List<string>>(str);
        list.Sort();
        return list;
    }

    private async void GetVariablesDerails()
    {
        List<string> VariableNames = await GetVariablesNames();
        Console.WriteLine($"Variable Count: {VariableNames.Count}");
        foreach (string name in VariableNames)
        {
            Console.WriteLine(name);
            VariableDetails.Add(new VariablesModel
                {
                    Name = name,
                    IsDefault = false,
                    Value = (await HttpClient.GetStringAsync($"/api/variables/{name}")).Replace("\"", "")
                });
            await InvokeAsync(StateHasChanged);
        }
    }

    private async void PostVariableData(VariablesModel variables)
    {
        await HttpClient.PostAsJsonAsync($"/api/variables/{variables.Name}", new VariablePostModel { data = variables.Value, Isdefault = variables.IsDefault });
    }









}
