using DevExpress.Blazor;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TIAMWebApp.Shared.Application.Models.ClientSide.UI.WizardModels;
using TIAMWebApp.Shared.Application.Models;
using TIAMWebApp.Shared.Application.Interfaces;
using Microsoft.AspNetCore.Components;
using TIAM.Entities.TransferDestinations;

namespace TIAMSharedUI.Pages.User
{
    public partial class TransferDestinations
    {

        IGrid Grid { get; set; }
        //object? TransferData { get; set; }

        public TransferDestinationWizardModel myModel = new TransferDestinationWizardModel();

        bool PopupVisible { get; set; }
        public List<string> ignoreList = new List<string>
    {
         "ReceiverId"
    };

        public MessageWizardModel messageWizardModel = new MessageWizardModel();

        [Inject]
        public ITransferDataService transferDataService { get; set; }


        object? TransferData = new TransferDestinationWizardModel[]
        {
            new TransferDestinationWizardModel(Guid.NewGuid(), "Liszt Ferenc Airport", "International airport of Budapest", "1185, Budapest, Liszt Ferenc Repülőtér" ),
            new TransferDestinationWizardModel(Guid.NewGuid(), "Buda Castle", "Historical site in the heart of Budapest", "1014 Budapest, Szent György tér 2" ),
            new TransferDestinationWizardModel(Guid.NewGuid(), "Hungarian National Museum", "Historical site in the heart of Budapest", "1088 Budapest, Múzeum krt. 14-16" ),
            new TransferDestinationWizardModel(Guid.NewGuid(), "Parliament of Hungary", "Historical site in the heart of Budapest", "1055 Budapest, Kossuth Lajos tér 1-3" ),
            new TransferDestinationWizardModel(Guid.NewGuid(), "Heroes square", "Historical site in the heart of Budapest", "1146 Budapest, Hősök tere" ),
            new TransferDestinationWizardModel(Guid.NewGuid(), "Gellert Hill", "Historical site in the heart of Budapest", "1118 Budapest, Gellérthegy" ),
            new TransferDestinationWizardModel(Guid.NewGuid(), "Margaret Island", "Historical site in the heart of Budapest", "1138 Budapest, Margitsziget" ),
        };

        object? TransferDataFromDb = new TransferDestinationWizardModel[] { };

        void CancelCreateClick()
        {


            PopupVisible = false;
        }
        void EulaPopupClosed()
        {
            //cancel clicked

        }
        void EulaPopupClosing(PopupClosingEventArgs args)
        {
            //myModel = new TransferWizardModel();
            messageWizardModel = new MessageWizardModel();
        }

        //-----------------------------------------------------------------------------------


        public async Task SubmitForm(object Result)
        {
            //await WizardProcessor.ProcessWizardAsync(Result.GetType(), Result);
            logToBrowserConsole.LogToBC($"Submitted nested form: {Result.GetType().FullName}");
        }


        protected override async Task OnAfterRenderAsync(bool firstRender)
        {
            //if (firstRender)
            //    await Grid.StartEditRowAsync(0);
        }

        void Grid_CustomizeEditModel(GridCustomizeEditModelEventArgs e)
        {
            if (e.IsNew)
            {
                var newDestination = (TransferDestinationWizardModel)e.EditModel;
                newDestination.Id = Guid.NewGuid().ToString();
                newDestination.Name = "ghjgkg hkgh ghjkghgkjgh";
                newDestination.Description = "ghjgkg hkgh ghjkghgkjgh";
                newDestination.AddressString = "ghjgkg hkgh ghjkghgkjgh";
                

            }
        }

        void Grid_CustomizeElement(GridCustomizeElementEventArgs e)
        {
            
        }

        async Task Grid_EditModelSaving(GridEditModelSavingEventArgs e)
        {
            if (e.IsNew)
            {
                //add new row to grid
                myModel = (TransferDestinationWizardModel)e.EditModel;
                //add mymodel to transferData array
                TransferData = ((TransferDestinationWizardModel[])TransferData).Append(myModel);

                //add new orderData to orderData array
                logToBrowserConsole.LogToBC("New orderData added");
            //await NwindDataService.InsertEmployeeAsync((EditableEmployee)e.EditModel);

            }
            else
            {
                    logToBrowserConsole.LogToBC("orderData updated");
                //modify transferData where transferData.Id == e.EditModel.Id
                //get transfer from TransferData by Id

                foreach (var transferToModify in (TransferDestinationWizardModel[])TransferData)
                {
                    myModel = (TransferDestinationWizardModel)e.EditModel;

                    if (transferToModify.Id == myModel.Id)
                    {
                        myModel = (TransferDestinationWizardModel)e.EditModel;
                        transferToModify.Name = myModel.Name;  
                        transferToModify.Description = myModel.Description;                        
                        transferToModify.AddressString = myModel.AddressString;
                        transferToModify.Price = myModel.Price;
                    }
                }

            }

            //await NwindDataService.UpdateEmployeeAsync((EditableEmployee)e.DataItem, (EditableEmployee)e.EditModel);

            await UpdateDataAsync();
        }
        async Task Grid_DataItemDeleting(GridDataItemDeletingEventArgs e)
        {
            //await NwindDataService.RemoveEmployeeAsync((EditableEmployee)e.DataItem);
            //remove orderData from orderData array
            logToBrowserConsole.LogToBC("orderData deleted");
            //await UpdateDataAsync();
        }
        async Task UpdateDataAsync()
        {
            //DataSource = await NwindDataService.GetEmployeesEditableAsync();
            //refresh grid
            logToBrowserConsole.LogToBC("orderData grid refreshed");
        }

        protected override async Task OnInitializedAsync()
        {
            base.OnInitialized();
            var a = await transferDataService.GetDestinationsAsync();
            logToBrowserConsole.LogToBC($"TransferDataFromDb: {((TransferDestinationWizardModel[])TransferDataFromDb).Length}");
            foreach (var item in a)
            {
                //add new transferwizardmodel to transferData array
                TransferDataFromDb = ((TransferDestinationWizardModel[])TransferDataFromDb).Append(
                    new TransferDestinationWizardModel(Guid.NewGuid(), item.Name, item.Description, item.AddressString));
                logToBrowserConsole.LogToBC($"TransferDataFromDb: {item.Name}");
            }
        }

        void ColumnChooserButton_Click()
        {
            Grid.ShowColumnChooser();
        }

    }
}
