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.SysAdmins
{
    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 = "Destination name";
                newDestination.Description = "Type some description here";
                newDestination.AddressString = "The address of the destination";                

            }
        }

        void Grid_CustomizeElement(GridCustomizeElementEventArgs e)
        {
            
        }

        async Task Grid_EditModelSaving(GridEditModelSavingEventArgs e)
        {
            if (e.IsNew)
            {
                //add new row to grid
                myModel = (TransferDestinationWizardModel)e.EditModel;
                //add new orderData to orderData array

                //await NwindDataService.InsertEmployeeAsync((EditableEmployee)e.EditModel);
                await transferDataService.CreateTransferDestination(TransferDestinationWizardModel.SaveToTransferDestination(myModel));
                //await transferDataService.CreateTransferDestination(new TransferDestination
                /*{
                    Id = Guid.NewGuid(),
                    Name = myModel.Name,                    
                    Description = myModel.Description,
                    AddressString = myModel.AddressString,
                    Price = myModel.Price,
                    PriceType = myModel.PriceType
                });*/
                logToBrowserConsole.LogToBC("New orderData added");

                //add mymodel to transferData array
                TransferDataFromDb = ((TransferDestinationWizardModel[])TransferDataFromDb).Append(myModel).ToArray();
            }
            else
            {
                    logToBrowserConsole.LogToBC("orderData updated");
                //modify transferData where transferData.Id == e.EditModel.Id
                //get transfer from TransferData by Id

                var abg = ((TransferDestinationWizardModel[])TransferDataFromDb).Length;

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

                    if (transferToModify.Id == myModel.Id)
                    {
                       
                        transferToModify.Id = myModel.Id;
                        transferToModify.Name = myModel.Name;  
                        transferToModify.Description = myModel.Description;                        
                        transferToModify.AddressString = myModel.AddressString;
                        transferToModify.Price = myModel.Price;
                        transferToModify.PriceType = myModel.PriceType;

                        await transferDataService.UpdateTransferDestination(TransferDestinationWizardModel.SaveToTransferDestination(transferToModify));
                    }
                }

            }

            //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();
            await transferDataService.GetDestinationsAsync();
            //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(item.Id, item.Name, item.Description, item.AddressString, item.Price, item.PriceType)).ToArray();
                logToBrowserConsole.LogToBC($"TransferDataFromDb: {item.Name}");
                
            }
            logToBrowserConsole.LogToBC($"TransferDataFromDb: {((TransferDestinationWizardModel[])TransferDataFromDb).Length}");
        }

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

    }
}
