@page "/login"
@* Virtual route to trick blazor *@

@using Moonlight.Core.Models.Abstractions
@using Moonlight.Core.Models.Forms
@using Moonlight.Core.Services
@using MoonCore.Exceptions
@using MoonCoreUI.Services

@inject IAuthenticationProvider AuthenticationProvider
@inject IdentityService IdentityService
@inject CookieService CookieService
@inject NavigationManager Navigation

<div class="d-flex justify-content-center">
    <div class="d-flex align-items-center">
        <div class="card card-body m-15 p-15">
            <div class="text-center mb-8">
                <div class="fw-bold mb-3 fs-1">
                    Login
                </div>
                <div class="text-gray-400 fw-semibold fs-4">
                    Login in order to start managing your services
                </div>
            </div>

            <SmartForm Model="Form" OnValidSubmit="OnValidSubmit">
                @if (RequiresTwoFactor)
                {
                    <div class="fv-row mb-7">
                        <input @bind="Form.TwoFactorCode" type="number" placeholder="Enter your two factor code" class="form-control form-control-solid">
                    </div>
                }
                else
                {
                    <div class="fv-row mb-8">
                        <input @bind="Form.Email" type="text" placeholder="Email" class="form-control form-control-solid">
                    </div>

                    <div class="fv-row mb-7">
                        <input @bind="Form.Password" type="password" placeholder="Password" class="form-control form-control-solid">
                    </div>
                }
                
                <div class="d-flex flex-stack flex-wrap gap-3 fs-base fw-semibold mb-10">
                    <a href="/password-reset" class="link-primary">
                        Forgot Password ?
                    </a>
                    <a href="/register" class="link-primary">
                        Need an account ?
                    </a>
                </div>

                <div class="d-flex flex-stack">
                    <button type="submit" class="btn btn-primary me-2 flex-shrink-0">Sign In</button>
                    <div class="d-flex align-items-center">
                        <div class="text-gray-400 fw-semibold fs-6 me-3 me-md-6">Or</div>
                        @* OAuth2 Providers here *@
                    </div>
                </div>
            </SmartForm>
        </div>
    </div>
</div>

@code
{
    private LoginForm Form = new();
    private bool RequiresTwoFactor = false;

    private async Task OnValidSubmit()
    {
        // This code is responsible for handling the need of an two factor code and for showing and hiding the responsible field
        var currentTwoFactorRequired = await AuthenticationProvider.RequiresTwoFactorCode(Form.Email, Form.Password);

        if (RequiresTwoFactor != currentTwoFactorRequired && currentTwoFactorRequired)
        {
            RequiresTwoFactor = currentTwoFactorRequired;
            
            await InvokeAsync(StateHasChanged);
            return;
        }
        
        RequiresTwoFactor = currentTwoFactorRequired;
        
        //
        var user = await AuthenticationProvider.Authenticate(Form.Email, Form.Password, Form.TwoFactorCode);

        if (user == null)
            throw new DisplayException("A user with these credential combination was not found");

        // Generate token and authenticate
        var token = await IdentityService.Authenticate(user);

        // Save token for later use
        await CookieService.SetValue("token", token);
        
        // Forward the user if not on the specific page
        if(new Uri(Navigation.Uri).LocalPath.StartsWith("/login"))
            Navigation.NavigateTo("/");
    }
}