@page "/register"
@using BlazorAnimation
@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="text-center m-5">
    <h1>Register</h1>
    <h2 style="font-size:small">Welcome to TourIam!</h2>
</div>



<div class="container mt-3">
    <div class="row d-flex justify-content-center align-items-center h-100">
        <div class="col-12 col-sm-6">
            <Animation Effect="@Effect.FadeInUp" Speed="@Speed.Fast" Delay="@TimeSpan.FromMilliseconds(250)">
                <div class="card glass inputwizardwrapper my-5">
                    <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>
                </div>
            </Animation>
        </div>
    </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);
    }


}