using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WebAPIBase.NetCore.BusinessCore
{
    public class FdcSystemGroup : IComparable<FdcSystemGroup>
    {
        public static string SaHouseTypeClassCode = "670201";

        public static string SaMarketPlanTypeClassCode = "670203";

        public static string SaComplaintType = "670202";

        public static string BalanceMode = "670204";

        public static string SaComplaintFault = "670205";

        public string GroupCode;

        public string ClassCode;

        public string ParentCode;

        public string FullId;

        public string SortId;

        public string Deep;

        private string[] fullIdList;

        public string GroupName;

        protected string fullName = null;

        public List<FdcSystemGroup> Children = new List<FdcSystemGroup>();

        private FdcUnitStructure fus;

        public string FullName
        {
            get
            {
                if (fullName == null)
                {
                    if (ParentCode == null || ParentCode == "")
                    {
                        fullName = "";
                    }
                    else
                    {
                        string text = fus.GetGroupByCode(ParentCode).FullName;
                        if (text != "")
                        {
                            fullName = text + "->" + GroupName;
                        }
                        else
                        {
                            fullName = GroupName;
                        }
                    }
                }
                return fullName;
            }
        }

        internal FdcSystemGroup(FdcUnitStructure _fus, string groupcode, string classcode, string parentcode, string fullid, string groupname)
        {
            fus = _fus;
            GroupCode = groupcode;
            ClassCode = classcode;
            ParentCode = parentcode;
            FullId = fullid;
            GroupName = groupname;
            fullIdList = FullId.Split('-');
        }

        public bool isMyParent(FdcSystemGroup parentGroup)
        {
            if (FullId.IndexOf(parentGroup.GroupCode) >= 0)
            {
                return true;
            }
            return false;
        }

        public bool isMyParent(string parentGroupCode)
        {
            if (FullId.IndexOf(parentGroupCode) >= 0)
            {
                return true;
            }
            return false;
        }

        public bool isMyChild(FdcSystemGroup childGroup)
        {
            if (childGroup.isMyParent(this))
            {
                return true;
            }
            return false;
        }

        public bool isMyChild(string childGroupCode)
        {
            FdcSystemGroup groupByCode = fus.GetGroupByCode(childGroupCode);
            if (groupByCode == null)
            {
                return false;
            }
            return isMyChild(groupByCode);
        }

        internal void GetChild2List(List<FdcSystemGroup> children)
        {
            if (children.Contains(this))
            {
                return;
            }
            children.Add(this);
            foreach (FdcSystemGroup child in Children)
            {
                child.GetChild2List(children);
            }
        }

        public static string GetChildList(string groupCode)
        {
            FdcSystemGroup groupByCode = FdcUnitStructure.Instance.GetGroupByCode(groupCode);
            if (groupByCode == null)
            {
                return "";
            }
            StringBuilder stringBuilder = new StringBuilder();
            List<FdcSystemGroup> list = new List<FdcSystemGroup>();
            groupByCode.GetChild2List(list);
            foreach (FdcSystemGroup item in list)
            {
                if (stringBuilder.Length > 0)
                {
                    stringBuilder.Append(",");
                }
                stringBuilder.Append(item.GroupCode);
            }
            return stringBuilder.ToString();
        }

        public static FdcSystemGroup GetSystemGroupRootByClass(string classCode)
        {
            foreach (DictionaryEntry item in FdcUnitStructure.Instance.GroupTree)
            {
                FdcSystemGroup fdcSystemGroup = item.Value as FdcSystemGroup;
                if (fdcSystemGroup.ClassCode == classCode && fdcSystemGroup.ParentCode == "")
                {
                    return fdcSystemGroup;
                }
            }
            return null;
        }

        int IComparable<FdcSystemGroup>.CompareTo(FdcSystemGroup other)
        {
            int num = int.Parse(GroupCode);
            int num2 = int.Parse(other.GroupCode);
            if (num > num2)
            {
                return 1;
            }
            if (num == num2)
            {
                return 0;
            }
            return -1;
        }
    }
}
