@page "/formulaone"
@using AyCode.Interfaces.StorageHandlers;
@using BlazorAnimation
@using Newtonsoft.Json;
@using TIAM.Entities.Transfers
@using TIAMSharedUI.Shared.Components
@using TIAMWebApp.Shared.Application.Interfaces
@using TIAMWebApp.Shared.Application.Models.ClientSide;
@using AyCode.Blazor.Components;
@using TIAMWebApp.Shared.Application.Models;
@using TIAMWebApp.Shared.Application.Models.ClientSide.UI
@using TIAMWebApp.Shared.Application.Models.PageModels
@using TIAMWebApp.Shared.Application.Utility;
@using System.IdentityModel.Tokens.Jwt;
@using TIAMSharedUI.Pages.Components;
@using TIAMSharedUI.Shared
@using TIAMWebApp.Shared.Application.Models.ClientSide.UI.WizardModels
@inject NavigationManager NavManager
@inject IUserDataService UserDataService;
@inject IJSRuntime jsRuntime;
@inject ISecureStorageHandler SecureStorageHandler
@inject ISessionService sessionService;
@inject IStringLocalizer<MyResources> localizer;
@inject NavigationManager navManager
@inject LogToBrowserConsole logToBrowserConsole

@inject ITransferDataService transferDataService

<PageTitle>Index</PageTitle>

<AuthComponent />

<HeroSlider SliderItems="@sliders" PBottom="200px" Height="70vh"></HeroSlider>
<div class="container" style="position: relative; z-index: 2;">
    <div class="row d-flex justify-content-center">
        <div class="col-12">

            @{
                if (!showWizard)
                {

                    <DxTabs>

                        <DxTabPage CssClass="text-white" Text="From Budapest">
                            <div class="row d-flex justify-content-center p-5 text-white">
                                <h2>BUDAPEST - HUNGARORING</h2>

                                <div class="col-lg-3 col-12">
                                    <div class="card bg-gradient p-3 my-3">
                                        <div class="card-header">

                                            <h5>Early bird sale</h5>

                                        </div>
                                        <div class="card-body text-center">
                                            <h3>Friday</h3>
                                            <h4>round trip</h4>
                                            <img src="_content/TIAMSharedUI/images/f1_b.png" class="img-fluid py-3" />
                                            <span>Budapest </span>  <i class="fa-solid fa-right-left"></i> <span>Hungaroring</span>
                                        </div>
                                        <div class="card-footer">
                                            <div class="row">
                                                <div class="col-6">
                                                    <p>07.19.2024</p>
                                                </div>
                                                <div class="col-6">
                                                    <DxButton CssClass="float-end" Click="() => Book(new int[1] {19}, true, _location1)">Book now</DxButton>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                                <div class="col-lg-3 col-12">
                                    <div class="card bg-gradient p-3 my-3">
                                        <div class="card-header">

                                            <h5>Early bird sale</h5>

                                        </div>
                                        <div class="card-body text-center">
                                            <h3>Saturday</h3>
                                            <h4>round trip</h4>
                                            <img src="_content/TIAMSharedUI/images/f1_b.png" class="img-fluid py-3" />
                                            <span>Budapest </span>  <i class="fa-solid fa-right-left"></i> <span>Hungaroring</span>
                                        </div>
                                        <div class="card-footer">
                                            <div class="row">
                                                <div class="col-6">
                                                    <p>07.20.2024</p>
                                                </div>
                                                <div class="col-6">
                                                    <DxButton CssClass="float-end" Click="() => Book(new int[1] {20}, true, _location1)">Book now</DxButton>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                                <div class="col-lg-3 col-12">
                                    <div class="card bg-gradient p-3 my-3">
                                        <div class="card-header">

                                            <h5>Early bird sale</h5>

                                        </div>
                                        <div class="card-body text-center">
                                            <h3>Sunday</h3>
                                            <h4>round trip</h4>
                                            <img src="_content/TIAMSharedUI/images/f1_b.png" class="img-fluid py-3" />
                                            <span>Budapest </span>  <i class="fa-solid fa-right-left"></i> <span>Hungaroring</span>
                                        </div>
                                        <div class="card-footer">
                                            <div class="row">
                                                <div class="col-6">
                                                    <p>07.21.2024</p>
                                                </div>
                                                <div class="col-6">
                                                    <DxButton CssClass="float-end" Click="() => Book(new int[1] {21}, true, _location1)">Book now</DxButton>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                                <div class="col-lg-3 col-12">
                                    <div class="card bg-gradient p-3 my-3">
                                        <div class="card-header">

                                            <h5>Early bird sale</h5>

                                        </div>
                                        <div class="card-body text-center">
                                            <h3>3 days</h3>
                                            <h4>round trip</h4>
                                            <img src="_content/TIAMSharedUI/images/f1_b.png" class="img-fluid py-3" />
                                            <span>Budapest </span>  <i class="fa-solid fa-right-left"></i> <span>Hungaroring</span>
                                        </div>
                                        <div class="card-footer">
                                            <div class="row">
                                                <div class="col-6">
                                                    <p>07.19-21.2024</p>
                                                </div>
                                                <div class="col-6">
                                                    <DxButton CssClass="float-end" Click="() => Book(new int[3] {19,20,21}, true, _location1)">Book now</DxButton>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </DxTabPage>
                        <DxTabPage CssClass="text-white" Text="Liszt Ferenc Airport">
                            <div class="row d-flex justify-content-center p-5 text-white">
                                <h2>AIRPORT - HUNGARORING</h2>

                                <div class="col-lg-3 col-12">
                                    <div class="card bg-white p-3 my-3">
                                        <div class="card-body">
                                            <h3>One way transfer</h3>
                                        </div>
                                        <div class="card-footer">
                                            <div class="row">
                                                <div class="col-5">
                                                    <p>07.19.2024</p>
                                                </div>
                                                <div class="col-7">
                                                    <DxButton CssClass="float-end" Click="() => Book(new int[1] {19}, false, _location2)">Book now</DxButton>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                                <div class="col-lg-3 col-12">
                                    <div class="card bg-white p-3 my-3">
                                        <div class="card-body">
                                            <h3>One way transfer</h3>
                                        </div>
                                        <div class="card-footer">
                                            <div class="row">
                                                <div class="col-5">
                                                    <p>07.20.2024</p>
                                                </div>
                                                <div class="col-7">
                                                    <DxButton CssClass="float-end" Click="() => Book(new int[1] {20}, false, _location2)">Book now</DxButton>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                                <div class="col-lg-3 col-12">
                                    <div class="card bg-white p-3 my-3">
                                        <div class="card-body">
                                            <h3>One way transfer</h3>
                                        </div>
                                        <div class="card-footer">
                                            <div class="row">
                                                <div class="col-5">
                                                    <p>07.21.2024</p>
                                                </div>
                                                <div class="col-7">
                                                    <DxButton CssClass="float-end" Click="() => Book(new int[1] {21}, false, _location2)">Book now</DxButton>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>

                            </div>
                        </DxTabPage>
                        <DxTabPage CssClass="text-white" Text="Other than Budapest">
                            <div class="row d-flex justify-content-center p-5 text-white">
                                <h2>FROM LOCATION OUTSIDE OF BUDAPEST</h2>

                                <div class="col-lg-3 col-12">
                                    <div class="card bg-white p-3 my-3">
                                        <div class="card-body">
                                            <h3>Friday round trip</h3>
                                            <i class="fa-solid fa-right-left"></i>
                                        </div>
                                        <div class="card-footer">
                                            <div class="row">
                                                <div class="col-5">
                                                    <p>07.19.2024</p>
                                                </div>
                                                <div class="col-7">
                                                    <DxButton CssClass="float-end" Click="() => Book(new int[1] {19}, true, _location3)">Book now</DxButton>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                                <div class="col-lg-3 col-12">
                                    <div class="card bg-white p-3 my-3">
                                        <div class="card-body">
                                            <h3>Saturday round trip</h3>
                                            <i class="fa-solid fa-right-left"></i>
                                        </div>
                                        <div class="card-footer">
                                            <div class="row">
                                                <div class="col-5">
                                                    <p>07.20.2024</p>
                                                </div>
                                                <div class="col-7">
                                                    <DxButton CssClass="float-end" Click="() => Book(new int[1] {20}, true, _location3)">Book now</DxButton>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                                <div class="col-lg-3 col-12">
                                    <div class="card bg-white p-3 my-3">
                                        <div class="card-body">
                                            <h3>Sunday round trip</h3>
                                            <i class="fa-solid fa-right-left"></i>
                                        </div>
                                        <div class="card-footer">
                                            <div class="row">
                                                <div class="col-5">
                                                    <p>07.21.2024</p>
                                                </div>
                                                <div class="col-7">
                                                    <DxButton CssClass="float-end" Click="() => Book(new int[1] {21}, true, _location3)">Book now</DxButton>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                                <div class="col-lg-3 col-12">
                                    <div class="card bg-white p-3 my-3">
                                        <div class="card-body">
                                            <h3>3 days round trip bundle</h3>
                                            <i class="fa-solid fa-right-left"></i>
                                        </div>
                                        <div class="card-footer">
                                            <div class="row">
                                                <div class="col-5">
                                                    <p>07.19-21.2024</p>
                                                </div>
                                                <div class="col-7">
                                                    <DxButton CssClass="float-end" Click="() => Book(new int[3] {19,20,21}, true, _location3)">Book now</DxButton>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </DxTabPage>


                    </DxTabs>

                    //six cards for the six available services



                }
                else
                {
                    <div class="row">
                        <div class="col-sm-6 col-12">
                        </div>
                        <div class="col-sm-6 col-12">

                            <div class="card bg-white p-3">
                                <div class="card-header">
                                    <div class="row">
                                        <div class="col-9">
                                            <h5>Early bird sale</h5>

                                        </div>
                                        <div class="col-3">
                                            <DxButton CssClass="float-end" Click="() => showWizard = false">
                                                <i class="fa-solid fa-times"></i>
                                            </DxButton>
                                        </div>
                                    </div>

                                </div>
                                <div class="card-body">
                                    <InputWizard Data=@myModel
                                                 OnSubmit="SubmitForm"
                                                 IgnoreReflection="@TransferIgnorList"
                                                 SubmitButtonText="ButtonSend"
                                                 TitleResourceString="TransferTitle"
                                                 SubtitleResourceString="TransferSubtitle"
                                                 CssClass="text-white"></InputWizard>
                                </div>



                            </div>
                        </div>
                    </div>

                }
            }

        </div>
    </div>

</div>
<CallToActionComponent />

@code {

    [CascadingParameter]
    private PopupMessageBox PopupMessageBox { get; set; } = default!;

    public bool showWizard = false;

    public string _location1 = "Budapest";
    public string _location2 = "Airport";
    public string _location3 = "Other";

    public int[] OrderDates;
    public bool OrderIsReturn;
    public string OrderLocation;


    public List<HeroSliderItem> sliders = new List<HeroSliderItem>
    {
    new HeroSliderItem
    {
    Title = "Welcome to TIAM",
    ImageUrl = "https://cdn.discordapp.com/attachments/1009417752978661446/1223738909545201734/adam_the_walking_dad_on_the_racetrack_in_epic_scene_with_sunset_949f618b-40f6-460c-80c8-b1acb1dd609e.png?ex=661af2ac&is=66087dac&hm=a65227652da3b9c165b197f58d031cbdbbea392290a1bca53fc8c768d732b53e&"
    },
    new HeroSliderItem
    {
    Title = "Welcome to TIAM",
    ImageUrl = "https://cdn.discordapp.com/attachments/1009417752978661446/1223720528544792687/adam_the_walking_dad_a_formula_1_race_track_with_a_formula_1_ra_f9ca52cb-6e1a-4458-929d-99d08bce7c1f.png?ex=661ae18d&is=66086c8d&hm=aa9ef7d602f1f9e42884469b0c556886540400c0a46131d9b06e855e85523140&"
    },
new HeroSliderItem
    {
    Title = "Welcome to TIAM",
    ImageUrl = "https://cdn.discordapp.com/attachments/1009417752978661446/1223734873504616580/adam_the_walking_dad_a_formula_1_race_track_with_a_formula_1_ra_09b9f360-9660-41bc-840a-5b0b7a189ef7.png?ex=661aeee9&is=660879e9&hm=6e07a2da7d6de8767f56b4d62e5a268975b62d417e96cd5ed723dcb88f299d65&"
    },
    };

    public TransferWizardModel myModel = new TransferWizardModel();

    public List<string> TransferIgnorList = new List<string>
    {
    "Id",
    "UserId",
    "Destination",
    "PickupAddress",
    "ProductId",
    "TripDate",
    "UserProductMappingId",
    "UserProductToCarId",
    "ReferralId",
    "Price",
    "Comment"
    };

    /*protected override void OnAfterRender(bool isFirst)
    {
    message = " Target destination is " + slider.SliderElementId.ToString();
    }*/

    public async Task SubmitForm(object result)
    {
        var orderModel = result as TransferWizardModel;
     
        //check if user exists
        if (sessionService.IsAuthenticated && sessionService.User != null )
        {
            //Basic settings
            orderModel.UserId = sessionService.User.UserModelDto.Id;
            orderModel.ProductId = sessionService.User.UserModelDto.Products.FirstOrDefault()?.Id;
        }
        else
        {
            //cherck if user exists
            var user = await UserDataService.GetUserByEmailAsync(orderModel.EmailAddress);

            if (user.Id == Guid.Empty)
                user = null;

            if (user != null)
            {
                orderModel.UserId = user.Id;
                
                orderModel.ProductId = user.Products.FirstOrDefault()?.Id;
            }
            else
            {
                //if not, create user

                Random random = new Random();
                string chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
                string password = new string(Enumerable.Repeat(chars, 10)
                  .Select(s => s[random.Next(s.Length)]).ToArray());

                RegistrationModel regModel = new RegistrationModel
                    {
                        PhoneNumber = orderModel.PhoneNumber,
                        Email = orderModel.EmailAddress,
                        Password = password,
                        ReferralId = null
                    };

                var bleh = await UserDataService.CreateGuestUser(regModel);
                orderModel.UserId = bleh.user.Id;

            }
        }

        await ProcessTransfers(orderModel);
    }

    protected override void OnInitialized()
    {
        bool IsLoggedIn = sessionService.IsAuthenticated;
        if (IsLoggedIn)
        {

            logToBrowserConsole.LogToBC("logged in" + sessionService.User.UserModelDto.Products.FirstOrDefault().Id);

        }
    }

    public void Book(int[] dates, bool isReturn, string location)
    {
        myModel.TripDate = new DateTime(2024, 07, dates[0]);
        myModel.Destination = "Hungaroring";
        myModel.PickupAddress = location;
        OrderDates = dates;
        OrderIsReturn = isReturn;
        OrderLocation = location;
        showWizard = true;
    }

    public async Task<List<Transfer>> ProcessTransfers(TransferWizardModel orderModel)
    {
        List<TransferWizardModel> transferList = new List<TransferWizardModel>();
        foreach (var date in OrderDates)
        {

            TransferWizardModel transfer = orderModel.Clone();
            transfer.TripDate = new DateTime(2024, 07, date);


            // Outbound trip
            transfer.PickupAddress = OrderLocation;
            transfer.Destination = "Hungaroring";
            transferList.Add(transfer);

            // Return trip
            transfer = orderModel.Clone();
            transfer.TripDate = new DateTime(2024, 07, date);
            transfer.PickupAddress = "Hungaroring";
            transfer.Destination = OrderLocation;
            transfer.PhoneNumber = orderModel.PhoneNumber;
            transfer.EmailAddress = orderModel.EmailAddress;
            transferList.Add(transfer);
        }

        var transferResult = await transferDataService.CreateTransfers(transferList);
        logToBrowserConsole.LogToBC($"Submitted nested form: {transferResult.GetType().FullName}, {transferResult.Count}");
        return transferResult;
    }
}





