using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using UAS_MES_NEW.DataOperate;
using UAS_MES_NEW.Entity;
using UAS_MES_NEW.PublicMethod;
using UAS_MES_NEW.PublicForm;

namespace UAS_MES_NEW.CustomControl
{
    public partial class HeadBar : UserControl
    {

        //需要关闭前提示窗口的在这里加
        private List<string> NeedConfirm = new List<string>() { "Main" };
        [DllImport("user32.dll")]
        public static extern bool ReleaseCapture();
        [DllImport("user32.dll")]
        public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
        public const int WM_SYSCOMMAND = 0x0112;
        public const int SC_MOVE = 0xF010;
        public const int HTCAPTION = 0x0002;

        //定义委托
        public delegate void OnHeadBarClick(object sender, EventArgs e);
        //定义事件
        public event OnHeadBarClick HeadBarDoubleClick;

        string Title1;

        public string Title
        {
            get
            {
                return Title1;
            }

            set
            {
                Title1 = value;
            }
        }

        public HeadBar()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 用于对窗体的关闭,判断主窗体的时候退出程序,非主窗体时关闭该窗体,同时回收内存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void CloseWindow_Click(object sender, EventArgs e)
        {
            //判断是否需要关闭前的提示
            if (NeedConfirm.Contains(this.FindForm().Name))
            {
                string logout_confirm = MessageBox.Show(this.ParentForm, "是否退出程序", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question).ToString();
                if (logout_confirm == "Yes")
                {
                    this.FindForm().Close();
                    Application.Exit();
                }
            }
            else
            {
                this.FindForm().Close();
            }
        }

        private void panel1_Click(object sender, EventArgs e)
        {
            HeadBarDoubleClick?.Invoke(sender, new EventArgs());
        }

        private void MinWindow_Click(object sender, EventArgs e)
        {
            this.FindForm().WindowState = FormWindowState.Minimized;
        }

        private void HeadBar_Load(object sender, EventArgs e)
        {
            TitleLabel.Text = Title1;
            if (!NeedConfirm.Contains(this.FindForm().Name))
            {
                LoginOut.Visible = false;
                Change_psw.Visible = false;
                UpperCollection.Visible = false;
            }
        }

        private void LoginOut_MouseEnter(object sender, EventArgs e)
        {
            this.Cursor = Cursors.Hand;
        }

        private void LoginOut_MouseLeave(object sender, EventArgs e)
        {
            this.Cursor = Cursors.Default;
        }

        private void Change_psw_Click(object sender, EventArgs e)
        {
            ChangePwd chagepwd = new ChangePwd();
            BaseUtil.SetFormCenter(chagepwd);
            chagepwd.ShowDialog();
        }

        private void HeadBar_BackColorChanged(object sender, EventArgs e)
        {
            Change_psw.BackColor = BackColor;
            LoginOut.BackColor = BackColor;
        }

        private void LoginOut_Click(object sender, EventArgs e)
        {
            //判断是否需要关闭前的提示
            if (NeedConfirm.Contains(this.FindForm().Name))
            {
                string logout_confirm = MessageBox.Show(this.ParentForm, "退出登录?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question).ToString();
                if (logout_confirm == "Yes")
                {
                    //注销的时候切换回默认数据库
                    SystemInf.ConnectionString = Properties.Settings.Default.Properties["MES"].DefaultValue.ToString();
                    DataHelper.DBConnectionString = SystemInf.ConnectionString;
                    //清除上个用户的权限信息
                    SystemInf.Caller.Clear();
                    //清除已经打开过的窗口的信息
                    AccordionMenu.NavagationBar.OpenedFormName.Clear();
                    //注销的时候将除了登陆窗口之外的全部窗口关闭
                    for (int i = 0; i < Application.OpenForms.Count; i++)
                    {
                        if (Application.OpenForms[i].Name != "Login")
                            Application.OpenForms[i].Close();
                    }
                    this.FindForm().Hide();
                    Login login = new Login();
                    login.ShowDialog();
                    this.FindForm().Close();
                }
            }
        }

        private void UpperCollection_Toggled(object sender, EventArgs e)
        {
            SystemInf.UpperCollection = UpperCollection.IsOn;
        }
    }
}