using ryCommon;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace 开发辅助工具
{
    public partial class FrmMessageBox : Form
    {
        public FrmMessageBox()
        {
            InitializeComponent();
        }
        private void LoadPram()
        {
            ryCommon.Storage Stor = new ryCommon.Storage();
            Stor.LoadFromFile(Application.StartupPath+ "\\UserDb\\MessageBox.xml");
            Stor.SelectNodeBySet();
            CbbTitle.Text = Stor.GetAttrValue("Title");
            TxtContent.Text= Stor.GetAttrValue("Content");
            switch(Stor.GetAttrValue("Icon"))
            {
                case "MessageBoxIcon.None":
                    RbIconNone.Checked = true;
                    break;
                case "MessageBoxIcon.Error":
                    RbIconError.Checked = true;
                    break;
                case "MessageBoxIcon.Information":
                    RbIconInformation.Checked = true;
                    break;
                case "MessageBoxIcon.Question":
                    RbIconQuestion.Checked = true;
                    break;
                case "MessageBoxIcon.Warning":
                    RbIconWarning.Checked = true;
                    break;
            }
            switch (Stor.GetAttrValue("Btn"))
            {
                case "MessageBoxButtons.OK":
                    RbBtnOK.Checked = true;
                    break;
                case "MessageBoxButtons.OKCancel":
                    RbBtnOKCancel.Checked = true;
                    break;
                case "MessageBoxButtons.RetryCancel":
                    RbBtnRetryCancel.Checked = true;
                    break;
                case "MessageBoxButtons.AbortRetryIgnore":
                    RbBtnAbortRetryIgnore.Checked = true;
                    break;
                case "MessageBoxButtons.YesNo":
                    RbBtnYesNo.Checked = true;
                    break;
                case "MessageBoxButtons.YesNoCancel":
                    RbBtnYesNoCancel.Checked = true;
                    break;
            }
            switch (Stor.GetAttrValue("Default_Btn"))
            {
                case "MessageBoxDefaultButton.Button1":
                    RbDefaultButton1.Checked = true;
                    break;
                case "MessageBoxDefaultButton.Button2":
                    RbDefaultButton2.Checked = true;
                    break;
                case "MessageBoxDefaultButton.Button3":
                    RbDefaultButton3.Checked = true;
                    break;
            }
            ChkTopMost.Checked = Stor.GetAttrValue("TopMost",false);
        }
        private void BtnCreate_Click(object sender, EventArgs e)
        {
            string title = CbbTitle.Text.Replace("\r","").Replace("\n", "");
            string content = TxtContent.Text.Replace("\r", "\\r").Replace("\n", "\\n").Replace("\"", "\\\"");
            if(ChkTitleIsPram.Checked)
            {
                if (title == "") { MessageBox.Show("标题变量不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);return; }
                if (!Strings.IsEngOrNum(title.Replace("_","")) || title.Substring(0,1).IsInt()) { MessageBox.Show("变量只能是英文、下横杠或数字,并且不能以数字开头", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; }
            }
            if (ChkContentIsPram.Checked)
            {
                if (content == "") { MessageBox.Show("文本信息变量不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; }
                if (!Strings.IsEngOrNum(content.Replace("_", "")) || content.Substring(0, 1).IsInt()) { MessageBox.Show("文本信息只能是英文、下横杠或数字,并且不能以数字开头", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; }
            }
            MessageBoxButtons btn = MessageBoxButtons.OK;
            MessageBoxIcon icon = MessageBoxIcon.None;
            MessageBoxDefaultButton default_btn = MessageBoxDefaultButton.Button1;
            MessageBoxOptions option = MessageBoxOptions.DefaultDesktopOnly;
            List<DialogResult> dg = new List<DialogResult>() ;
            string MessageBoxButtons_str = "";
            string MessageBoxIcon_str = "";
            string MessageBoxDefaultButton_str = "";
            string option_str = "";
            List<string> DialogResult_str =new List<string>();
            #region 对话框图标设置
            if (RbIconError.Checked) { icon = MessageBoxIcon.Error; MessageBoxIcon_str = "MessageBoxIcon.Error"; }
            else if (RbIconInformation.Checked) { icon = MessageBoxIcon.Information; MessageBoxIcon_str = "MessageBoxIcon.Information"; }
            else if (RbIconNone.Checked) { icon = MessageBoxIcon.None; MessageBoxIcon_str = "MessageBoxIcon.None"; }
            else if (RbIconQuestion.Checked) { icon = MessageBoxIcon.Question; MessageBoxIcon_str = "MessageBoxIcon.Question"; }
            else if (RbIconWarning.Checked) { icon = MessageBoxIcon.Warning; MessageBoxIcon_str = "MessageBoxIcon.Warning"; }
            #endregion
            #region 对话框按钮生成
            if (RbBtnOK.Checked) { btn = MessageBoxButtons.OK; MessageBoxButtons_str = "MessageBoxButtons.OK"; }
            else if (RbBtnOKCancel.Checked) { btn = MessageBoxButtons.OKCancel; MessageBoxButtons_str = "MessageBoxButtons.OKCancel"; }
            else if (RbBtnRetryCancel.Checked) { btn = MessageBoxButtons.RetryCancel; MessageBoxButtons_str = "MessageBoxButtons.RetryCancel"; }
            else if (RbBtnAbortRetryIgnore.Checked) { btn = MessageBoxButtons.AbortRetryIgnore; MessageBoxButtons_str = "MessageBoxButtons.AbortRetryIgnore"; }
            else if (RbBtnYesNo.Checked) { btn = MessageBoxButtons.YesNo; MessageBoxButtons_str = "MessageBoxButtons.YesNo"; }
            else if (RbBtnYesNoCancel.Checked) { btn = MessageBoxButtons.YesNoCancel; MessageBoxButtons_str = "MessageBoxButtons.YesNoCancel"; }
            #endregion
            #region 返回值
            if (ChkResultAbort.Checked && ChkResultAbort.Enabled) { dg.Add(DialogResult.Abort); DialogResult_str.Add("DialogResult.Abort"); }
            if (ChkResultCancel.Checked && ChkResultCancel.Enabled) { dg.Add(DialogResult.Cancel); DialogResult_str.Add("DialogResult.Cancel"); }
            if (ChkResultIgnore.Checked && ChkResultIgnore.Enabled) { dg.Add(DialogResult.Ignore); DialogResult_str.Add("DialogResult.Ignore"); }
            if (ChkResultNo.Checked && ChkResultNo.Enabled) { dg.Add(DialogResult.No); DialogResult_str.Add("DialogResult.No"); }
            if (ChkResultOK.Checked && ChkResultOK.Enabled) { dg.Add(DialogResult.OK); DialogResult_str.Add("DialogResult.OK"); }
            if (ChkResultRetry.Checked && ChkResultRetry.Enabled) { dg.Add(DialogResult.Retry); DialogResult_str.Add("DialogResult.Retry"); }
            if (ChkResultYes.Checked && ChkResultYes.Enabled) { dg.Add(DialogResult.Yes); DialogResult_str.Add("DialogResult.Yes"); }
            #endregion
            #region 默认按钮
            if (RbDefaultButton1.Checked) { default_btn = MessageBoxDefaultButton.Button1; MessageBoxDefaultButton_str = "MessageBoxDefaultButton.Button1"; }
            else if (RbDefaultButton2.Checked) { default_btn = MessageBoxDefaultButton.Button2; MessageBoxDefaultButton_str = "MessageBoxDefaultButton.Button2"; }
            else if (RbDefaultButton3.Checked) { default_btn = MessageBoxDefaultButton.Button3; MessageBoxDefaultButton_str = "MessageBoxDefaultButton.Button3"; }
            #endregion
            if (ChkTopMost.Checked) { option = MessageBoxOptions.DefaultDesktopOnly;option_str = "MessageBoxOptions.DefaultDesktopOnly"; }
            MessageBox.Show(content.Replace("\\r","\r").Replace("\\n", "\n"), title, btn, icon, default_btn, option);
            string create_str = "MessageBox.Show(\""+ content + "\",\""+ title + "\"";
            if (ChkTitleIsPram.Checked && !ChkContentIsPram.Checked)
            {
                create_str = "MessageBox.Show(\"" + content + "\"," + title;
            }
            else if (!ChkTitleIsPram.Checked && ChkContentIsPram.Checked)
            {
                create_str = "MessageBox.Show(" + content + ",\"" + title + "\"";
            }
            else if (ChkTitleIsPram.Checked && ChkContentIsPram.Checked)
            {
                create_str = "MessageBox.Show(" + content + "," + title;
            }
            if(option_str != "")
            { create_str += "," + MessageBoxButtons_str + "," + MessageBoxIcon_str + "," + MessageBoxDefaultButton_str + "," + option_str + ")"; }
            else if (MessageBoxDefaultButton_str != "MessageBoxDefaultButton.Button1" && option_str == "")
            { create_str += "," + MessageBoxButtons_str + "," + MessageBoxIcon_str + "," + MessageBoxDefaultButton_str + ")"; }
            else if (MessageBoxDefaultButton_str == "MessageBoxDefaultButton.Button1" && option_str == "")
            { create_str += "," + MessageBoxButtons_str + "," + MessageBoxIcon_str + ")"; }
            else if (MessageBoxButtons_str == "MessageBoxButtons.OK" && MessageBoxIcon_str == "MessageBoxIcon.None" && MessageBoxDefaultButton_str == "MessageBoxDefaultButton.Button1" && option_str == "")
            { create_str += ")"; }
            else
            {
                { create_str += ")"; }
            }
            if (dg.Count == 0) { create_str += ";"; }
            else
            {
                create_str = "switch(" + create_str + ")";
                create_str += "\r\n{";
                for (int i = 0; i < DialogResult_str.Count; i++)
                {
                    create_str += "\r\n   case "+ DialogResult_str[i]+ ":\r\n        break;";
                }
                create_str += "\r\n}";
            }
            ryCommon.RyFiles.CopyToClip(create_str);
            ryCommon.Storage Stor = new ryCommon.Storage();
            Stor.LoadFromFile(Application.StartupPath + "\\UserDb\\MessageBox.xml");
            Stor.SelectNodeBySet();
            Stor.SetAttrValue("Title", CbbTitle.Text);
            Stor.SetAttrValue("Content", TxtContent.Text);
            Stor.SetAttrValue("Icon",MessageBoxIcon_str);
            Stor.SetAttrValue("Btn", MessageBoxButtons_str);
            Stor.SetAttrValue("Default_Btn", MessageBoxDefaultButton_str);
            Stor.SetAttrValue("TopMost", ChkTopMost.Checked);
            Stor.SaveToFile(Application.StartupPath + "\\UserDb\\MessageBox.xml");
            this.DialogResult=DialogResult.OK;
        }
        private void InitResult()
        {
            for (int i = 0; i < GroupResult.Controls.Count; i++)
            {
                if (GroupResult.Controls[i] is CheckBox)
                {
                    GroupResult.Controls[i].Enabled = false;
                }
            }
        }
        private void RbBtnOK_CheckedChanged(object sender, EventArgs e)
        {
            InitResult();
        }

        private void RbBtnOKCancel_CheckedChanged(object sender, EventArgs e)
        {
            InitResult();
            RadioButton rb_btn = (RadioButton)sender;
            if (rb_btn.Checked) { ChkResultOK.Enabled = true;ChkResultCancel.Enabled = true; }
        }

        private void RbBtnYesNo_CheckedChanged(object sender, EventArgs e)
        {
            InitResult();
            RadioButton rb_btn = (RadioButton)sender;
            if (rb_btn.Checked) { ChkResultYes.Enabled = true; ChkResultNo.Enabled = true; }
        }

        private void RbBtnYesNoCancel_CheckedChanged(object sender, EventArgs e)
        {
            InitResult();
            RadioButton rb_btn = (RadioButton)sender;
            if (rb_btn.Checked) { ChkResultYes.Enabled = true; ChkResultNo.Enabled = true;ChkResultCancel.Enabled = true; }
        }

        private void RbBtnRetryCancel_CheckedChanged(object sender, EventArgs e)
        {
            InitResult();
            RadioButton rb_btn = (RadioButton)sender;
            if (rb_btn.Checked) { ChkResultRetry.Enabled = true; ChkResultCancel.Enabled = true; }
        }

        private void RbBtnAbortRetryIgnore_CheckedChanged(object sender, EventArgs e)
        {
            InitResult();
            RadioButton rb_btn = (RadioButton)sender;
            if (rb_btn.Checked) { ChkResultAbort.Enabled = true; ChkResultRetry.Enabled = true; ChkResultIgnore.Enabled = true; }
        }

        private void FrmMessageBox_Load(object sender, EventArgs e)
        {
            LoadPram();
        }
    }
}
