using KGIS.Framework.Platform;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
namespace Kingo.PluginServiceInterface.Model
{
    public class TreeNode : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        private string _Text;
        /// 
        /// 显示的文本值
        /// 
        public string Name
        {
            get { return this._Text; }
            set
            {
                _Text = value;
                if (this.PropertyChanged != null)
                {
                    this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Name"));
                }
            }
        }
        private ContextMenu _cMenu;
        public ContextMenu cMenu
        {
            get { return this._cMenu; }
            set
            {
                _cMenu = value;
                if (this.PropertyChanged != null)
                {
                    this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("cMenu"));
                }
            }
        }
        private bool? _Checked;
        /// 
        /// 是否选中
        /// 
        public bool? Checked
        {
            get { return this._Checked; }
            set
            {
                _Checked = value;
                if (this.PropertyChanged != null)
                {
                    this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Checked"));
                }
            }
        }
        private bool _IsExpanded;
        /// 
        /// 是否展开
        /// 
        public bool IsExpanded
        {
            get { return this._IsExpanded; }
            set
            {
                _IsExpanded = value;
                if (this.PropertyChanged != null)
                {
                    this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("IsExpanded"));
                }
            }
        }
        private string _icon;
        /// 
        /// 节点图标:相对路径
        /// 
        public string Icon
        {
            get
            {
                if (this.Data is WYRWTB)
                {
                    WYRWTB wYRWTB = this.Data as WYRWTB;
                    if (wYRWTB.XBTB == 1)
                    {
                        _icon = "pack://application:,,,/Kingo.Plugin.General;component/Resources/星标.png";
                    }
                    else
                    {
                        _icon = "pack://application:,,,/Kingo.Plugin.General;component/Resources/未星标.png";
                    }
                }
                return this._icon;
            }
            set
            {
                _icon = value;
                if (this.PropertyChanged != null)
                {
                    this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Icon"));
                }
            }
        }
        private string _iconTooltip;
        public string IconTooltip
        {
            get
            {
                if (!string.IsNullOrWhiteSpace(Icon))
                {
                    _iconTooltip = System.IO.Path.GetFileNameWithoutExtension(Icon);
                }
                return this._iconTooltip;
            }
            set
            {
                _iconTooltip = value;
                if (this.PropertyChanged != null)
                {
                    this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("IconTooltip"));
                }
            }
        }
        private ObservableCollection _nodes;
        /// 
        /// 子节点,默认null
        /// 
        public ObservableCollection Nodes
        {
            get { return this._nodes; }
            set
            {
                _nodes = value;
                if (this.PropertyChanged != null)
                {
                    this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Nodes"));
                }
            }
        }
        private object _data;
        /// 
        /// 该节点数据项,默认null
        /// 
        public virtual object Data
        {
            get { return this._data; }
            set
            {
                _data = value;
                if (this.PropertyChanged != null)
                {
                    this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Data"));
                    this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Icon"));
                    this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("IconTooltip"));
                    this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("FWYVisibility"));
                }
            }
        }
        public virtual bool IsOpenEdit { get; set; }
        private string _PackageState;
        /// 
        /// 0:未完成   1:已完成  2:返外业 3:待确认 4:质检错误
        /// 
        public string PackageState
        {
            get { return this._PackageState; }
            set
            {
                _PackageState = value;
                if (this.PropertyChanged != null && _PackageState != null)
                {
                    this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("PackageState"));
                    this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("WWCVisibility"));
                    this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("WCVisibility"));
                    this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("ErrorVisibility"));
                    this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("YDCVisibility"));
                    this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("UploadVisibility"));
                    this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("ErrorUploadVisibility"));
                }
            }
        }
        private int _px;
        public int Paixu
        {
            get { return this._px; }
            set
            {
                _px = value;
                if (this.PropertyChanged != null)
                {
                    this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Paixu"));
                }
            }
        }
        public object _nodeID { get; internal set; }
        public object NodeID
        {
            get { return this._nodeID; }
            set
            {
                _nodeID = value;
                if (this.PropertyChanged != null)
                {
                    this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("NodeID"));
                }
            }
        }
        public string PackageID { get; set; }
        public string PackageName { get; set; }
        public int _nodeLevel { get; internal set; }
        /// 
        /// 层级:
        /// 0:任务列表 1、任务包节点  2、变更范围节点 3、图斑节点
        /// 
        public int NodeLevel
        {
            get { return this._nodeLevel; }
            set
            {
                _nodeLevel = value;
                if (this.PropertyChanged != null)
                {
                    this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("NodeLevel"));
                }
            }
        }
        /// 
        /// 返外业
        /// 
        public Visibility FWYVisibility
        {
            get
            {
                if (Data == null || (Data as WYRWTB) == null)
                {
                    return Visibility.Collapsed;
                }
                if (this.NodeLevel == 2 && Data is WYRWTB && (Data as WYRWTB).FWY == 1)
                {
                    return Visibility.Visible;
                }
                return Visibility.Collapsed;
            }
        }
        private Visibility _PackageButtonVisibility;
        public Visibility PackageButtonVisibility
        {
            get { return _PackageButtonVisibility; }
            set
            {
                _PackageButtonVisibility = value;
                if (this.PropertyChanged != null)
                {
                    this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("PackageButtonVisibility"));
                }
            }
        }
        private Visibility _ParentButtonVisibility;
        /// 
        /// 树父节点刷新按钮状态
        /// 
        public Visibility ParentButtonVisibility
        {
            get { return this._ParentButtonVisibility; }
            set
            {
                _ParentButtonVisibility = value;
                if (this.PropertyChanged != null)
                {
                    this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("ParentButtonVisibility"));
                }
            }
        }
        private Visibility _ZZTBVisibility;
        public Visibility ZZTBVisibility
        {
            get { return _ZZTBVisibility; }
            set
            {
                _ZZTBVisibility = value;
                if (this.PropertyChanged != null)
                {
                    this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("ZZTBVisibility"));
                }
            }
        }
        private Visibility _TBVisibility;
        /// 
        /// 图斑状态   预处理:已预审  审核:已审核
        /// 
        public Visibility TBVisibility
        {
            get { return this._TBVisibility; }
            set
            {
                _TBVisibility = value;
                if (this.PropertyChanged != null)
                {
                    this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("TBVisibility"));
                }
            }
        }
        private Visibility _YYSVisibility;
        /// 
        /// 适用于预处理   图斑状态   预处理:已预审  审核:已审核
        /// 
        public Visibility YYSVisibility
        {
            get { return this._YYSVisibility; }
            set
            {
                _YYSVisibility = value;
                if (this.PropertyChanged != null)
                {
                    this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("YYSVisibility"));
                }
            }
        }
        private Visibility _ZZBGVisibility;
        public Visibility ZZBGVisibility
        {
            get { return this._ZZBGVisibility; }
            set
            {
                _ZZBGVisibility = value;
                if (this.PropertyChanged != null)
                {
                    this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("ZZBGVisibility"));
                }
            }
        }
        private Visibility visibilityPackageButton;
        /// 
        /// 打开任务包按钮显示状态
        /// 
        public Visibility OpenPackageVisibility
        {
            get { return this.visibilityPackageButton; }
            set
            {
                visibilityPackageButton = value;
                if (this.PropertyChanged != null)
                {
                    this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("OpenPackageVisibility"));
                    this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("ClosePackageVisibility"));
                }
            }
        }
        /// 
        /// 关闭任务包按钮显示状态
        /// 
        public Visibility ClosePackageVisibility
        {
            get
            {
                //只有任务包节点 NodeLevel = 1才显示
                if (this.NodeLevel != 1)
                {
                    return Visibility.Collapsed;
                }
                if (this.OpenPackageVisibility == Visibility.Collapsed && this.Data is ESRI.ArcGIS.Carto.IFeatureLayer)
                {
                    return Visibility.Visible;
                }
                if (this.OpenPackageVisibility == Visibility.Visible || this.Data == null || (Data as TaskPackage) == null)
                {
                    return Visibility.Collapsed;
                }
                TaskPackage taskPackage = Data as TaskPackage;
                if (string.IsNullOrWhiteSpace(taskPackage.PackagePath) || !System.IO.File.Exists(taskPackage.PackagePath))
                {
                    return Visibility.Collapsed;
                }
                return Visibility.Visible;
            }
        }
        /// 
        /// 完成和未完成个数是否显示
        /// 
        public Visibility WCAndWWCNumVisibility
        {
            get
            {
                return Visibility.Collapsed;
                if (!KGIS.Framework.Utils.SysConfigsOprator.GetAppsetingValueByKey("ArearName").Equals("65"))
                {
                    return Visibility.Collapsed;
                }
                if (Platform.Instance.SystemType != SystemTypeEnum.DTBJK)
                {
                    return Visibility.Collapsed;
                }
                if (Data == null)
                {
                    return Visibility.Collapsed;
                }
                if (this.NodeLevel == 1)
                {
                    return Visibility.Visible;
                }
                return Visibility.Collapsed;
            }
        }
        private int _UncompletedCount;
        /// 
        /// 未完成个数
        /// 
        public int UncompletedCount
        {
            get { return this._UncompletedCount; }
            set
            {
                _UncompletedCount = value;
                if (this.PropertyChanged != null)
                {
                    this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("UncompletedCount"));
                }
            }
        }
        private int _CompletedCount;
        /// 
        /// 完成个数
        /// 
        public int CompletedCount
        {
            get { return this._CompletedCount; }
            set
            {
                _CompletedCount = value;
                if (this.PropertyChanged != null)
                {
                    this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("CompletedCount"));
                }
            }
        }
        /// 
        /// 未完成
        /// 
        public Visibility WWCVisibility
        {
            get
            {
                if (Platform.Instance.SystemType == SystemTypeEnum.YCLJK)
                {
                    return Visibility.Collapsed;
                }
                if (Data == null)
                {
                    return Visibility.Collapsed;
                }
                if (this.NodeLevel == 2 && (this.PackageState == null || this.PackageState.Equals("未完成")))
                {
                    return Visibility.Visible;
                }
                return Visibility.Collapsed;
            }
        }
        /// 
        /// 已完成
        /// 
        public Visibility WCVisibility
        {
            get
            {
                if (Platform.Instance.SystemType == SystemTypeEnum.YCLJK)
                {
                    return Visibility.Collapsed;
                }
                if (Data == null)
                {
                    return Visibility.Collapsed;
                }
                if (this.NodeLevel == 2 && this.PackageState != null && this.PackageState.Equals("已完成"))
                {
                    return Visibility.Visible;
                }
                return Visibility.Collapsed;
            }
        }
        /// 
        /// 已上传
        /// 
        public Visibility UploadVisibility
        {
            get
            {
                if (Platform.Instance.SystemType == SystemTypeEnum.YCLJK)
                {
                    return Visibility.Collapsed;
                }
                if (Data == null)
                {
                    return Visibility.Collapsed;
                }
                if (this.NodeLevel == 2 && this.PackageState != null && this.PackageState.Equals("已上传"))
                {
                    return Visibility.Visible;
                }
                return Visibility.Collapsed;
            }
        }
        /// 
        /// 上传失败
        /// 
        public Visibility ErrorUploadVisibility
        {
            get
            {
                if (Platform.Instance.SystemType == SystemTypeEnum.YCLJK)
                {
                    return Visibility.Collapsed;
                }
                if (Data == null)
                {
                    return Visibility.Collapsed;
                }
                if (this.NodeLevel == 2 && this.PackageState != null && this.PackageState.Equals("上传失败"))
                {
                    return Visibility.Visible;
                }
                return Visibility.Collapsed;
            }
        }
        /// 
        /// 已导出
        /// 
        public Visibility YDCVisibility
        {
            get
            {
                if (Data == null || (Data as WYRWTB) == null)
                {
                    return Visibility.Collapsed;
                }
                if (this.NodeLevel == 2 && this.PackageState != null && this.PackageState.Equals("已输出"))
                {
                    return Visibility.Visible;
                }
                return Visibility.Collapsed;
            }
        }
        /// 
        /// 质检错误
        /// 
        public Visibility ErrorVisibility
        {
            get
            {
                if (Data == null)
                {
                    return Visibility.Collapsed;
                }
                if (this.NodeLevel == 2 && this.PackageState != null && this.PackageState.Equals("质检错误"))
                {
                    return Visibility.Visible;
                }
                return Visibility.Collapsed;
            }
        }
        private Visibility _LoadingVisibility = Visibility.Collapsed;
        public Visibility LoadingVisibility
        {
            get
            {
                return _LoadingVisibility;
            }
            set
            {
                _LoadingVisibility = value;
                if (this.PropertyChanged != null)
                {
                    this.PropertyChanged(this, new PropertyChangedEventArgs("LoadingVisibility"));
                }
            }
        }
        private Visibility _TreeVisibility;
        /// 
        /// 树节点是否显示
        /// 
        public Visibility TreeVisibility
        {
            get { return this._TreeVisibility; }
            set
            {
                _TreeVisibility = value;
                if (this.PropertyChanged != null)
                {
                    this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("TreeVisibility"));
                }
            }
        }
        #region NodeX-构造函数(初始化)
        /// 
        ///  NodeX-构造函数(初始化)
        /// 
        public TreeNode()
        {
            this.IconTooltip = string.Empty;
            this.Name = string.Empty;
            this.Icon = string.Empty;
            this.Checked = false;
            ParentButtonVisibility = Visibility.Collapsed;
            OpenPackageVisibility = Visibility.Collapsed;
            ZZBGVisibility = Visibility.Collapsed;
            ZZTBVisibility = Visibility.Collapsed;
            TBVisibility = Visibility.Collapsed;
            YYSVisibility = Visibility.Collapsed;
            this.LoadingVisibility = Visibility.Collapsed;
            Nodes = new ObservableCollection();
            if (Platform.Instance.SystemType == SystemTypeEnum.DTBJK || Platform.Instance.SystemType == SystemTypeEnum.WYZS)
            {
                if (ParentNode != null && ParentNode.Data != null)
                    Paixu = ParentNode.Nodes.Count + 1;
                this.PackageState = "未完成";
            }
            else if (Platform.Instance.SystemType == SystemTypeEnum.YCLJK)
            {
                this.PackageState = null;
            }
        }
        private TreeNode _ParentNode;
        /// 
        /// 上级节点
        /// 
        public TreeNode ParentNode
        {
            get { return this._ParentNode; }
            set
            {
                _ParentNode = value;
                if (this.PropertyChanged != null)
                {
                    this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("ParentNode"));
                }
            }
        }
        private TreeNode _PackageNode;
        /// 
        /// 所属监测图斑图层节点/任务包所在的树节点
        /// 
        public TreeNode PackageNode
        {
            get { return this._PackageNode; }
            set
            {
                _PackageNode = value;
                if (this.PropertyChanged != null)
                {
                    this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("PackageNode"));
                }
            }
        }
        #endregion
    }
}