@using Microsoft.Extensions.Localization
@using AliasVault.Client.Main.Constants
@inject IStringLocalizerFactory LocalizerFactory

@if (!string.IsNullOrEmpty(Password))
{
    <div class="mt-3">
        <div class="flex items-center justify-between mb-2">
            <span class="text-xs font-medium text-gray-700 dark:text-gray-300">@Localizer["PasswordStrength"]</span>
            <span class="text-xs font-semibold @GetStrengthTextColorClass()">@GetStrengthLabel()</span>
        </div>
        <div class="w-full bg-gray-200 dark:bg-gray-700 rounded-full h-2.5 overflow-hidden shadow-inner">
            <div
                class="h-2.5 rounded-full transition-all duration-500 ease-out @GetStrengthColorClass()"
                style="width: @GetStrengthPercentage()%">
            </div>
        </div>
    </div>
}

@code {
    [Parameter]
    public string Password { get; set; } = string.Empty;

    [Parameter]
    public EventCallback<int> OnStrengthChanged { get; set; }

    private int _passwordStrength = 0;

    private IStringLocalizer Localizer => LocalizerFactory.Create("Components.Main.Shared.PasswordStrengthIndicator", "AliasVault.Client");

    protected override void OnParametersSet()
    {
        var previousStrength = _passwordStrength;
        CalculatePasswordStrength();

        if (previousStrength != _passwordStrength && OnStrengthChanged.HasDelegate)
        {
            _ = OnStrengthChanged.InvokeAsync(_passwordStrength);
        }
    }

    private void CalculatePasswordStrength()
    {
        if (string.IsNullOrEmpty(Password))
        {
            _passwordStrength = 0;
            return;
        }

        var length = Password.Length;
        if (length < 8)
        {
            _passwordStrength = 0;
        }
        else if (length < 12)
        {
            _passwordStrength = 1;
        }
        else if (length < 16)
        {
            _passwordStrength = 2;
        }
        else if (length < 20)
        {
            _passwordStrength = 3;
        }
        else
        {
            _passwordStrength = 4;
        }
    }

    private string GetStrengthLabel()
    {
        return _passwordStrength switch
        {
            0 => Localizer["StrengthVeryWeak"],
            1 => Localizer["StrengthWeak"],
            2 => Localizer["StrengthFair"],
            3 => Localizer["StrengthGood"],
            4 => Localizer["StrengthStrong"],
            _ => string.Empty
        };
    }

    private string GetStrengthColorClass()
    {
        return _passwordStrength switch
        {
            0 => "bg-orange-400 dark:bg-orange-500",
            1 => "bg-yellow-500 dark:bg-yellow-600",
            2 => "bg-green-500 dark:bg-green-600",
            3 => "bg-green-600 dark:bg-green-700",
            4 => "bg-green-700 dark:bg-green-800",
            _ => "bg-gray-300 dark:bg-gray-600"
        };
    }

    private string GetStrengthTextColorClass()
    {
        return _passwordStrength switch
        {
            0 => "text-orange-600 dark:text-orange-400",
            1 => "text-yellow-600 dark:text-yellow-400",
            2 => "text-green-600 dark:text-green-400",
            3 => "text-green-700 dark:text-green-300",
            4 => "text-green-800 dark:text-green-200",
            _ => "text-gray-500 dark:text-gray-400"
        };
    }

    private int GetStrengthPercentage()
    {
        return (_passwordStrength + 1) * 20;
    }

    /// <summary>
    /// Gets the current password strength level.
    /// </summary>
    /// <returns>The password strength level (0-4).</returns>
    public int GetPasswordStrength()
    {
        return _passwordStrength;
    }

    /// <summary>
    /// Checks if the current password meets the minimum required strength.
    /// </summary>
    /// <returns>True if the password meets minimum requirements, false otherwise.</returns>
    public bool MeetsMinimumRequirement()
    {
        return _passwordStrength >= PasswordStrengthConstants.MinimumRequiredStrength;
    }
}
