using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace AIProofread.Controls
{
    public partial class FormMessage : BaseWinForm
    {
        public const int WM_NCLBUTTONDOWN = 0xA1;
        public const int HT_CAPTION = 0x2;

        [DllImport("user32.dll")]
        public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
        [DllImport("user32.dll")]
        public static extern bool ReleaseCapture();

        /// <summary>
        /// 初始化宽度
        /// </summary>
        private const int INIT_WIDTH = 380;

        public FormMessage()
        {
            InitializeComponent();
        }

        private void SetMessage(string message)
        {
            LblMeesage.Text = message;
        }
        private string currentConfirmText;
        private string currentConfirmAction;
        private void SetConfirmText(string confirmText)
        {
            currentConfirmText = confirmText;
            if (confirmText == "proofread")
            {
                confirmText = "重新校对";
            }
            BtnConfirm.Text = confirmText;
            BtnConfirm.Visible = true;
            BtnConfirm.Enabled = true;
        }

        private void HideConfirm()
        {
            BtnConfirm.Visible = false;
            BtnConfirm.Enabled = false;
            BtnClose.Location = new System.Drawing.Point((
                (this.Width - BtnClose.Width) / 2
                ), BtnClose.Location.Y);
        }

        /// <summary>
        /// 计算缩放
        /// </summary>
        /// <param name="size"></param>
        /// <returns></returns>
        private int ScaleSize(int size)
        {
            return (int)(size * this.Width * 1.0 / INIT_WIDTH);
        }

        private void ResetButtons()
        {
            BtnClose.Location = new System.Drawing.Point(ScaleSize(200), BtnClose.Location.Y);
            BtnConfirm.Location = new System.Drawing.Point(ScaleSize(60), BtnConfirm.Location.Y);
            BtnConfirm.Visible = true;
            BtnConfirm.Enabled = true;
        }
        
        public void HideCloseBtn()
        {
            BtnClose.Visible = false;
            LblMeesage.Location = new System.Drawing.Point(LblMeesage.Location.X, LblMeesage.Location.Y + ScaleSize(30));
        }

        public static FormMessage ShowMessage(string message, int closeDelay = 3000)
        {
            FormMessage formMessage = new FormMessage();
            formMessage.SetMessage(message);
            formMessage.HideConfirm();
            // 先置顶
            formMessage.TopMost = true;
            formMessage.Show();
            return formMessage;
        }
        public static DialogResult ShowMessage(string message, string confirmText = "确认", string confirmAction = null)
        {
            FormMessage formMessage = new FormMessage();
            formMessage.SetMessage(message);
            formMessage.currentConfirmAction = confirmAction;

            if (string.IsNullOrEmpty(confirmText))
            {
                formMessage.HideConfirm();
            }
            else
            {
                formMessage.BtnConfirm.Text = confirmText;
                formMessage.ResetButtons();
            }
            return formMessage.ShowDialog();
        }

        private void BtnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void BtnConfirm_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(currentConfirmAction))
            {
                string time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                Globals.ThisAddIn.SendMessageToWeb(currentConfirmAction, time);
            }
            this.Close();
        }

        private void IconClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void HanleMouseDown()
        {
            ReleaseCapture();
            SendMessage(this.Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
        }

        private void label1_MouseDown(object sender, MouseEventArgs e)
        {
            HanleMouseDown();
        }

        private void panel1_MouseDown(object sender, MouseEventArgs e)
        {
            HanleMouseDown();
        }
    }
}
