using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Mesnac.Controls.Base;
using System.Drawing;

namespace Mesnac.Controls.Default
{
    [ToolboxBitmap(typeof(TreeView))]
    public partial class MCTreeView : TreeView, IBaseControl
    {
        #region 字段定义

        private static bool _onOff = true;  //开关变量
        private bool _mcVisible = true; //保存可见性
        private bool _mcEnabled = true; //保存可用性
        private bool _isValid = true; //保存有效性
        private bool _isEventValid = true;      //保存事件有效性
        private List<DesignAction> _clickActionList = new List<DesignAction>();
        private List<DesignAction> _doubleClickActionList = new List<DesignAction>();
        private List<DesignAction> _afterCheckActionList = new List<DesignAction>();

        #endregion

        #region 构造方法

        public MCTreeView()
        {
            InitializeComponent();
            this.AfterCheck += new System.Windows.Forms.TreeViewEventHandler(MCTreeView_AfterCheck);
        }

        public MCTreeView(IContainer container)
        {
            container.Add(this);

            InitializeComponent();
            this.AfterCheck += new System.Windows.Forms.TreeViewEventHandler(MCTreeView_AfterCheck);
        }

        #endregion

        #region 事件处理

        private void MCTreeView_AfterCheck(object sender, TreeViewEventArgs e)
        {
            if (_onOff)
            {
                this.SetChildCheckState(e.Node);
                _onOff = false;
                this.SetParentCheckState(e.Node);
                _onOff = true;
            }
        }

        #endregion

        #region 方法定义

        /// <summary>
        /// 递归更新子节点状态
        /// </summary>
        /// <param name="node"></param>
        private void SetChildCheckState(TreeNode node)
        {
            foreach (TreeNode childNode in node.Nodes)
            {
                childNode.Checked = node.Checked;
                this.SetChildCheckState(childNode);
            }
        }
        /// <summary>
        /// 递归更新父节点状态
        /// </summary>
        /// <param name="node"></param>
        private void SetParentCheckState(TreeNode node)
        {
            if (node.Parent != null)
            {
                if (node.Checked == true)
                {
                    node.Parent.Checked = node.Checked;
                }
                else
                {
                    bool flag = false;
                    foreach (TreeNode brotherNode in node.Parent.Nodes)
                    {
                        if (brotherNode.Checked == true)
                        {
                            flag = true;
                            break;
                        }
                    }
                    if (flag == false)
                    {
                        node.Parent.Checked = flag;
                    }
                }
                this.SetParentCheckState(node.Parent);
            }
        }

        #endregion

        #region 事件定义

        public List<DesignAction> ClickActionList
        {
            get { return this._clickActionList; }
            set { this._clickActionList = value; }
        }
        public List<DesignAction> DoubleClickActionList
        {
            get { return _doubleClickActionList; }
            set { _doubleClickActionList = value; }
        }
        public List<DesignAction> AfterCheckActionList
        {
            get { return _afterCheckActionList; }
            set { _afterCheckActionList = value; }
        }
        #endregion

        #region 接口成员实现

        public string MCKey
        {
            get;
            set;
        }

        public object MCValue
        {
            get
            {
                return this.SelectedNode;
            }
            set
            {
                this.SelectedNode = value == null ? new TreeNode() : (TreeNode)value;
            }
        }


        public IBaseControl MCRoot
        {
            get;
            set;
        }
        [TypeConverter(typeof(DataSourceConverter))]

        [Description("数据连接"), Category("数据")]
        public string MCDataSourceID
        {
            get;
            set;
        }
        public MCDataSource MCDataSource
        {
            get;
            set;
        }
        [Description("是否为数据库控件"), Category("数据")]
        public bool IsDbControl
        {
            get;
            set;
        }
        [Description("初始化SQL"), Category("数据")]
        public string InitDataSource
        {
            get;
            set;
        }
        [Description("执行SQL"), Category("数据")]
        public string ActionDataSource
        {
            get;
            set;
        }
        [Description("绑定数据源"), Category("数据")]
        public object BindDataSource
        {
            get;
            set;
        }
        [Description("操作类型"), Category("数据")]
        public DbOptionTypes DbOptionType
        {
            get;
            set;
        }
        [Description("是否可见"), Category("行为")]
        public bool MCVisible
        {
            get
            {
                return this._mcVisible;
            }
            set
            {
                this._mcVisible = value == null ? true : value;
                if (this.Site.DesignMode)
                {
                    this.Visible = true;
                }
                else
                {
                    this.Visible = this._mcVisible;
                }
            }
        }
        [Description("是否可用"), Category("行为")]
        public bool MCEnabled
        {
            get
            {
                return this._mcEnabled;
            }
            set
            {
                this._mcEnabled = value == null ? true : value;
                if (this.Site.DesignMode)
                {
                    this.Enabled = true;
                }
                else
                {
                    this.Enabled = this._mcEnabled;
                }
            }
        }

        public bool IsValid
        {
            get { return _isValid; }
            set { _isValid = value; }
        }

        public bool IsEventValid
        {
            get { return this._isEventValid; }
            set { this._isEventValid = value; }
        }

        #endregion
    }
}