@inherits ViewBase
@code {
    [Inject]
    private FnzDataSetHelper DataSetHelper { get; set; }

    private bool IsLoading = true;
    private string LoadingErrorMessage;

    private ProgressBar ProgressBar1;
    private ProgressBar ProgressBar2;
    private ProgressBar ProgressBar3;
    private ProgressBar ProgressBar4;

    private SettingDialog dlgSetting;

    private enum ViewNames { Browse, Search, Playlists }
    private ViewNames CurrentView { get => LocalStorage.Get<ViewNames>(); set => LocalStorage.Set(value); }

    protected override async Task OnInitializedAsync()
    {
        if (CurrentView == 0) CurrentView = ViewNames.Browse;
        base.OnInitialized();
        await LoadData();
    }
}

<SettingDialog @ref="dlgSetting" ReloadData="async ()=>await LoadData()" />

<div class="container">
    <a style="float:right" href="/classic-index">Back OLD Home Page</a>
    <h3>Blazor WASM UI</h3>

    <ul class="nav nav-tabs justify-content-center nav-fill" id="MainNavBar">
        @foreach (ViewNames item in Enum.GetValues(typeof(ViewNames)))
        {
            <li class="nav-item">
                <button class="nav-link @(item==CurrentView ?"active":"")" type="button" @onclick="()=>CurrentView=item">
                    @item
                </button>
            </li>
        }
        <li class="nav-item" role="presentation">
            <button class="nav-link" type="button" role="tab" @onclick="()=>dlgSetting.Show()">
                <i class="bi bi-gear"></i>
            </button>
        </li>
    </ul>

    @if (IsLoading)
    {
        @if (LoadingErrorMessage != null)
        {
            <div class="p-3">
                <span class="bi bi-x-circle text-danger" role="status" />
                <span>@LoadingErrorMessage</span>
            </div>
        }
        else
        {
            <div class="p-3">
                <ProgressBar @ref="ProgressBar1" Throttle="500"></ProgressBar>
            </div>
            <div class="p-3">
                <ProgressBar @ref="ProgressBar2" Throttle="500"></ProgressBar>
            </div>
            <div class="p-3">
                <ProgressBar @ref="ProgressBar3" Throttle="500"></ProgressBar>
            </div>
            <div class="p-3">
                <ProgressBar @ref="ProgressBar4" Throttle="500"></ProgressBar>
            </div>
        }
    }

    @if (!IsLoading)
    {
        switch (CurrentView)
        {
            case ViewNames.Browse:
                <BrowseView></BrowseView>
                break;
            case ViewNames.Search:
                <SearchView></SearchView>
                break;
            case ViewNames.Playlists:
                <PlaylistView></PlaylistView>
                break;
        }
    }

</div>

@code {
    private async Task LoadData()
    {
        IsLoading = true;
        StateHasChanged();
        await Task.Delay(10);

        await ProgressBar1.SetProgress(0, "Waiting server ready");
        await ProgressBar2.SetProgress(0, "Pending load fileset");
        await ProgressBar3.SetProgress(0, "Pending load tags");
        await ProgressBar4.SetProgress(0, "Pending parse models");
        StateHasChanged();
        await Task.Delay(10);
        try
        {
            do
            {
                var r = await ApiClient.GetProgressAsync();
                await ProgressBar1.SetProgress((float)r.LoadedTags / r.TotalTrackCount, $"Waiting server ready {r.LoadedTags}/{r.TotalTrackCount}");
                if (r?.IsLoading == false)
                {
                    await ProgressBar1.SetProgress(100, "Server ready");
                    await ProgressBar1.ForceUpdate();
                    break;
                }
                await Task.Delay(1000);
            } while (true);

            StateHasChanged();
            await Task.Delay(10);

            await DataSetHelper.InitFeModulesAsync(new[] { ProgressBar2, ProgressBar3, ProgressBar4, });

            StateHasChanged();
            await Task.Delay(500);

            IsLoading = false;
        }
        catch (Exception ex)
        {
            LoadingErrorMessage = ex.Message;
        }
    }
}
