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

namespace wispro.sp.web.Components
{
    public partial class ResponseManCondition
    {
        
        UserConditionType userConditionType;
        
        UserType _userType;

        List<Department> departments;
        List<Position> lstPosition;
        int SelectPositionsId;

        internal class UserConditionTpyeItem
        {
            public UserConditionType Type { get; set; }

            public string Description
            {
                get
                {
                    return EnumHelper.GetDescription<UserConditionType>(Type);
                }
            }
        }

        List<UserConditionTpyeItem> uctItems = new List<UserConditionTpyeItem>();
        List<CascaderNode> optoins = new List<CascaderNode>();

        private List<CascaderNode> GetFromDepartment(List<Department> _departments)
        {
            List<CascaderNode> retList = new List<CascaderNode>();
            foreach(var dept in _departments)
            {
                CascaderNode node = new CascaderNode() { Label = dept.Name, Value = dept.Id.ToString() };
                retList.Add(node);
            }

            foreach(var dept in _departments)
            {
                if(dept.parentId != null)
                {
                    var parentNode = FindNode(dept.parentId.Value.ToString(),retList);
                    var currentNode = FindNode(dept.Id.ToString(), retList);

                    //Console.WriteLine($"Find：{dept.parentId}，return：{System.Text.Json.JsonSerializer.Serialize(parentNode)}");
                    //Console.WriteLine($"Find：{dept.Id}，return：{System.Text.Json.JsonSerializer.Serialize(currentNode)}");

                    if (parentNode != null)
                    {
                        if (parentNode.Children == null)
                        {
                            parentNode.Children = new List<CascaderNode>();
                        }

                        ((List<CascaderNode>)parentNode.Children).Add(currentNode);
                        retList.Remove(currentNode);
                    }

                    

                }
                
            }

            return retList;
        }

        private CascaderNode FindNode(string deptId,List<CascaderNode> retList)
        {
            
            foreach(var node in retList)
            {
                
                if (node.Value == deptId)
                {
                    //Console.WriteLine($"Founded:{System.Text.Json.JsonSerializer.Serialize(node)}");
                    return node;
                }
                else
                {
                    if (node.Children != null)
                    {
                        var temNode = FindNode(deptId, (List<CascaderNode>)node.Children);
                        if (temNode != null)
                        {
                            
                            return temNode;
                        }
                    }
                }
                
            }

            
            return null;
        }

        [Inject] OrganizationService _orgService { get; set; }

        [Parameter]
        public Workflow Workflow { get; set; }

        [Parameter]
        public UserField UserField { get; set; }

        protected async override Task OnInitializedAsync()
        {
            departments =await  _orgService.GetDepartments();
            lstPosition = await _orgService.getPositions(null);

            optoins = GetFromDepartment(departments);

            uctItems.Add(new UserConditionTpyeItem() { Type = UserConditionType.Department });
            uctItems.Add(new UserConditionTpyeItem() { Type = UserConditionType.DepartmentPosition });
            uctItems.Add(new UserConditionTpyeItem() { Type = UserConditionType.Staff });
            uctItems.Add(new UserConditionTpyeItem() { Type = UserConditionType.UserDepartment });
            uctItems.Add(new UserConditionTpyeItem() { Type = UserConditionType.UserDepartmentPosition });

            if (UserField != null)
            {
                if (!string.IsNullOrEmpty(UserField.Positon))
                {
                    SelectPositionsId = int.Parse(UserField.Positon);
                }

                
            }
            await base.OnInitializedAsync();
        }


        private void OnUCTItemChangedHandler(UserConditionTpyeItem value)
        {
            userConditionType = value.Type;
        }

        private async Task OnDeptChange(CascaderNode[] selectedNodes)
        {
            int deptId = int.Parse(selectedNodes[0].Value);
            if(userConditionType == UserConditionType.DepartmentPosition || userConditionType == UserConditionType.DepartmentPosition)
            {
                lstPosition =await _orgService.getPositions(deptId);
            }
        }

        void OnPositionChangedHandler(Position position)
        {
            UserField.Positon = position.Id.ToString();
        }
    }
}
