using AntDesign;
using Microsoft.AspNetCore.Components;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using wispro.sp.entity;

namespace wispro.sp.web.Components
{
    public partial class InputValueSetting
    {
        List<wispro.sp.entity.EnumnDescription<wispro.sp.entity.workflowDefine.EnumFieldType>> FieldTypes
            = entity.EnumHelper.getEnumDescriptionDic<wispro.sp.entity.workflowDefine.EnumFieldType>();

        
        entity.workflowDefine.InputValueSetting EditObj;

        [Parameter]
        public share.ActionObject EditAction { get; set; }


        IDictionary<string, (bool edit, entity.workflowDefine.InputValueSetting data)> editCache = new Dictionary<string, (bool edit, entity.workflowDefine.InputValueSetting data)>();

        private List<entity.workflowDefine.InputValueSetting > GetChildItem(share.InputValueSettingObject setting)
        {
            if(setting.Children == null)
            {
                setting.Children = new List<entity.workflowDefine.InputValueSetting>();
            }
            return setting.Children;
            //return DataSource.Where<entity.workflowDefine.InputValueSetting>(p => p.ParentSetting == setting).ToList();
        }

        void AddNew(entity.workflowDefine.InputValueSetting setting)
        {
            Console.WriteLine("AddNew");
            var newObj = new entity.workflowDefine.InputValueSetting()
            {
                valueType = entity.workflowDefine.EnumFieldType.Text,
                DisplayName = "新添栏位",
                ParentSetting = setting
                
            };
            EditObj = newObj;

            Console.WriteLine(System.Text.Json.JsonSerializer.Serialize(EditObj));

            if (setting != null)
            {
                foreach(var ss in EditAction.InputValueSettingObjects)
                {
                    if(ss.InputValueSetting == setting)
                    {
                        if(ss.Children == null)
                        {
                            ss.Children = new List<entity.workflowDefine.InputValueSetting>();
                            ss.Children.Add(newObj);
                            break;
                        }
                    }
                }
            }
            else
            {
                share.InputValueSettingObject newSettingobj = new share.InputValueSettingObject();
                newSettingobj.InputValueSetting = EditObj;

                EditAction.InputValueSettingObjects.Add(newSettingobj);

                if (EditAction.NewInputValueSettingObjects == null)
                {
                    EditAction.NewInputValueSettingObjects = new List<share.InputValueSettingObject>();
                }
                EditAction.NewInputValueSettingObjects.Add(newSettingobj);
            }
        }

        void Delete(entity.workflowDefine.InputValueSetting setting)
        {
            Console.WriteLine($"begin Delete:{ System.Text.Json.JsonSerializer.Serialize(EditAction)}");

            Console.WriteLine($"Delete Setting:{ System.Text.Json.JsonSerializer.Serialize(setting)}");

            foreach (var temObj in EditAction.InputValueSettingObjects)
            {
                if (temObj.InputValueSetting.DisplayName  == setting.DisplayName )
                {
                    EditAction.InputValueSettingObjects.Remove(temObj);
                }

                bool isFound = false;
                if (temObj.Children != null)
                {
                    foreach (var iv in temObj.Children)
                    {
                        if (iv.DisplayName  == setting.DisplayName)
                        {
                            temObj.Children.Remove(iv);
                            isFound = true;
                            break;
                        }
                    }
                }

                if (isFound)
                {
                    break;
                }
            }

            if (EditAction.NewInputValueSettingObjects != null)
            {
                foreach (var temObj in EditAction.NewInputValueSettingObjects)
                {
                    if (temObj.InputValueSetting.DisplayName == setting.DisplayName)
                    {
                        EditAction.NewInputValueSettingObjects.Remove(temObj);
                    }

                    bool isFound = false;
                    if (temObj.Children != null)
                    {
                        foreach (var iv in temObj.Children)
                        {
                            if (iv.DisplayName == setting.DisplayName)
                            {
                                temObj.Children.Remove(iv);
                                isFound = true;
                                break;
                            }
                        }
                    }

                    if (isFound)
                    {
                        break;
                    }
                }
            }

            if (EditAction.UpdateInputValueSettingObjects != null)
            {
                foreach (var temObj in EditAction.UpdateInputValueSettingObjects)
                {
                    if (temObj.InputValueSetting.DisplayName == setting.DisplayName)
                    {
                        EditAction.UpdateInputValueSettingObjects.Remove(temObj);
                    }

                    bool isFound = false;
                    if (temObj.Children != null)
                    {
                        foreach (var iv in temObj.Children)
                        {
                            if (iv.DisplayName == setting.DisplayName)
                            {
                                temObj.Children.Remove(iv);
                                isFound = true;
                                break;
                            }
                        }
                    }

                    if (isFound)
                    {
                        break;
                    }
                }
            }

            Console.WriteLine($"End Delete:{ System.Text.Json.JsonSerializer.Serialize(EditAction)}");
        }
        async void Delete(share.InputValueSettingObject setting)
        {

            Console.WriteLine($"Begin Delete:{ System.Text.Json.JsonSerializer.Serialize(EditAction)}");

            Console.WriteLine($"Delete Setting:{ System.Text.Json.JsonSerializer.Serialize(setting)}");
            //此处添加删除代码
            if (EditAction.InputValueSettingObjects.Contains(setting))
            {
                EditAction.InputValueSettingObjects.Remove(setting);
            }
            else
            {
                foreach (var temObj in EditAction.InputValueSettingObjects)
                {
                    if (temObj.Children != null && temObj.Children.Contains ( setting.InputValueSetting))
                    {
                        temObj.Children.Remove(setting.InputValueSetting);
                        break;
                    }
                }
            }

            if (EditAction.NewInputValueSettingObjects != null)
            {
                if (EditAction.NewInputValueSettingObjects.Contains(setting))
                {
                    EditAction.NewInputValueSettingObjects.Remove(setting);
                }
                else
                {
                    foreach (var temObj in EditAction.NewInputValueSettingObjects)
                    {
                        if (temObj.Children != null && temObj.Children.Contains(setting.InputValueSetting))
                        {
                            temObj.Children.Remove(setting.InputValueSetting);
                            break;
                        }
                    }
                }
            }

            if (EditAction.UpdateInputValueSettingObjects != null)
            {
                if (EditAction.UpdateInputValueSettingObjects.Contains(setting))
                {
                    EditAction.UpdateInputValueSettingObjects.Remove(setting);
                }
                else
                {
                    foreach (var temObj in EditAction.UpdateInputValueSettingObjects)
                    {
                        if (temObj.Children != null && temObj.Children.Contains(setting.InputValueSetting))
                        {
                            temObj.Children.Remove(setting.InputValueSetting);
                            break;
                        }
                    }
                }
            }

            if (setting.InputValueSetting.Id > 0)
            {
                //添加从数据库中删除代码；
                await wfService.DeleteInputValueSetting(setting.InputValueSetting.Id);
            }

            Console.WriteLine($"End Delete:{ System.Text.Json.JsonSerializer.Serialize(EditAction)}");

            StateHasChanged();
        }

        void Edit(entity.workflowDefine.InputValueSetting setting)
        {
            EditObj = setting;
        }

        void Save(entity.workflowDefine.InputValueSetting setting)
        {
            
            if (setting.Id > 0)
            {
                var temObj = EditAction.InputValueSettingObjects.FirstOrDefault(p => p.InputValueSetting.Id == setting.Id);

                if (temObj != null)
                {
                    if (EditAction.UpdateInputValueSettingObjects == null)
                    {
                        EditAction.UpdateInputValueSettingObjects = new List<share.InputValueSettingObject>();
                        
                    }

                    if (!EditAction.UpdateInputValueSettingObjects.Contains(temObj))
                    {
                        EditAction.UpdateInputValueSettingObjects.Add(temObj);
                    }
                }
            }

            EditObj = null;
            StateHasChanged();
        }

        async Task OnCancel(entity.workflowDefine.InputValueSetting setting)
        {
            if(setting.Id == 0)
            {
                foreach(var temobj in EditAction.InputValueSettingObjects)
                {
                    if(temobj.InputValueSetting == setting)
                    {
                        EditAction.InputValueSettingObjects.Remove(temobj);
                        break;
                    }
                }

                foreach (var temobj in EditAction.NewInputValueSettingObjects )
                {
                    if (temobj.InputValueSetting == setting)
                    {
                        EditAction.NewInputValueSettingObjects.Remove(temobj);
                        break;
                    }
                }
            }
            else
            {
                foreach (var temobj in EditAction.UpdateInputValueSettingObjects)
                {
                    if (temobj.InputValueSetting == setting)
                    {
                        EditAction.UpdateInputValueSettingObjects.Remove(temobj);
                        break;
                    }
                }

                setting = await  wfService.getInputValueSteeingById(setting.Id);

            }

            EditObj = null;
        }

        [Inject]
        Services.WorkflowService  wfService { get; set; }
    }

    
}
