@namespace Oqtane.Modules.Admin.Reset
@inherits ModuleBase
@inject NavigationManager NavigationManager
@inject IUserService UserService

<div class="container">
    <div class="form-group">
        <label for="Username" class="control-label">Username: </label>
        <input type="text" class="form-control" placeholder="Username" @bind="@_username" readonly id="Username"/>
    </div>
    <div class="form-group">
        <label for="Password" class="control-label">Password: </label>
        <input type="password" class="form-control" placeholder="Password" @bind="@_password" id="Password"/>
    </div>
    <div class="form-group">
        <label for="Confirm" class="control-label">Confirm Password: </label>
        <input type="password" class="form-control" placeholder="Password" @bind="@_confirm" id="Confirm"/>
    </div>
    <button type="button" class="btn btn-primary" @onclick="Reset">Reset Password</button>
    <button type="button" class="btn btn-secondary" @onclick="Cancel">Cancel</button>
</div>

@code {
    private string _username = string.Empty;
    private string _password = string.Empty;
    private string _confirm = string.Empty;

    public override SecurityAccessLevel SecurityAccessLevel => SecurityAccessLevel.Anonymous;

    protected override void OnInitialized()
    {
        if (PageState.QueryString.ContainsKey("name") && PageState.QueryString.ContainsKey("token"))
        {
            _username = PageState.QueryString["name"];
        }
        else
        { 
            NavigationManager.NavigateTo(NavigateUrl(string.Empty));
        }
    }

    private async Task Reset()
    {
        try
        {
            if (_username != string.Empty && _password != string.Empty && _confirm != string.Empty)
            {
                if (_password == _confirm)
                {
                    var user = new User
                    {
                        SiteId = PageState.Site.SiteId,
                        Username = _username,
                        Password = _password
                    };
                    user = await UserService.ResetPasswordAsync(user, PageState.QueryString["token"]);

                    if (user != null)
                    {
                        await logger.LogInformation("User Password Reset {Username}", _username);
                        NavigationManager.NavigateTo(NavigateUrl("login"));
                    }
                    else
                    {
                        await logger.LogError("Error Resetting User Password {Username}", _username);
                        AddModuleMessage("Error Resetting User Password. Please Ensure Password Meets Complexity Requirements.", MessageType.Error);
                    }
                }
                else
                {
                    AddModuleMessage("Passwords Entered Do Not Match", MessageType.Warning);
                }
            }
            else
            {
                AddModuleMessage("You Must Provide A Username, Password, and Email Address", MessageType.Warning);
            }
        }
        catch (Exception ex)
        {
            await logger.LogError(ex, "Error Resetting User Password {Username} {Error}", _username, ex.Message);
            AddModuleMessage("Error Resetting User Password", MessageType.Error);
        }
    }

    private void Cancel()
    {
        NavigationManager.NavigateTo(NavigateUrl(string.Empty));
    }
}
