@page "/mud/project/releases"
@using System.Text.RegularExpressions

@inject GitHubApiClient _gitHubApiClient;

<PageTitle>Releases - MudBlazor</PageTitle>

<DocsPage DisplayFooter="true">
    <DocsPageHeader Title="Releases" />
    <DocsPageContent>
        @if (_githubReleases == null)
        {
            <MudProgressCircular Color="Color.Primary" Indeterminate="true" />
        }
        else
        {
            <MudGrid Class="docs-github-releases">
                @foreach (var release in _githubReleases)
                {
                    <MudItem xs="12" md="4">
                        <div class="docs-sticky-info">
                            <MudText Typo="Typo.h5" HtmlTag="span">Version </MudText>
                            <MudText Typo="Typo.h5" HtmlTag="span" Color="Color.Primary"><b>@release.TagName</b></MudText>
                            <MudText Typo="Typo.subtitle1">Released on @release.PublishedAt.ToString("MMMM dd, yyyy")</MudText>
                        </div>
                    </MudItem>
                    <MudItem xs="12" md="8">
                            @if (release.PublishedAt > _oldReleases)
                            {
                                <div class="docs-github-release-body">
                                    @((MarkupString)GetBody(release.Body))
                                </div>
                            }
                            else
                            {
                                <div class="docs-github-release-body old-releases">
                                    @((MarkupString)GetOldBody(release.Body))
                                </div>
                            }
                    </MudItem>
                }
            </MudGrid>
        }
    </DocsPageContent>
</DocsPage>

@code {
    private GitHubReleases[] _githubReleases;
    private readonly DateTime _oldReleases = DateTime.Parse("2022-11-09");

    protected override async Task OnInitializedAsync()
    {
        _githubReleases = await _gitHubApiClient.GetReleasesAsync();
        StateHasChanged();
    }

    private static string GetBody(string value)
    {
        value = Regex.Replace(value, @"((###\s)(?<subtitle>.+))(?<items>.*(?:\r?\n[*] .*)*)", "<h6 class=\"mud-typography mud-typography-h6\">${subtitle}</h6><ul class=\"mt-3 mb-6 px-6\">${items}</ul>");
        value = Regex.Replace(value, @"((##\s)(?<title>.+))(?<items>.*(?:\r?\n[*] .*)*)", "<h5 class=\"mud-typography mud-typography-h5\">${title}</h5><ul class=\"mt-3 mb-6 px-6\">${items}</ul>");
        value = Regex.Replace(value, @"(([*]\s)(?<row>.+))", "<li>${row}</li>");
        value = Regex.Replace(value, @"(@)(\S+)", "<a target=\"_blank\" href=\"https://github.com/$2\" class=\"mud-link mud-default-text mud-link-underline-hover\"><b>@$2</b></a>");
        value = Regex.Replace(value, @"https://github.com/MudBlazor/MudBlazor/pull/([0-9]{3,6})", "<a target=\"_blank\" href=\"$0\" class=\"mud-link mud-primary-text mud-link-underline-hover\"><b>#$1</b></a>");
        value = Regex.Replace(value, @"([*][*]Full Changelog[*][*]: )(https://github.com/MudBlazor/MudBlazor/compare/)(.+)", "<p class=\"mud-typography mud-typography-body1\">Full Changelog: <a target=\"_blank\" href=\"$2$3\" class=\"docs-code docs-code-primary\">$3</a></p>");
        return value;
    }

    private static string GetOldBody(string value)
    {
        value = Regex.Replace(value, @"((###\s)(?<subtitle>.+))(?<items>.*(?:\r?\n-\s[*][*].*)*)", "<h6 class=\"mud-typography mud-typography-h6\">${subtitle}</h6><ul class=\"mt-3 mb-6 px-6\">${items}</ul>");
        value = Regex.Replace(value, @"^(#\s)(?<title>.+)", "<h5 class=\"mud-typography mud-typography-h5 mud-inherit-text\">${title}</h5>");
        value = Regex.Replace(value, @"((-\s[*][*])(?<row>.+))", "<li>${row}</li>");
        value = Regex.Replace(value, @"(#){1}([0-9]{3,6}){1}", "<a target=\"_blank\" href=\"https://github.com/MudBlazor/MudBlazor/pull/$2\" class=\"mud-link mud-primary-text mud-link-underline-hover\"><b>#$2</b></a>");
        value = Regex.Replace(value, @"\[(.*?)\]\((.*?)\)", "<a target=\"_blank\" href=\"$2\" class=\"mud-link mud-primary-text mud-link-underline-hover\">$1</a>");
        value = Regex.Replace(value, @"(https://github.com/MudBlazor/MudBlazor/compare/)(.+)", "<a target=\"_blank\" href=\"$1$2\" class=\"docs-code docs-code-primary\">$2</a>");
        value = Regex.Replace(value, @"(https://github.com/MudBlazor/MudBlazor/milestone/)(.+)", "<a target=\"_blank\" href=\"$1$2\" class=\"docs-code docs-code-primary\">Milestone</a>");
        value = value.Replace("**","");
        return value;
    }
}