@page "/credentials/{id:guid}"
@inherits MainBase
@inject CredentialService CredentialService

<LayoutPageTitle>View credentials</LayoutPageTitle>

@if (IsLoading || Alias == null)
{
    <LoadingIndicator />
}
else
{
    <PageHeader
        BreadcrumbItems="@BreadcrumbItems"
        Title="View credentials entry">
        <CustomActions>
            <LinkButton
                Text="Edit"
                AdditionalText="credentials entry"
                Href="@($"/credentials/{Id}/edit")"
                Color="primary" />
            <LinkButton
                Text="Delete"
                AdditionalText="credentials entry"
                Href="@($"/credentials/{Id}/delete")"
                Color="danger" />
        </CustomActions>
    </PageHeader>

    <div class="grid grid-cols-2 px-4 pt-6 md:grid-cols-3 lg:gap-4 dark:bg-gray-900">
        <div class="col-span-full lg:col-auto">
            <div class="p-4 mb-4 bg-white border border-gray-200 rounded-lg shadow-sm 2xl:col-span-2 dark:border-gray-700 sm:p-6 dark:bg-gray-800">
                <div class="items-center flex space-x-4">
                    <DisplayFavicon FaviconBytes="@Alias.Service.Logo" />

                    <div>
                        <h3 class="mb-1 text-xl font-bold text-gray-900 dark:text-white">@Alias.Service.Name</h3>
                        @if (Alias.Service.Url is not null && Alias.Service.Url.Length > 0)
                        {
                            <a href="@Alias.Service.Url" target="_blank" class="text-blue-500 dark:text-blue-400">@Alias.Service.Url</a>
                        }
                    </div>
                </div>
            </div>
            <RecentEmails EmailAddress="@Alias.Alias.Email" />
            @if (Alias.Notes != null && Alias.Notes.Length > 0)
            {
                <FormattedNote Notes="@Alias.Notes" />
            }

            @if (Alias.Attachments.Count > 0)
            {
                <AttachmentViewer Attachments="@Alias.Attachments" />
            }
        </div>
        <div class="col-span-2">
            <div class="p-4 mb-4 bg-white border border-gray-200 rounded-lg shadow-sm 2xl:col-span-2 dark:border-gray-700 sm:p-6 dark:bg-gray-800">
                <h3 class="mb-2 text-xl font-semibold dark:text-white">Login credentials</h3>
                <p class="mb-4 text-sm text-gray-600 dark:text-gray-400">
                    Use the generated credentials below to create your account. Any emails sent to the shown address will automatically appear on this page.
                </p>
                <form action="#">
                    <div class="grid gap-6">
                        <div class="col-span-6 sm:col-span-3">
                            <CopyPasteFormRow Id="email" Label="Email" Value="@Alias.Alias.Email"></CopyPasteFormRow>
                        </div>
                        <div class="col-span-6 sm:col-span-3">
                            <CopyPasteFormRow Id="username" Label="Username" Value="@(Alias.Username)"></CopyPasteFormRow>
                        </div>
                        <div class="col-span-6 sm:col-span-3">
                            <CopyPastePasswordFormRow Id="password" Label="Password" Value="@(Alias.Passwords.FirstOrDefault()?.Value ?? string.Empty)"></CopyPastePasswordFormRow>
                        </div>
                    </div>
                </form>
            </div>
            <div class="p-4 mb-4 bg-white border border-gray-200 rounded-lg shadow-sm 2xl:col-span-2 dark:border-gray-700 sm:p-6 dark:bg-gray-800">
                <h3 class="mb-4 text-xl font-semibold dark:text-white">Alias</h3>
                <form action="#">
                    <div class="grid grid-cols-6 gap-6">
                        <div class="col-span-6">
                            <CopyPasteFormRow Label="Full name" Value="@(Alias.Alias.FirstName + " " + Alias.Alias.LastName)"></CopyPasteFormRow>
                        </div>
                        <div class="col-span-6 sm:col-span-3">
                            <CopyPasteFormRow Label="First name" Value="@(Alias.Alias.FirstName)"></CopyPasteFormRow>
                        </div>
                        <div class="col-span-6 sm:col-span-3">
                            <CopyPasteFormRow Label="Last name" Value="@(Alias.Alias.LastName)"></CopyPasteFormRow>
                        </div>
                        <div class="col-span-6 sm:col-span-3">
                            <CopyPasteFormRow Label="Birthdate" Value="@(Alias.Alias.BirthDate.ToString("yyyy-MM-dd"))"></CopyPasteFormRow>
                        </div>
                        <div class="col-span-6 sm:col-span-3">
                            <CopyPasteFormRow Label="Nickname" Value="@(Alias.Alias.NickName)"></CopyPasteFormRow>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
}

@code {
    /// <summary>
    /// Gets or sets the credentials ID.
    /// </summary>
    [Parameter]
    public Guid Id { get; set; }
    private bool IsLoading { get; set; } = true;
    private Credential? Alias { get; set; } = new();

    /// <inheritdoc />
    protected override async Task OnInitializedAsync()
    {
        await base.OnInitializedAsync();
        BreadcrumbItems.Add(new BreadcrumbItem { DisplayName = "View credentials entry" });
    }

    /// <inheritdoc />
    protected override async Task OnParametersSetAsync()
    {
        await base.OnParametersSetAsync();
        await LoadEntryAsync();
    }

    /// <summary>
    /// Loads the credentials entry.
    /// </summary>
    private async Task LoadEntryAsync()
    {
        IsLoading = true;
        StateHasChanged();

        // Load the aliases from the webapi via AliasService.
        Alias = await CredentialService.LoadEntryAsync(Id);

        if (Alias is null)
        {
            // Error loading alias.
            GlobalNotificationService.AddErrorMessage("This credentials entry does not exist (anymore). Please try again.");
            NavigationManager.NavigateTo("/", false, true);
            return;
        }

        IsLoading = false;
        StateHasChanged();
    }
}
