using System;
using System.Drawing;
using System.Windows.Forms;
using DevExpress.XtraBars;
using DevExpress.XtraTabbedMdi;
using ProjectBase.Data.BaseDAL;
using ProjectBase.Data.Logs;
using SIASUN.Autopilot.BLL;
using SIASUN.Autopilot.Device;

namespace SIASUN.Autopilot.View
{
    public partial class FormMain : DevExpress.XtraBars.Ribbon.RibbonForm
    {
        //用来第一次创建动态菜单
        private SIASUN.Autopilot.View.RibbonPageHelper.RibbonPageHelper ribbonHelper = null;

        public FormMain()
        {
            InitializeComponent();
        }

        private void FormMain_Load(object sender, EventArgs e)
        {
            this.ribbonControl.Minimized = true;
            InitRibbonPage();
        }

        /// <summary>
        /// 右键弹出菜单
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void xtraTabbedMdiManager1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button != MouseButtons.Right)
                return;

            DevExpress.XtraTab.ViewInfo.BaseTabHitInfo hi = xtraTabbedMdiManager1.CalcHitInfo(new Point(e.X, e.Y));
            if (hi.HitTest == DevExpress.XtraTab.ViewInfo.XtraTabHitTest.PageHeader)
            {
                popupMenu1.ShowPopup(Cursor.Position);
            }
        }

        /// <summary>
        /// 关闭当前窗口
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pop_CloseCurrent_ItemClick(object sender, ItemClickEventArgs e)
        {
            XtraMdiTabPage page = xtraTabbedMdiManager1.SelectedPage;
            if (page != null && page.MdiChild != null)
            {
                page.MdiChild.Close();
            }
        }

        /// <summary>
        /// 关闭所有窗口
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pop_CloseAll_ItemClick(object sender, ItemClickEventArgs e)
        {
            foreach (Form form in this.MdiChildren)
            {
                form.Close();
                if (form != null && !form.IsDisposed)
                {
                    form.Dispose();
                }
            }
        }

        /// <summary>
        /// 关闭其他窗口
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pop_CloseOther_ItemClick(object sender, ItemClickEventArgs e)
        {
            XtraMdiTabPage selectedPage = xtraTabbedMdiManager1.SelectedPage;
            Type currentType = selectedPage.MdiChild.GetType();

            for (int i = xtraTabbedMdiManager1.Pages.Count - 1; i >= 0; i--)
            {
                XtraMdiTabPage page = xtraTabbedMdiManager1.Pages[i];
                if (page != null && page.MdiChild != null)
                {
                    Form form = page.MdiChild;
                    if (form.GetType() != currentType)
                    {
                        form.Close();
                        if (form != null && !form.IsDisposed)
                        {
                            form.Dispose();
                        }
                    }
                }
            }
        }            

        /// <summary>
        /// 退出系统
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barBtnItem_Quit_ItemClick(object sender, ItemClickEventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// 退出系统
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FormMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (DevExpress.XtraEditors.XtraMessageBox.Show("您确定要退出吗？", "新松自动驾驶辅助系统", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
            {
                //e.Cancel = true;
                //task.Stop();
                //this.notifyIcon1.Visible = false;
                //this.Close();
            }
            else
            {
                e.Cancel = true;
            }
        }

        /// <summary>
        /// 动态创建界面菜单对象
        /// </summary>
        private void InitRibbonPage()
        {
            try
            {
                //this.barStaticItem_User.Caption = string.Format("{0}，欢迎登录", SysEnvironment.CurrentLoginName);
                //动态创建界面菜单对象(防止重复构建）
                if (ribbonHelper == null)
                {
                    ribbonHelper = new SIASUN.Autopilot.View.RibbonPageHelper.RibbonPageHelper(this, ref this.ribbonControl);
                    ribbonHelper.AddPages();
                }
            }
            catch (Exception ex)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("获取界面菜单失败。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                LogHelper.log.Error(string.Format("初始化界面菜单出现错误：{0}", ex.ToString()));
            }

        }

        /// <summary>
        /// 初始化地图按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barBtn_InitMap_ItemClick(object sender, ItemClickEventArgs e)
        {
            FormInitMap form = new FormInitMap();
            form.ShowDialog();
        }
    }
}