using System;
using System.Windows.Forms;
using WinAPI;

namespace ryCommon
{
    /// <summary>
    /// 新模态窗体
    /// </summary>
    public class ModalForm
    {
        /// <summary>
        /// 设置窗体返回的结果值，并关闭窗体。
        /// </summary>
        /// <param name="frm">当前窗体</param>
        /// <param name="mr">模态变量</param>
        /// <param name="dr">防止</param>
        public static void SetDialogResult(Form frm, ModalForm mr,DialogResult dr)
        {
            if(mr==null)
            {
                if(frm.Modal)
                {
                    frm.DialogResult = dr;
                }
                else { frm.Close(); }
            }
            else
            { mr.Form_Result = dr; }
        }
        /// <summary>
        /// 窗体返回的结果事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public delegate void DialogResultHandler(object sender, DialogResult e);
        /// <summary>
        /// 当打开的窗体返回结果时激发
        /// </summary>
        public event DialogResultHandler OnDialogResult;
        DialogResult dg = DialogResult.None;
        /// <summary>
        /// 窗体返回的状态
        /// </summary>
        public DialogResult Form_Result
        {
           set { dg = value; sub_Form.Close();   }
           get { return dg; }
        }
        private Form parent;
        private Form sub_Form;
        /// <summary>
        /// 实例化类
        /// </summary>
        /// <param name="parent">父窗体,如果为null,则取桌面为父窗体</param>
        /// <param name="sub_Form"></param>
        public ModalForm(Form parent,Form sub_Form)
        {
            this.parent = parent;
            this.sub_Form = sub_Form;
        }
        /// <summary>
        /// 获取最顶级的父窗口
        /// </summary>
        /// <returns></returns>
        private Form GetTopParentForm()
        {
            var _parent = parent;
            while(_parent!=null)
            {
                if (_parent.Parent != null) { _parent = _parent.Parent.FindForm(); }
                else { break; }
            }
            return _parent;
        }
        /// <summary>
        /// 显示新模态窗体,会暂时禁用父窗体,关闭当前窗体后,父窗体会恢复。
        /// </summary>
        public void ShowModal()
        {
            Form parent_form = parent;
            if (parent_form != null) { parent_form.Enabled = false; }
            sub_Form.FormClosing += new FormClosingEventHandler((object t, FormClosingEventArgs e1) =>
            {
                if (parent_form != null){ parent_form.Enabled = true; }
                OnDialogResult?.Invoke(sub_Form, dg);
            });
            if (parent_form != null)
            {
                var screen = Screen.FromControl(parent_form);
                sub_Form.StartPosition = FormStartPosition.Manual;
                //User32.GetWindowRect(parent_form.Handle, out var rect);
                var x = parent_form.Left + (parent_form.Width - sub_Form.Width) / 2;
                var y = parent_form.Top + (parent_form.Height - sub_Form.Height) / 2;
                if ((x + sub_Form.Width) > screen.WorkingArea.X + screen.WorkingArea.Width)
                {
                    x = screen.WorkingArea.X + screen.WorkingArea.Width - sub_Form.Width;
                }
                if ((y + sub_Form.Height) > screen.WorkingArea.Y + screen.WorkingArea.Height)
                {
                    y = screen.WorkingArea.Y + screen.WorkingArea.Height - sub_Form.Height;
                }
                sub_Form.Location = new System.Drawing.Point(x, y);
                if (parent_form.TopMost) { sub_Form.TopMost = true; }
                sub_Form.Show(parent_form);
            }
            else
            {
                sub_Form.StartPosition = FormStartPosition.CenterScreen;
                sub_Form.Show();
            }
        }
        /// <summary>
        /// 只运行一个实例,打开后,所有窗体都依然有效。
        /// </summary>
        /// <returns></returns>
        public bool ShowOnce()
        {
            try
            {
                foreach (Form frm in Application.OpenForms)
                {
                    if (frm.GetType() == sub_Form.GetType())
                    {
                        frm.WindowState = FormWindowState.Normal;
                        frm.BringToFront();
                        return true;
                    }
                }
                sub_Form.StartPosition = FormStartPosition.CenterScreen;
                if (parent != null)
                {
                    if (parent.TopMost) { sub_Form.TopMost = true; }
                    sub_Form.Show(parent);
                }
                else { sub_Form.Show(); }
            }
            catch { }
            return false;
        }
        /// <summary>
        /// 显示窗体
        /// </summary>
        /// <param name="location">显示的位置</param>
        public void Show(FormLocation location)
        {
            int x = 0;
            int y = 0;
           switch(location)
            {
                case FormLocation.BottomLeft:
                    x =0;
                    y = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Size.Height - sub_Form.Height;
                    break;
                case FormLocation.BottomMiddle:
                    x = (System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Size.Width - sub_Form.Width)/2;
                    y = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Size.Height - sub_Form.Height;
                    break;
                case FormLocation.BottomRight:
                    x = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Size.Width - sub_Form.Width;
                    y = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Size.Height - sub_Form.Height;
                    break;
                case FormLocation.DesktopMiddle:
                    x = (System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Size.Width - sub_Form.Width)/2;
                    y = (System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Size.Height - sub_Form.Height)/2;
                    break;
                case FormLocation.ParentMiddle:
                    if (parent != null)
                    {
                        User32.GetWindowRect(parent.Handle, out var rect);
                        x = rect.Left + (rect.Right- rect.Left - sub_Form.Width) / 2;
                        y = rect.Top + (rect.Bottom- rect.Top - sub_Form.Height) / 2;
                    }
                    else
                    {
                        x = (System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Size.Width - sub_Form.Width) / 2;
                        y = (System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Size.Height - sub_Form.Height) / 2;
                    }
                    break;
                case FormLocation.TopLeft:
                    x =0;
                    y =0;
                    break;
                case FormLocation.TopMiddle:
                    x = (System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Size.Width - sub_Form.Width) / 2; ;
                    y = 0;
                    break;
                case FormLocation.TopRight:
                    x = (System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Size.Width - sub_Form.Width); ;
                    y = 0;
                    break;
            }
            sub_Form.Load += new EventHandler((object t, EventArgs e) =>
            {
                sub_Form.SetDesktopLocation(x, y);
                try
                {
                    sub_Form.BringToFront();
                }
                catch { }
            });
            if (parent != null) { if (parent.TopMost) { sub_Form.TopMost = true; } }
            sub_Form.Show();
        }
        /// <summary>
        /// 显示在父窗体中间
        /// </summary>
        public void Show()
        {
            Show(FormLocation.ParentMiddle);
        }
    }
    /// <summary>
    /// 窗体显示位置
    /// </summary>
    public enum FormLocation
    {
       /// <summary>
       /// 右上角
       /// </summary>
       TopLeft,
       /// <summary>
       /// 顶部中间
       /// </summary>
       TopMiddle,
       /// <summary>
       /// 顶部右边
       /// </summary>
       TopRight,
       /// <summary>
       /// 中间
       /// </summary>
       DesktopMiddle,
       /// <summary>
       /// 左下角
       /// </summary>
       BottomLeft,
       /// <summary>
       /// 底部中间
       /// </summary>
       BottomMiddle,
       /// <summary>
       /// 右下角
       /// </summary>
       BottomRight,
       /// <summary>
       /// 父窗体居中
       /// </summary>
       ParentMiddle,
    }
}
