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.ServiceProviders;
using TIAM.Core.Enums;
using TIAMWebApp.Shared.Application.Services;

namespace TIAMSharedUI.Pages.User.SysAdmins
{
    public partial class Products :ComponentBase
    {

        IGrid Grid { get; set; }
        object? ProductData { get; set; }

        public ProductWizardModel myModel = new ProductWizardModel();


        public MessageWizardModel messageWizardModel = new MessageWizardModel();

        [Inject]
        public IServiceProviderDataService serviceProviderDataService { get; set; }


        object? ProductArray = new ProductWizardModel[]
        {
            new ProductWizardModel(new TiamServiceProvider(), ProductType.Hotel, "XY hotel", "XYHotel description is here ", 10.0f, ""),
            new ProductWizardModel(new TiamServiceProvider(), ProductType.Hotel, "XY hotel", "XYHotel description is here ", 10.0f, ""),
            new ProductWizardModel(new TiamServiceProvider(), ProductType.Hotel, "XY hotel", "XYHotel description is here ", 10.0f, ""),                      
        };

        object? ProductDataFromDb = new ProductWizardModel[] { };

        void CancelCreateClick()
        {
            
        }
        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 newProduct = (ProductWizardModel)e.EditModel;
                newProduct.Id = Guid.NewGuid();
                newProduct.Name = "ghjgkg hkgh ghjkghgkjgh";
                newProduct.Description = "ghjgkg hkgh ghjkghgkjgh";
                newProduct.ProductType = ProductType.NotDefined;
                

            }
        }

        void Grid_CustomizeElement(GridCustomizeElementEventArgs e)
        {
            
        }

        async Task Grid_EditModelSaving(GridEditModelSavingEventArgs e)
        {
            if (e.IsNew)
            {
                //add new row to grid
                myModel = (ProductWizardModel)e.EditModel;
                //add mymodel to transferData array
                ProductData = ((ProductWizardModel[])ProductData).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 productToModify in (ProductWizardModel[])ProductData)
                {
                    myModel = (ProductWizardModel)e.EditModel;

                    if (productToModify.Id == myModel.Id)
                    {
                        myModel = (ProductWizardModel)e.EditModel;
                        productToModify.ProductType = myModel.ProductType;                        
                        productToModify.Name = myModel.Name;  
                        productToModify.Description = myModel.Description;                        
                        productToModify.Price = myModel.Price;
                        productToModify.JsonDetails = myModel.JsonDetails;

                    }
                }

            }

            //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 serviceProviderDataService.GetProductsForServiceProviderAsync(Guid.Parse("3587F169-683C-4EEE-BCB5-E8D57F8C6DCE"));
            logToBrowserConsole.LogToBC($"ProductDataFromDb: {((ProductWizardModel[])ProductDataFromDb).Length}");
            foreach (var item in a)
            {
                //add new transferwizardmodel to transferData array
                ProductDataFromDb = ((ProductWizardModel[])ProductDataFromDb).Append(
                    new ProductWizardModel(item.ServiceProvider, item.ProductType, item.Name, item.Description, item.Price, item.JsonDetails));
                logToBrowserConsole.LogToBC($"TransferDataFromDb: {item.Name}");
            }
        }

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

    }
}
