@implements IDisposable
@using Microsoft.Extensions.Localization
@using System.Timers
@inject IStringLocalizerFactory LocalizerFactory

<div class="w-full mx-auto">
    @if (_isLoading)
    {
        <div class="absolute inset-0 flex justify-center items-center z-10">
            <div class="animate-spin rounded-full h-16 w-16 border-t-2 border-b-2 border-primary-500"></div>
        </div>
    }
    <div class="@(_isLoading ? "invisible opacity-0" : "opacity-100") transition-opacity duration-300 w-full">
        <div class="bg-white dark:bg-gray-800 rounded-lg shadow-lg lg:shadow-none p-6">
            <p class="text-sm text-gray-600 dark:text-gray-400 mb-4">
                @Localizer["PleaseReadAndAgree"]
            </p>
            <div class="bg-gray-100 dark:bg-gray-700 rounded-lg p-4 mb-8 h-80 overflow-y-auto">
                <h3 class="text-lg font-semibold text-gray-900 dark:text-white mb-2">@Localizer["TermsAndConditionsTitle"]</h3>
                <p class="text-sm text-gray-600 dark:text-gray-400 whitespace-pre-line">
                    @Localizer["TermsContent"]
                </p>
            </div>
            <div class="flex items-center">
                <input type="checkbox" id="agreeTerms" @bind="AgreedToTerms" @bind:after="OnAgreedToTerms" class="mr-2">
                <label for="agreeTerms" class="text-sm font-bold text-gray-600 dark:text-gray-400">
                    @Localizer["AgreementCheckboxLabel"]
                </label>
            </div>
        </div>
    </div>
</div>

@code {
    private IStringLocalizer Localizer => LocalizerFactory.Create("Components.Auth.Setup.TermsAndConditionsStep", "AliasVault.Client");
    /// <summary>
    /// Gets or sets a value indicating whether the user has agreed to the terms and conditions.
    /// </summary>
    [Parameter]
    public bool AgreedToTerms { get; set; }

    /// <summary>
    /// The event callback for when the user has agreed to the terms and conditions.
    /// </summary>
    [Parameter]
    public EventCallback<bool> OnAgreedToTermsChanged { get; set; }

    private bool _isLoading = true;
    private Timer? _loadingTimer;

    /// <inheritdoc />
    public void Dispose()
    {
        _loadingTimer?.Dispose();
    }

    /// <inheritdoc />
    protected override void OnInitialized()
    {
        _loadingTimer = new Timer(300);
        _loadingTimer.Elapsed += (sender, e) => FinishLoading();
        _loadingTimer.AutoReset = false;
        _loadingTimer.Start();
    }

    /// <summary>
    /// Finishes the loading animation.
    /// </summary>
    private void FinishLoading()
    {
        _isLoading = false;
        InvokeAsync(StateHasChanged);
    }

    /// <summary>
    /// Handles the user agreeing to the terms and conditions.
    /// </summary>
    private async Task OnAgreedToTerms()
    {
        await OnAgreedToTermsChanged.InvokeAsync(AgreedToTerms);
    }
}
