using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ICSharpCode.Core;
using Mesnac.Basic;

namespace Mesnac.Gui.Edit.Dialog
{
    public partial class FrmDataItem : Form
    {
        private int level = 2;          //节点默认展开级别
        private int selectlevel = 3;   //默认检索级别
        private string caption = StringParser.Parse(ResourceService.GetString("Mesnac_Dialog_Caption"));

        public FrmDataItem()
        {
            InitializeComponent();
            this.InitUIMethod();
            this.InitMethod();
        }

        /// <summary>
        /// 初始化界面元素值
        /// </summary>
        public void InitUIMethod()
        {
            this.Text = StringParser.Parse(ResourceService.GetString("Mesnac_Dialog_FrmDataItem_Text"));
            this.label1.Text = StringParser.Parse(ResourceService.GetString("Mesnac_Dialog_FrmDataItem_label1"));
            this.btnOk.Text = StringParser.Parse(ResourceService.GetString("Mesnac_Dialog_btnOK"));
            this.btnCancel.Text = StringParser.Parse(ResourceService.GetString("Mesnac_Dialog_btnCancel"));
            this.btnRefresh.Text = StringParser.Parse(ResourceService.GetString("Mesnac_Dialog_FrmDataItem_btnRefresh"));
        }
        /// <summary>
        /// 初始化方法
        /// </summary>
        public void InitMethod()
        {
            //防止跨线程访问控件报告异常
            if (this.InvokeRequired)
            {
                this.BeginInvoke(new Mesnac.Basic.CallBackDelegate(this.InitMethod));
                return;
            }

            try
            {
                TreeNode root = new TreeNode();
                TreeNode tmproot = DataSourceFactory.Instance.Root.Clone() as TreeNode;  //克隆根节点
                root.Text = tmproot.Text;
                this.IniTree(root, tmproot, txtFilter.Text);
                this.treeView1.Nodes.Clear();
                this.treeView1.Nodes.Add(root);
                foreach (TreeNode n in this.treeView1.Nodes)
                {
                    this.Expand(n, this.level);
                }
            }
            catch (Exception ex)
            {
                ICSharpCode.Core.LoggingService<FrmDataItem>.Error(ex.Message);
            }
        }
        //列表检索
        private void IniTree(TreeNode node, TreeNode cur, string Filter)
        {

            foreach (TreeNode actionnode in cur.Nodes)
            {
                if (actionnode.Parent.Level <= this.selectlevel)
                {
                    if (actionnode.Level == selectlevel && actionnode.Text.IndexOf(Filter, StringComparison.OrdinalIgnoreCase) >= 0)
                    {
                        TreeNode child = new TreeNode();
                        child.Name = actionnode.Text;
                        child.ToolTipText = actionnode.ToolTipText;
                        child.Text = actionnode.Text;
                        node.Nodes.Add(child);
                        this.IniTree(child, actionnode, Filter);
                    }
                    else if (actionnode.Level != selectlevel)
                    {
                        TreeNode child = new TreeNode();
                        child.Name = actionnode.Text;
                        child.ToolTipText = actionnode.ToolTipText;
                        child.Text = actionnode.Text;
                        node.Nodes.Add(child);
                        this.IniTree(child, actionnode, Filter);
                    }
                }
            }
        }
        //展开节点
        public void Expand(TreeNode parent, int level)
        {
            if (parent.Level <= level)
            {
                parent.Expand();
                foreach (TreeNode node in parent.Nodes)
                {
                    if (node.Level <= level)
                    {
                        node.Expand();
                    }
                    this.Expand(node, level);
                }
            }
        }
        /// <summary>
        /// 选择节点
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (e.Node.Level <= this.level)
            {
                //MessageBox.Show("请选择表、视图或字段!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.lblInfo.Text = StringParser.Parse(ResourceService.GetString("Mesnac_Dialog_FrmDataItem_lblInfo")); //"请选择表、视图或字段!";
                return;
            }
            else
            {
                this.lblInfo.Text = String.Empty;
            }
            if (e.Node.Level == 3)      //选择的数据对象(表、视图)
            {
                this.txtCurrent.Text = String.Format("[{0}].[{1}]", e.Node.Parent.Parent.Text, e.Node.Text);
            }
            if (e.Node.Level == 4)      //选择的字段
            {
                this.txtCurrent.Text = String.Format("[{0}].[{1}].[{2}]", e.Node.Parent.Parent.Parent.Text, e.Node.Parent.Text, e.Node.Text);
            }
        }
        /// <summary>
        /// 数据项
        /// </summary>
        public string DataItem
        {
            get { return this.txtCurrent.Text; }
        }
        /// <summary>
        /// 数据刷新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnRefresh_Click(object sender, EventArgs e)
        {
            Mesnac.Gui.Edit.ViewContent.DisplayUtil<Mesnac.Gui.Edit.Pad.ProjectWindow>.ResetProjectDataSourceFactory(this.InitMethod);
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(this.txtCurrent.Text))
            {
                string msg = StringParser.Parse(ResourceService.GetString("Mesnac_Dialog_FrmDataItem_Msg1"));       //"请选择或输入要绑定的数据节点!格式:[数据源].[数据对象]/[数据源].[数据对象].[数据列]"
                MessageBox.Show(msg, caption, MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.txtCurrent.Focus();
                return;
            }
            this.DialogResult = DialogResult.OK;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void txtFilter_TextChanged(object sender, EventArgs e)
        {
            this.InitMethod();

        }
    }
}