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

namespace WebAPIBase.NetCore.BusinessCore
{
    public class FdcStation
    {
        private FdcUnitStructure fdcUnitStructure;

        private string stationCode;

        private string stationName;

        private string unitCode;

        internal List<FdcUnitRight> unitList = new List<FdcUnitRight>();

        internal Dictionary<string, innerFunction> functionList = new Dictionary<string, innerFunction>();

        public List<int> UserList = new List<int>();

        public string StationCode
        {
            get
            {
                return stationCode;
            }
            set
            {
                stationCode = value;
            }
        }

        public string StationName
        {
            get
            {
                return stationName;
            }
            set
            {
                stationName = value;
            }
        }

        public string UnitCode
        {
            get
            {
                return unitCode;
            }
            set
            {
                unitCode = value;
            }
        }

        internal FdcStation(FdcUnitStructure unitStructure, string code, string name)
        {
            fdcUnitStructure = unitStructure;
            StationCode = code;
            StationName = name;
        }

        public bool HasRight(string functionCode)
        {
            return functionList.ContainsKey(functionCode);
        }

        public bool HasUnitRight(string functionCode, string unitCode)
        {
            if (unitCode == null || unitCode == string.Empty)
            {
                throw new ArgumentException("unit code不能为空");
            }
            if (functionList.ContainsKey(functionCode))
            {
                innerFunction innerFunction = functionList[functionCode];
                foreach (FdcUnitRight unit in innerFunction.UnitList)
                {
                    if (unit.unit.isMyChild(unitCode))
                    {
                        return true;
                    }
                }
            }
            return false;
        }

        public bool HasSubUnitRight(string functionCode, string unitCode)
        {
            if (unitCode == null || unitCode == string.Empty)
            {
                throw new ArgumentException("unit code不能为空");
            }
            if (functionList.ContainsKey(functionCode))
            {
                innerFunction innerFunction = functionList[functionCode];
                foreach (FdcUnitRight unit in innerFunction.UnitList)
                {
                    if (unit.unit.isMyChild(unitCode))
                    {
                        return true;
                    }
                    if (unit.unit.isMyParent(unitCode))
                    {
                        return true;
                    }
                }
            }
            return false;
        }

        public bool HasGroupRight(string functionCode, string groupCode)
        {
            return HasRight(functionCode, "", groupCode);
        }

        public bool HasRight(string functionCode, string unitCode, string groupCode)
        {
            if ((unitCode == null || unitCode == string.Empty) && (groupCode == null || groupCode == string.Empty))
            {
                return HasRight(functionCode);
            }
            bool flag = true;
            if (unitCode == null || unitCode == string.Empty)
            {
                flag = false;
            }
            if (groupCode == null || groupCode == string.Empty)
            {
                return HasUnitRight(functionCode, unitCode);
            }
            if (functionList.ContainsKey(functionCode))
            {
                innerFunction innerFunction = functionList[functionCode];
                FdcFunction functionByCode = fdcUnitStructure.GetFunctionByCode(functionCode);
                if (!functionByCode.UnitRelated && !functionByCode.SystemGroupRelated)
                {
                    return true;
                }
                if (functionByCode.UnitRelated && !functionByCode.SystemGroupRelated)
                {
                    return HasUnitRight(functionCode, unitCode);
                }
                if (!functionByCode.UnitRelated && functionByCode.SystemGroupRelated)
                {
                    flag = false;
                }
                foreach (FdcGroupUnitRight groupUnit in innerFunction.GroupUnitList)
                {
                    if ((!flag || groupUnit.unit.isMyChild(unitCode)) && groupUnit.group.isMyChild(groupCode))
                    {
                        return true;
                    }
                }
            }
            return false;
        }

        public RightScope GetUnitRightScope(string unitCode)
        {
            RightScope result = RightScope.无;
            foreach (FdcUnitRight unit in unitList)
            {
                if (unit.IsPerson == 0)
                {
                    if (unit.unit.isMyChild(unitCode))
                    {
                        return RightScope.部门;
                    }
                }
                else if (unit.unit.isMyParent(unitCode))
                {
                    result = RightScope.个人;
                }
            }
            return result;
        }
    }

    public class FdcUnitRight
    {
        public FdcUnit unit;

        public int IsPerson;
    }
}
