using ryCommonDb;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using 开发辅助工具.Manager;

namespace ryPaiban.Model
{
    [Description("子标签右键菜单")]
    public partial class MenuRight : System.Windows.Forms.ContextMenuStrip
    {
        public MenuRight()
        {
            InitializeComponent();
            LoadMenu();
        }
        private DockContent _SourceContent = null;
        [Description("菜单所属的子窗体")]
        public DockContent SourceContent
        {
            get { return _SourceContent; }
            set
            {
                if (_SourceContent != value)
                {
                    if (_SourceContent != null)
                    {
                        _SourceContent.FormClosing -= _SourceContent_FormClosing;
                    }
                    _SourceContent = value;
                    if (value != null)
                    {
                        _SourceContent.FormClosing += _SourceContent_FormClosing;
                        _SourceContent.TabPageContextMenuStrip = this;
                    }
                }
            }

        }

        private void _SourceContent_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (Itrycn_Db.IsCloseConfirm)
            {
                switch (MessageBox.Show("是否确定要关闭？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question))
                {
                    case DialogResult.Cancel:
                        e.Cancel = true;
                        break;
                }
            }
        }
        ToolStripMenuItem menu_start = null;
        private void LoadMenu()
        {
            base.Items.Add("关闭本标签").Click += OnClose_Click;
            base.Items.Add("除此之外全部关闭").Click += OnCloseAllNoMe_Click;
            base.Items.Add("临时重命名").Click += OnRename_Click;
            menu_start = (ToolStripMenuItem)base.Items.Add("软件启动时自动打开");
            menu_start.Click += OnStart_Click;
            base.Opening += MenuRight_Opening;
        }

        private void MenuRight_Opening(object sender, CancelEventArgs e)
        {
            DataProvider mydb = new DataProvider();
            IDbInterface db = Itrycn_Db.CreateDataProvider(Itrycn_Db.dataType);
            if (db.ConnDb(Itrycn_Db.User_SQLConn) == 1)
            {
                RyQuickSQL mySQL = new RyQuickSQL("Starts");
                mySQL.AddField("Addr", SourceContent.GetType().ToString());
                DataSet ds = db.ReadData("select * from Starts where Addr=@Addr", mySQL);
                menu_start.Checked = mydb.HaveData(ds);
            }
            db.Free();
        }

        private void OnStart_Click(object sender, EventArgs e)
        {
            if (SourceContent == null) { return; }
            ToolStripMenuItem item = (ToolStripMenuItem)sender;
            item.Checked = !item.Checked;
            DataProvider mydb = new DataProvider();
            IDbInterface db = Itrycn_Db.CreateDataProvider(Itrycn_Db.dataType);
            if (db.ConnDb(Itrycn_Db.User_SQLConn) == 1)
            {
                RyQuickSQL mySQL = new RyQuickSQL("Starts");
                mySQL.AddField("Name", SourceContent.Text);
                mySQL.AddField("Addr", SourceContent.GetType().ToString());
                mySQL.AddField("EditTime", DateTime.Now);
                if (item.Checked)
                {
                    DataSet ds = db.ReadData("select * from Starts where Addr=@Addr", mySQL);
                    if (!mydb.HaveData(ds))
                    {
                        mySQL.AddField("AddTime", DateTime.Now);
                        db.ExecuteNonQuery(mySQL.GetInsertSQL(),mySQL);
                    }
                }
                else
                {
                    db.ExecuteNonQuery("delete from Starts where Addr=@Addr", mySQL);
                }
            }
            db.Free();
        }
        private void OnClose_Click(object sender, EventArgs e)
        {
            if (SourceContent == null) { return; }
            SourceContent.Close();
        }
        private void OnCloseAllNoMe_Click(object sender, EventArgs e)
        {
            if (SourceContent == null) { return; }
            switch (MessageBox.Show("是否确定要关闭？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question))
            {
                case DialogResult.Cancel:
                    return;
            }
            Itrycn_Db.IsCloseConfirm = false;
            DockContentCollection contents = SourceContent.DockPanel.Contents;
            int num = 0;
            while (num < contents.Count)
            {
                if (contents[num].DockHandler.DockState == DockState.Document && SourceContent.DockPanel.ActiveContent != contents[num])
                {
                    contents[num].DockHandler.Close();
                }
                else
                {
                    num++;
                }
            }
            Itrycn_Db.IsCloseConfirm = true;
        }
        private void OnRename_Click(object sender, EventArgs e)
        {
            if (SourceContent == null) { return; }
            SuperDesign.Controls.FrmTitle frm = new SuperDesign.Controls.FrmTitle();
            frm.TxtTitle.Text = SourceContent.Text;
            frm.Icon = SourceContent.Icon;
            if(frm.ShowDialog()==DialogResult.OK)
            {
                SourceContent.Text = frm.TxtTitle.Text;
            }
            frm.Dispose();
        }
        public MenuRight(IContainer container)
        {
            container.Add(this); 
            InitializeComponent();
            LoadMenu();
        }
    }
}
