@page "/register"
@using TIAMWebApp.Shared.Application.Interfaces;
@using TIAMWebApp.Shared.Application.Models.PageModels;
@using TIAMSharedUI.Pages.Components;
@inject NavigationManager navManager
@inject IUserDataService UserDataservice
@inject IJSRuntime jsRuntime
<PageTitle>Register</PageTitle>

<div class="wrapper">
    <div class="my-logo">
        <img src="_content/TIAMSharedUI/images/png-logo-0.png" alt="">
    </div>
    <div class="text-center mt-4 name">
        Let's create your account!
    </div>
    <form class="p-3 mt-3">
        <div>
            @switch (currentStep)
            {
                case 1:
                    <Step1 @bind-RegModel="regModel" onNext="GoToNextStep" />
                    ;
                    break;
                case 2:
                    <Step2 @bind-RegModel="regModel" onNext="GoToNextStep" onPrev="GoToPreviousStep" />
                    ;
                    break;
                case 3:
                    <Step3 @bind-RegModel="regModel" onSubmit="SubmitRegistration" onPrev="GoToPreviousStep" />
                    ;
                    break;
            }
        </div>


    </form>
    @{
        if (!registered)
        {

            <div>
                <p>@regModel.Email</p>
                <p>@regModel.PhoneNumber</p>
                <p>@regModel.Password</p>
            </div>
        }
    }
    <div class="text-center fs-6">
        Already have an account? <a href="login">Sign in here!</a>
    </div>
</div>

@code {

    RegistrationModel regModel = new();
    /*IEnumerable<char> PredefinedPlaceholders = new List<char>() { '_', '#' };
    string Telephone { get; set; } = "5625595830";
    char Placeholder { get; set; } = '_';
    bool SaveLiterals { get; set; } = true;*/    

    private int currentStep = 1;



    bool registered = false;

    private void GoToNextStep()
    {
        currentStep++;
    }

    private void GoToPreviousStep()
    {
        currentStep--;
    }

    private async Task SubmitRegistration()
    {

        // Implement your registration logic here
        // You can use Email, PhoneNumber, and Password variables
        // Reset currentStep after successful registration
        registered = true;
        currentStep = 1;
        var response = await UserDataservice.CreateUser(regModel);

        LogToBrowserConsole("Registration started: " + "Email: " + regModel.Email + "PhoneNumber: " + regModel.PhoneNumber + "Password: " + regModel.Password);

        
            if (response.isSuccess)
            {
                //await App.Current.MainPage.DisplayAlert("Error", "Invalid credentials", "Ok");
                //display error message via jsinterop
                LogToBrowserConsole(response.ErrorMessage);
                
            }
            else if (!response.isSuccess)
            {
                //await App.Current.MainPage.DisplayAlert("Success", "Successful login", "Ok");
                //display success message via jsinterop
                LogToBrowserConsole(response.ErrorMessage);
                
            }
            else
            {
                //await App.Current.MainPage.DisplayAlert("Error", "An error occured while trying to login", "Ok");
                //display error message via jsinterop
                LogToBrowserConsole(response.ErrorMessage);
                
            }           
    }

    public void LogToBrowserConsole(string message)
    {
        jsRuntime.InvokeVoidAsync("console.log", message);
    }


}