using Kingo.PluginServiceInterface.Model;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
namespace Kingo.PluginServiceInterface
{
    [XmlRoot("LayerInfo")]
    public class LayerCfg
    {
        [System.Xml.Serialization.XmlIgnore]
        public int ID { get; private set; }
        [System.Xml.Serialization.XmlIgnore]
        public int PID { get; private set; }
        [System.Xml.Serialization.XmlIgnore]
        public Bitmap SymbolImg { get; set; }
        [System.Xml.Serialization.XmlIgnore]
        public object Data { get; set; }
        [System.Xml.Serialization.XmlIgnore]
        public bool IsEdit { get; set; }
        [System.Xml.Serialization.XmlIgnore]
        public int ImgIndex { get; set; }
        [System.Xml.Serialization.XmlIgnore]
        private bool _IsLoad;
        public bool IsLoad
        {
            get
            {
                //_IsLoad = !string.IsNullOrWhiteSpace(FcPath);
                return _IsLoad;
            }
            set
            {
                if (value && !string.IsNullOrWhiteSpace(FcPath))
                {
                    _IsLoad = value;
                }
                else
                {
                    _IsLoad = false;
                }
            }
        }
        /// 
        /// 图层名
        /// 
        [XmlAttribute("LayerName")]
        public string LayerName { get; set; }
        /// 
        /// 表名
        /// 
        [XmlAttribute("FcName")]
        public string FcName { get; set; }
        /// 
        /// 节点展开状态
        /// 
        [XmlAttribute("Expanded")]
        public bool Expanded { get; set; }
        /// 
        /// 是否显示
        /// 
        [XmlAttribute("Visible")]
        public bool Visible { get; set; }
        /// 
        /// 是否可选
        /// 
        [XmlAttribute("Selectable")]
        public bool Selectable { get; set; }
        /// 
        /// 是否允许编辑
        /// 
        [XmlAttribute("AllowEdit")]
        public bool AllowEdit { get; set; }
        /// 
        /// 图层透明度
        /// 
        [XmlAttribute("Transparency")]
        public int Transparency { get; set; }
        /// 
        /// 是否必选图层
        /// 
        [XmlAttribute("Required")]
        public bool Required { get; set; }
        /// 
        /// 图层路径
        /// 
        public string FcPath { get; set; }
        /// 
        /// 图层符号
        /// 
        public string Symbol { get; set; }
        /// 
        /// 图层标注
        /// 
        public string Annotation { get; set; }
        /// 
        /// 属性编辑时字段配置方案
        /// 
        public string FieldEditCfg { get; set; }
        /// 
        /// 绝对路径Data
        /// 
        public string AbsolutePath { get; set; }
        /// 
        /// 区分内外部数据
        /// 
        public string AbsolutePathType { get; set; }
        /// 
        ///  基础数据加载状态
        /// 
        public TBState LoadLayerState { get; set; }
        /// 
        /// 图层字段配置
        /// 
        public List FieldsCfg
        {
            get
            {
                List result = new List();
                if (_FieldsCfg != null)
                {
                    for (int i = 0; i < _FieldsCfg.Count; i++)
                    {
                        _FieldsCfg[i].PID = this.ID;//layerCfg.ID
                        _FieldsCfg[i].ID = ID * 100 + i + 1;
                        //var subList = GetAllFields(_FieldsCfg[i]);
                        //if (subList != null && subList.Count > 0)
                        //    result.AddRange(subList);
                    }
                }
                if (result.Count == 0)
                    return _FieldsCfg;
                else
                    return result;
            }
            set
            {
                _FieldsCfg = value;
            }
        }
        private List _FieldsCfg { get; set; }
        /// 
        /// 图层类型
        /// 
        [XmlAttribute("LayerType")]
        public EnumLayerType LayerType { get; set; }
        public List Layers { get; set; }
        public int MinScale { get; set; }
        public int MaxScale { get; set; }
        public string DefinitionExpression { get; set; }
        public LayerCfg()
        {
            PID = 0;
            ID = 1;
            Layers = new List();
        }
        public LayerCfg(LayerCfg pParentLayer)
        {
            Layers = new List();
            if (pParentLayer == null) return;
            PID = pParentLayer.ID;
            ID = pParentLayer.ID * 1000 + pParentLayer.Layers.Count + 1;
        }
        public string FileSize { get; set; }
        public bool IsChecked { get; set; }
        /// 
        /// 将父子级数据结构转换为普通list
        /// 
        /// 
        /// 
        public List GetAllItem()
        {
            List Resultlist = new List();
            //foreach (var item in list)
            //{
            OperationChildData(Resultlist, this);
            Resultlist.Add(this);
            //}
            return Resultlist;
        }
        //public List GetAllFields(FieldCfg pField)
        public List GetAllFields()
        {
            List Resultlist = new List();
            foreach (FieldCfg pField in this.FieldsCfg)
            {
                OperationChildDataForField(Resultlist, pField);
                Resultlist.Add(pField);
            }
            return Resultlist;
        }
        /// 
        /// 递归子级数据
        /// 
        /// 树形列表数据
        /// 父级model
        private void OperationChildDataForField(List AllList, FieldCfg item)
        {
            if (item.Fields != null)
            {
                if (item.Fields.Count > 0)
                {
                    foreach (var subItem in item.Fields)
                    {
                        //subItem.SetParentLayer(item);
                        AllList.Add(subItem);
                        OperationChildDataForField(AllList, subItem);
                    }
                }
            }
        }
        #region Private
        /// 
        /// 递归子级数据
        /// 
        /// 树形列表数据
        /// 父级model
        private void OperationChildData(List AllList, LayerCfg item)
        {
            if (item.Layers != null)
            {
                if (item.Layers.Count > 0)
                {
                    foreach (var subItem in item.Layers)
                    {
                        subItem.SetParentLayer(item);
                        AllList.Add(subItem);
                        OperationChildData(AllList, subItem);
                    }
                }
            }
        }
        private void SetParentLayer(LayerCfg pParent)
        {
            PID = pParent.ID;
            ID = pParent.ID * 100 + pParent.Layers.IndexOf(this) + 1;
        }
        #endregion
    }
    public enum TBState
    {
        /// 
        /// 等待中
        /// 
        Waiting = 0,
        /// 
        /// 删除
        /// 
        Delete = 1,
        /// 
        /// 开始导入
        /// 
        BeingImport = 2,
        /// 
        /// 结束导入
        /// 
        EndImport = 3
    }
    //public enum EnumLayerType
    //{
    //    GroupLayer,
    //    FeatureLayer,
    //    RasterLayer,
    //    KOTilesLayer,
    //    Map
    //}
}