using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace RySkins
{
   public static class Msg
    {
        /// <summary>
        /// 显示对话框
        /// </summary>
        /// <param name="msgText"></param>
        /// <param name="title"></param>
        /// <returns></returns>
        public static DialogResult ShowMsg(string msgText, string title)
        {
            return ShowMsg(msgText, title, MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        /// <summary>
        /// 显示对话框
        /// </summary>
        /// <param name="msgText"></param>
        /// <param name="title"></param>
        /// <param name="button"></param>
        /// <returns></returns>
        public static DialogResult ShowMsg(string msgText, string title, MessageBoxButtons button)
        {
            return ShowMsg(msgText, title, button, MessageBoxIcon.Information);
        }
        /// <summary>
        /// 显示对话框
        /// </summary>
        /// <param name="msgText"></param>
        /// <param name="title"></param>
        /// <param name="button"></param>
        /// <param name="Icon"></param>
        /// <param name="defaultButton"></param>
        /// <returns></returns>
        public static DialogResult ShowMsg(string msgText, string title, MessageBoxButtons button, MessageBoxIcon Icon,MessageBoxDefaultButton defaultButton)
        {
            return ShowMsg(msgText, title, button, Icon);
        }
        /// <summary>
        /// 显示对话框
        /// </summary>
        /// <param name="msgText"></param>
        /// <param name="title"></param>
        /// <param name="button"></param>
        /// <param name="Icon"></param>
        /// <returns></returns>
        public static DialogResult ShowMsg(string msgText, string title, MessageBoxButtons button, MessageBoxIcon Icon)
        {
#pragma warning disable CA2000 // 丢失范围之前释放对象
            FrmMessageBox frm = new FrmMessageBox
            {
                Text = title
            };
#pragma warning restore CA2000 // 丢失范围之前释放对象
            frm.lblMsg.Text = msgText;
            frm.lblMsg.UpdateInfo();
            frm.ResultDg = button;
            switch (button)
            {
                case MessageBoxButtons.OK:
                    frm.btnYes.Location = new System.Drawing.Point(frm.ClientSize.Width - frm.btnYes.Width - 8, frm.ClientSize.Height - frm.btnYes.Height - 8);
                    frm.btnCancel.Visible = false;
                    break;
                case MessageBoxButtons.OKCancel:
                case MessageBoxButtons.YesNo:
                    frm.btnCancel.Location = new System.Drawing.Point(frm.ClientSize.Width - frm.btnYes.Width - 8, frm.ClientSize.Height - frm.btnYes.Height - 8);
                    frm.btnYes.Location = new System.Drawing.Point(frm.btnCancel.Left - frm.btnYes.Width - 8, frm.btnCancel.Top);
                    break;
                case MessageBoxButtons.RetryCancel:
                    frm.btnCancel.Location = new System.Drawing.Point(frm.ClientSize.Width - frm.btnYes.Width - 8, frm.ClientSize.Height - frm.btnYes.Height - 8);
                    frm.btnYes.Location = new System.Drawing.Point(frm.btnCancel.Left - frm.btnYes.Width - 8, frm.btnCancel.Top);
                    frm.btnYes.Text = "重试";
                    break;
                default:
                    frm.btnYes.Location = new System.Drawing.Point(frm.ClientSize.Width - frm.btnYes.Width - 8, frm.ClientSize.Height - frm.btnYes.Height - 8);
                    frm.btnCancel.Visible = false;
                    break;
            }
            frm.TopMost = true;
            var handle= WinAPI.User32.GetActiveWindow();
            var have_owner = false;
            for (int i = 0; i < Application.OpenForms.Count; i++)
            {
                if(Application.OpenForms[i].Handle==handle)
                {
                    var owner = Application.OpenForms[i];
                    frm.Location = new System.Drawing.Point(owner.Left + (owner.Width - frm.Width) / 2, owner.Top + (owner.Height - frm.Height) / 2);
                    have_owner = true;
                }
            }
            //WinAPI.User32.GetWindowRect(handle,out var rectangle);
            //var title2= ryCommon.RyForm.GetWinText(handle);
            //frm.Location = new System.Drawing.Point(rectangle.X + (rectangle.Width - frm.Width) / 2, rectangle.Y + (rectangle.Height - frm.Height) / 2);
            if (!have_owner) { frm.StartPosition = FormStartPosition.CenterScreen; }
            return frm.ShowDialog();
        }
    }
}
