using Common;
using Common.Models;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.Messaging;
using MegaRobo.Contract;
using MegaRobo.Entities;
using MegaRobo.WpfInfrastructure.Abstractions;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;

namespace MegaRobo.C00225155App.MenuViewModels
{
    public class EditSampleBottlePropertiesViewModel : ViewModelBaseEx
    {
        private SampleBottleModel sampleBottle = new SampleBottleModel();
        public SampleBottleModel SampleBottle
        {
            get => sampleBottle;
            set
            {
                SetProperty(ref sampleBottle, value);
            }
        }

        private MaterialDoseFunctionFlow _selectedFlow_Dose = null;
        public MaterialDoseFunctionFlow SelectedFlow_Dose
        {
            get => _selectedFlow_Dose;
            set
            {
                SetProperty(ref _selectedFlow_Dose, value);
            }
        }


        private ReactSampleFunctionFlow _selectedsample = null;
        /// <summary>
        /// 选中的取样
        /// </summary>
        public ReactSampleFunctionFlow SelectedSample
        {
            get => _selectedsample;
            set => SetProperty(ref _selectedsample, value);
        }

        private AddLiquidFunctionFlow _selectedSampleAfterAddLiquid = null;
        /// <summary>
        /// 选中的加液液体，取样后紧接着加液
        /// </summary>
        public AddLiquidFunctionFlow SelectedSampleAfterAddLiquid
        {
            get { return _selectedSampleAfterAddLiquid; }
            set { SetProperty(ref _selectedSampleAfterAddLiquid, value); }
        }

        private AddLiquidFunctionFlow _selectedaddLiquid = null;
        /// <summary>
        /// 选中的加液液体
        /// </summary>
        public AddLiquidFunctionFlow SelectedAddLiquid
        {
            get { return _selectedaddLiquid; }
            set { SetProperty(ref _selectedaddLiquid, value); }
        }

        private AddDiluentFunction _selecteddiluent = null;
        /// <summary>
        /// 选中的稀释剂
        /// </summary>
        public AddDiluentFunction SelectedDiluent
        {
            get { return _selecteddiluent; }
            set { SetProperty(ref _selecteddiluent, value); }
        }


        #region 命令
        public ICommand AddCommand { get; set; }
        public ICommand DeleteCommand { get; set; }
        public ICommand MoveUpCommand { get; set; }
        public ICommand MoveDownCommand { get; set; }
        //public ICommand CopyCommand { get; set; }
        //public ICommand PasteCommand { get; set; }
        public ICommand ClearCommand { get; set; }
        //public ICommand ExportExcelCommand { get; set; }
        //public ICommand ImportExcelCommand { get; set; }

        //加液
        public ICommand AddCommand_Datagrid { get; set; }
        public ICommand DeleteCommand_Datagrid { get; set; }
        public ICommand ClearCommand_Datagrid { get; set; }

        #endregion

        public EditSampleBottlePropertiesViewModel()
        {
            // 初始化命令
            AddCommand = new RelayCommand<object>(AddRow);
            DeleteCommand = new RelayCommand<object>(DeleteRow);
            MoveUpCommand = new RelayCommand(MoveRowUp);
            MoveDownCommand = new RelayCommand(MoveRowDown);
            //CopyCommand = new RelayCommand(CopyRow);
            //PasteCommand = new RelayCommand(PasteRow);
            ClearCommand = new RelayCommand<object>(ClearRows);

            //datagrid 操作
            AddCommand_Datagrid = new RelayCommand<string>(Add_Datagrid);
            DeleteCommand_Datagrid = new RelayCommand<string>(Delete_Datagrid);
            ClearCommand_Datagrid = new RelayCommand<string>(Clear_Datagrid);
        }

        private void MoveRowDown()
        {
            if (SelectedFlow_Dose == null)
            {
                WeakReferenceMessenger.Default.Send(new DesktopAlertMessage() { Content = "未选中任何物料" });
                return;
            }
            for (int i = 0; i < SampleBottle.MaterialDoseFlowList.Count - 1; i++)
            {
                if (SelectedFlow_Dose == SampleBottle.MaterialDoseFlowList[i])
                {
                    //var temp = JsonConvert.DeserializeObject<MaterialDoseFunctionFlow>(JsonConvert.SerializeObject(SelectedFlow_Dose));
                    var temp = SelectedFlow_Dose;
                    //SampleBottle.MaterialDoseFlowList[i] = JsonConvert.DeserializeObject<MaterialDoseFunctionFlow>(JsonConvert.SerializeObject(SampleBottle.MaterialDoseFlowList[i + 1]));
                    SampleBottle.MaterialDoseFlowList[i] = SampleBottle.MaterialDoseFlowList[i + 1];
                    SampleBottle.MaterialDoseFlowList[i + 1] = temp;
                    return;
                }
            }
        }

        private void MoveRowUp()
        {
            if (SelectedFlow_Dose == null)
            {
                WeakReferenceMessenger.Default.Send(new DesktopAlertMessage() { Content = "未选中任何物料" });
                return;
            }
            for (int i = 1; i < SampleBottle.MaterialDoseFlowList.Count; i++)
            {
                if (SelectedFlow_Dose == SampleBottle.MaterialDoseFlowList[i])
                {
                    //var temp = JsonConvert.DeserializeObject<MaterialDoseFunctionFlow>(JsonConvert.SerializeObject(SelectedFlow_Dose));
                    var temp = SelectedFlow_Dose;
                    //SampleBottle.MaterialDoseFlowList[i] = JsonConvert.DeserializeObject<MaterialDoseFunctionFlow>(JsonConvert.SerializeObject(SampleBottle.MaterialDoseFlowList[i + 1]));
                    SampleBottle.MaterialDoseFlowList[i] = SampleBottle.MaterialDoseFlowList[i - 1];
                    SampleBottle.MaterialDoseFlowList[i - 1] = temp;
                    return;
                }
            }
        }

        //public override void Initialize(ICoreService coreService, string title, params object[] args)
        //{
        //    base.Initialize(coreService, title, args);
        //}

        //protected override void LoadServices()
        //{ 
        //    base.LoadServices();
        //}

        //protected override void LoadDatas()
        //{
        //    base.LoadDatas();
        //}

        //protected override void Register()
        //{
        //    base.Register();
        //}


        // 增加行
        private void AddRow(Object param)
        {
            if (param is string)
            {
                if (param.Equals("Liquid"))
                {
                    SampleBottle.MaterialDoseFlowList.Add(new MaterialDoseFunctionFlow());
                    SelectedFlow_Dose = SampleBottle.MaterialDoseFlowList.Last(); // 选中新增行
                }

            }
        }

        // 删除行
        private void DeleteRow(Object param)
        {
            if (param is string)
            {
                if (param.Equals("Liquid"))
                {
                    if (SelectedFlow_Dose != null)
                    {
                        // 获取当前选中行的索引
                        var liquidList = SampleBottle.MaterialDoseFlowList;
                        int currentIndex = liquidList.IndexOf(SelectedFlow_Dose);
                        if (currentIndex == -1) return; // 未找到索引

                        liquidList.Remove(SelectedFlow_Dose);
                        if (liquidList.Count > 0)
                        {
                            if (currentIndex == 0)
                            {
                                SelectedFlow_Dose = liquidList[0];
                            }
                            else
                            {
                                SelectedFlow_Dose = liquidList[currentIndex - 1];
                            }
                        }
                    }
                }
            }
        }

        // 清空行
        private void ClearRows(Object param)
        {
            if (param is string)
            {
                if (param.Equals("Liquid"))
                {
                    SampleBottle.MaterialDoseFlowList.Clear();
                }
            }
        }

        //加液
        private void Add_Datagrid(string command)
        {
            if (command.Equals("AddLiquid"))
            {
                SampleBottle.AddLiquidFunctionFlowList.Add(new AddLiquidFunctionFlow());
                SelectedAddLiquid = SampleBottle.AddLiquidFunctionFlowList.Last();
            }
            else if (command.Equals("Diluent"))
            {
                if (SelectedSample == null) return;
                SelectedSample.DiluentFunctionFlowList.Add(new AddDiluentFunction());
                SelectedDiluent = SelectedSample.DiluentFunctionFlowList.Last();
            }
            else if (command.Equals("Sample"))
            {
                SampleBottle.SampleFunctionFlowList.Add(new ReactSampleFunctionFlow());
                SelectedSample = SampleBottle.SampleFunctionFlowList.Last();
            }
            else if (command.Equals("SampleAfterAddLiquid"))
            {
                if (SelectedSample == null) return;
                SelectedSample.AddLiquidList.Add(new AddLiquidFunctionFlow());
                SelectedSampleAfterAddLiquid = SelectedSample.AddLiquidList.Last();
            }
        }


        private void Delete_Datagrid(string command)
        {
            if (command.Equals("AddLiquid"))
            {
                if (SelectedAddLiquid != null)
                {
                    // 获取当前选中行的索引
                    var addLiquidList = SampleBottle.AddLiquidFunctionFlowList;
                    int currentIndex = addLiquidList.IndexOf(SelectedAddLiquid);
                    if (currentIndex == -1) return; // 未找到索引

                    addLiquidList.Remove(SelectedAddLiquid);
                    if (addLiquidList.Count > 0)
                    {
                        if (currentIndex == 0)
                        {
                            SelectedAddLiquid = addLiquidList[0];
                        }
                        else
                        {
                            SelectedAddLiquid = addLiquidList[currentIndex - 1];
                        }
                    }
                }
            }
            else if (command.Equals("Diluent"))
            {
                if (SelectedSample != null)
                {
                    // 获取当前选中行的索引
                    var diluentList = SelectedSample.DiluentFunctionFlowList;
                    int currentIndex = diluentList.IndexOf(SelectedDiluent);
                    if (currentIndex == -1) return; // 未找到索引

                    diluentList.Remove(SelectedDiluent);
                    if (diluentList.Count > 0)
                    {
                        if (currentIndex == 0)
                        {
                            SelectedDiluent = diluentList[0];
                        }
                        else
                        {
                            SelectedDiluent = diluentList[currentIndex - 1];
                        }
                    }
                }
            }
            else if (command.Equals("Sample"))
            {
                if (SelectedSample != null)
                {
                    var list = SampleBottle.SampleFunctionFlowList;
                    int currentIndex = list.IndexOf(SelectedSample);
                    if (currentIndex == -1) return; // 未找到索引

                    list.Remove(SelectedSample);
                    if (list.Count > 0)
                    {
                        if (currentIndex == 0)
                        {
                            SelectedSample = list[0];
                        }
                        else
                        {
                            SelectedSample = list[currentIndex - 1];
                        }
                    }
                }
            }
            else if (command.Equals("SampleAfterAddLiquid"))
            {
                if (SelectedSample == null || SelectedSampleAfterAddLiquid == null) return;
                if (SelectedSampleAfterAddLiquid != null)
                {
                    var list = SelectedSample.AddLiquidList;
                    int currentIndex = list.IndexOf(SelectedSampleAfterAddLiquid);
                    if (currentIndex == -1) return; // 未找到索引

                    list.Remove(SelectedSampleAfterAddLiquid);
                    if (list.Count > 0)
                    {
                        if (currentIndex == 0)
                        {
                            SelectedSampleAfterAddLiquid = list[0];
                        }
                        else
                        {
                            SelectedSampleAfterAddLiquid = list[currentIndex - 1];
                        }
                    }
                }
            }
        }

        private void Clear_Datagrid(string command)
        {
            if (command.Equals("AddLiquid"))
            {
                SampleBottle.AddLiquidFunctionFlowList.Clear();
            }
            else if (command.Equals("Diluent"))
            {
                SelectedSample.DiluentFunctionFlowList.Clear();
            }
            else if (command.Equals("Sample"))
            {
                SampleBottle.SampleFunctionFlowList.Clear();
            }
            else if (command.Equals("SampleAfterAddLiquid"))
            {
                if (SelectedSample == null) return;
                SelectedSample.AddLiquidList.Clear();
            }
        }

    }
}
