@page "/changelog"
@implements IDisposable;
@inject IGitService gitService;

@layout PageLayout


@if (gitService.IsLoaded())
{
    <LayoutMediumContentComponent>
        <WebsiteTitleComponent>Change Log</WebsiteTitleComponent>

        <PaperComponent>
            <FormLayoutComponent>
                <FormCheckboxComponent Label="Show Important"
                                       Info="Only show important patches. Like database updates to the latest game patch."
                                       Value="@isViewImportant"
                                       OnChange="OnChangeClicked">
                </FormCheckboxComponent>
            </FormLayoutComponent>
        </PaperComponent>

        <PaperComponent>
            @foreach (var patch in Patches.OrderBy(x => x.Date).Reverse())
            {
                @if (!patch.Important.Equals("True") && isViewImportant)
                {
                    continue;
                }

                var daysAgo = Math.Floor(DateTime.Now.Subtract(patch.Date).TotalDays);

                <div class="patchContainer">
                    <div style="display: flex; justify-content: space-between;">
                        <div style="font-size: 1.2rem; font-weight: bolder; margin-bottom:4px;">
                            @patch.Name
                        </div>
                        <div>
                            @if (daysAgo == 0)
                            {
                                <i>Today</i>
                            }
                            else if (daysAgo < 8)
                            {
                                <i>@daysAgo days ago</i>
                            }
                            else
                            {
                                <i>@patch.Date.ToString("dd/MM/yyyy")</i>
                            }

                        </div>
                    </div>
                    <div>
                        @foreach (var change in Changes.FindAll(e => e.GitPatchModelId == patch.Id))
                        {
                            @if (!change.Important.Equals("True") && isViewImportant)
                            {
                                continue;
                            }

                            <div style="display: flex; justify-content: space-between; ">
                                <div>
                                    <div>
                                        <b>
                                            <span>- @change.Name </span>

                                            @if (change.Commit != CommitType.None)
                                            {
                                                <span>(@change.Commit)</span>
                                            }
                                            <span>: </span>
                                        </b> @((MarkupString)change.Description)
                                    </div>
                                </div>
                            </div>
                        }
                    </div>
                </div>
            }

        </PaperComponent>
    </LayoutMediumContentComponent>
}
else
{
    <LoadingComponent/>
}


@code {

    private IEnumerable<GitPatchModel> Patches => gitService.GitPatchModels;

    private List<GitChangeModel> Changes => gitService.GitChangeModels;

    private bool isViewImportant = true;


    protected override void OnInitialized()
    {
        gitService.Subscribe(HasChanged);
    }

    void IDisposable.Dispose()
    {
        gitService.Unsubscribe(HasChanged);
    }


    void OnChangeClicked(ChangeEventArgs changeEventArgs)
    {
        isViewImportant = (bool)changeEventArgs.Value!;
        StateHasChanged();
    }

    void HasChanged()
    {
        StateHasChanged();
    }

    protected override async Task OnInitializedAsync()
    {
        await gitService.Load();
    }

}