@using System.IO
@using Radzen.Blazor
@implements IDisposable
@inject ExampleService ExampleService
@inject NavigationManager NavigationManager
@inject IJSRuntime JSRuntime

<PageTitle>@ExampleService.TitleFor(example)</PageTitle>
<HeadContent>
    <meta name="description" content="@ExampleService.DescriptionFor(example)">
    <meta name="author" content="Radzen">
    <meta property="og:title" content="@ExampleService.TitleFor(example)" />
    <meta property="og:description" content="@ExampleService.DescriptionFor(example)" />
    <meta property="og:site_name" content="Radzen Blazor Components">
    <meta property="og:image" content="https://blazor.radzen.com/images/radzen-og.png" />
    <meta property="og:type" content="article" />
    <meta property="og:url" content="@CanonicalUrl" />
    <meta property="twitter:card" content="summary_large_image" />
    <meta property="twitter:title" content="@ExampleService.TitleFor(example)" />
    <meta property="twitter:description" content="@ExampleService.DescriptionFor(example)" />
</HeadContent>
@if (Heading)
{
    <RadzenText TextStyle="TextStyle.H2" TagName="TagName.H1" class="rz-pt-8">
        @Name
    </RadzenText>
    <RadzenText TextStyle="TextStyle.Subtitle1" TagName="TagName.P" class="rz-pb-4">
        Demonstration and configuration of the Radzen Blazor <strong>@Name</strong> component.
    </RadzenText>
}
<div class="documentation-links">
@if (Documentation)
{
    <RadzenLink target="blank" Path="@DocumentationHref" title=@($"Open {Name} documentation in new tab") ><RadzenIcon Icon="launch" style="font-size: 1rem;" class="rz-me-1" /> @Name Docs</RadzenLink>
}
@if (Heading)
{
    <RadzenLink target="blank" Path="@ComponentSourceHref" title=@($"View {Name} source code in new tab") ><RadzenIcon Icon="launch" style="font-size: 1rem;" class="rz-me-1" /> @Name Source</RadzenLink>
}
</div>
<RadzenCard class="rz-p-0 rz-material-3-demo-card">
    <RadzenTabs @bind-SelectedIndex="@selectedIndex" class="example-tabs">
        <Tabs>
            <RadzenTabsItem Text="Example" Title="Example preview">
                @(dynamicContent ?? ChildContent)
            </RadzenTabsItem>
            <RadzenTabsItem Text="Edit Source" Title="View and edit example source code">
                <div class="source-tab-item rz-pb-2">
                    <CodeViewer ComponentName="@ComponentName" @bind-Value="@source" ComponentSource="@Source" PageName="@ExampleUrl" Compiled="OnCompiled" />
                </div>
            </RadzenTabsItem>
            @foreach (var p in AdditionalSourceCodePages)
            {
            <RadzenTabsItem Text="@Path.GetFileName(p)" Icon="code">
                <div class="source-tab-item rz-pb-2">
                    <CodeViewer PageName="@p" ReadOnly="true" />
                </div>
            </RadzenTabsItem>
            }
        </Tabs>
    </RadzenTabs>
</RadzenCard>

@code {

    private int selectedIndex = 0;
}

@code {

    private RenderFragment dynamicContent;
}

@code {

    [Parameter]
    public RenderFragment ChildContent { get; set; }

    [Parameter]
    public string Name { get; set; }

    [Parameter]
    public string ComponentName { get; set; }

    [Parameter]
    public string Source { get; set; }

    [Parameter]
    public string Example { get; set; }

    [Parameter]
    public string DocumentationLink { get; set; }

    [Parameter]
    public bool Heading { get; set; } = false;

    [Parameter]
    public bool Documentation { get; set; } = false;

    string DocumentationHref => Documentation ? DocumentationLink ?? $"/docs/guides/components/{Name?.ToLower()}.html" : "";

    string ComponentSourceHref => Source ?? $"https://github.com/radzenhq/radzen-blazor/blob/master/Radzen.Blazor/Radzen{Name}.razor.cs";

    [Parameter]
    public IEnumerable<string> AdditionalSourceCodePages { get; set; } = Enumerable.Empty<string>();

    private string ExampleUrl => string.IsNullOrEmpty(Example) ? $"{Name}Page.razor" : $"{Example}.razor";

    Example example;

    string CanonicalUrl => new Uri(NavigationManager.Uri).GetLeftPart(UriPartial.Path);

    DateTime start;

    string source ;

    protected override void OnInitialized()
    {
        start = DateTime.Now;

        example = ExampleService.FindCurrent(NavigationManager.ToAbsoluteUri(NavigationManager.Uri));
    }

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        await base.OnAfterRenderAsync(firstRender);

        if (firstRender)
        {
            await JSRuntime.InvokeVoidAsync("console.log", $"Page {Name} render time is {(DateTime.Now - start).TotalMilliseconds} ms");
        }
    }

    void OnCompiled(Type type)
    {
        dynamicContent = builder =>
        {
            builder.OpenComponent(0, type);
            builder.CloseComponent();
        };

        selectedIndex = 0;
    }

    public void Dispose()
    {
        GC.Collect();
    }
}