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;
using ICSharpCode.Core;
using Mesnac.Core;
using Mesnac.Gui.Run.Global;

namespace Mesnac.Gui.Run.Dialog
{
    public partial class FrmRunConfigMenuItem : Form
    {
        private int _type = 0;              //操作类型,0为添加,1为修改
        private string _parentText = String.Empty;         //父级节点名称

        private string caption = StringParser.Parse(ResourceService.GetString("Mesnac_Dialog_Caption")); //提示

        public FrmRunConfigMenuItem()
        {
            InitializeComponent();
            InitUIMethod();
        }

        public FrmRunConfigMenuItem(int type, string parentText, string nodeText)
        {
            InitializeComponent();

            this._type = type;
            this._parentText = parentText;
            this.txtNodeText.Text = nodeText;

            InitUIMethod();
        }

        public FrmRunConfigMenuItem(int type, string parentText, string nodeText , string shortCut)
        {
            InitializeComponent();

            this._type = type;
            this._parentText = parentText;
            this.txtNodeText.Text = nodeText;
            this.txtShortCut.Text = shortCut;

            InitUIMethod();
        }

        public FrmRunConfigMenuItem(int type, string parentText, string nodeText, string shortCut, Mesnac.Basic.FormShowType showType)
        {
            InitializeComponent();

            this._type = type;
            this._parentText = parentText;
            this.txtNodeText.Text = nodeText;
            this.txtShortCut.Text = shortCut;
            this.cbShowType.SelectedIndex = (int)showType - 1;
            InitUIMethod();
        }

        public FrmRunConfigMenuItem(int type, string parentText, string nodeText, string shortCut, Mesnac.Basic.FormShowType showType, string formFile, bool isAutoLoad)
        {
            InitializeComponent();

            this._type = type;
            this._parentText = parentText;
            this.txtNodeText.Text = nodeText;
            this.txtShortCut.Text = shortCut;
            this.cbShowType.SelectedIndex = (int)showType - 1;
            this.cbIsAutoLoad.Checked = isAutoLoad;
            if (String.IsNullOrEmpty(formFile))
            {
                this.cbIsAutoLoad.Enabled = false;
            }
            InitUIMethod();
        }

        public FrmRunConfigMenuItem(int type, string parentText, string msgId, string nodeText, string shortCut, Mesnac.Basic.FormShowType showType, string formFile, bool isAutoLoad)
        {
            InitializeComponent();

            this._type = type;
            this._parentText = parentText;
            this.txtNodeText.Text = nodeText;
            this.txtShortCut.Text = shortCut;
            this.cbShowType.SelectedIndex = (int)showType - 1;
            this.cbIsAutoLoad.Checked = isAutoLoad;
            if (String.IsNullOrEmpty(formFile))
            {
                this.cbIsAutoLoad.Enabled = false;
            }
            InitUIMethod();

            msgId = msgId == null ? String.Empty : msgId;
            if (this.cbMessages.Items.Contains(msgId))
            {
                this.cbMessages.SelectedItem = msgId;
            }
        }
        /// <summary>
        /// 初始化UI元素
        /// </summary>
        private void InitUIMethod()
        {
            if (this._type == 0)
            {
                this.Text = StringParser.Parse(ResourceService.GetString("Dialog_FrmRunConfigMenuItem_Text_Add"));  //"添加";
            }
            else
            {
                this.Text = StringParser.Parse(ResourceService.GetString("Dialog_FrmRunConfigMenuItem_Text_Modify"));  //"修改";
            }
            this.btnOK.Text = StringParser.Parse(ResourceService.GetString("Dialog_Button_OK"));    //确定
            this.btnCancel.Text = StringParser.Parse(ResourceService.GetString("Dialog_Button_Cancel"));    //取消
            this.label1.Text = StringParser.Parse(ResourceService.GetString("Dialog_FrmRunConfigMenuItem_label1_Text"));   //打开方式
            this.label2.Text = StringParser.Parse(ResourceService.GetString("Dialog_FrmRunConfigMenuItem_label2_Text"));   //文本
            this.label3.Text = StringParser.Parse(ResourceService.GetString("Dialog_FrmRunConfigMenuItem_label3_Text"));   //图标
            this.label4.Text = StringParser.Parse(ResourceService.GetString("Dialog_FrmRunConfigMenuItem_label4_Text"));   //父级
            this.label5.Text = StringParser.Parse(ResourceService.GetString("Dialog_FrmRunConfigMenuItem_label5_Text"));   //快捷键
            this.label6.Text = StringParser.Parse(ResourceService.GetString("Dialog_FrmRunConfigMenuItem_label6_Text"));   //示例:Control|O
            this.label7.Text = StringParser.Parse(ResourceService.GetString("Dialog_FrmRunConfigMenuItem_label7_Text"));   //国际化
            string parentText = StringParser.Parse(ResourceService.GetString("ViewContent_FrmSysConfig_Msg_parentText"));  //顶级
            this.lblParent.Text = String.IsNullOrEmpty(this._parentText) ? parentText : this._parentText;
            this.cbShowType.SelectedIndex = this.cbShowType.SelectedIndex == -1 ? 0 : this.cbShowType.SelectedIndex;
            this.cbIsAutoLoad.Text = StringParser.Parse(ResourceService.GetString("Dialog_FrmRunConfigMenuItem_cbIsAutoLoad_Text"));    //自动加载

            this.RefreshMessages();
        }

        private void RefreshMessages()
        {
            this.cbMessages.Items.Clear();
            foreach (string msgId in AppConfigHandler.Instance.SysMessages.Messages.Keys)
            {
                this.cbMessages.Items.Add(msgId);
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(this.txtNodeText.Text))
            {
                string msg1 = StringParser.Parse(ResourceService.GetString("Dialog_FrmRunConfigMenuItem_Msg_btnOK1"));    //请输入文本!
                MessageBox.Show(msg1, this.caption, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (!String.IsNullOrEmpty(this.txtShortCut.Text))
            {
                Keys keyValue = Mesnac.Core.MenuCommand.ParseShortcut(this.txtShortCut.Text);
                if (keyValue == Keys.None)
                {
                    string msg2 = StringParser.Parse(ResourceService.GetString("Dialog_FrmRunConfigMenuItem_Msg_btnOK2"));    //快捷键设置不合法,请修正!
                    MessageBox.Show(msg2, this.caption, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
            }
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }

        #region 属性定义

        /// <summary>
        /// 国际化资源ID
        /// </summary>
        public string MsgId
        {
            get
            {
                string msgId = this.cbMessages.SelectedItem as string;
                if (String.IsNullOrEmpty(msgId))
                {
                    return String.Empty;
                }
                else
                {
                    return msgId;
                }
            }
        }
        /// <summary>
        /// 节点文本
        /// </summary>
        public string NodeText
        {
            get { return this.txtNodeText.Text; }
        }
        /// <summary>
        /// 图标文件
        /// </summary>
        public string NodeImg
        {
            get { return this.txtImg.Text; }
        }
        /// <summary>
        /// 快捷键
        /// </summary>
        public string ShortCut
        {
            get { return this.txtShortCut.Text; }
        }
        /// <summary>
        /// 显示方式
        /// </summary>
        public int ShowType
        {
            get { return this.cbShowType.SelectedIndex + 1; }
        }

        public bool IsAutoLoad
        {
            get { return this.cbIsAutoLoad.Checked; }
        }

        #endregion

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }

        private void btnDialog_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofDialog = new OpenFileDialog();
            ofDialog.Filter = "(*.png)|*.png|(*.jpg)|*.jpg|(*.jpeg)|*.jpeg|(*.bmp)|*.bmp";
            DialogResult result = ofDialog.ShowDialog();
            if (result == System.Windows.Forms.DialogResult.OK)
            {
                this.txtImg.Text = ofDialog.FileName;
            }
        }

        #region 打开国际化资源筛选对话框

        private void btnSelectMessage_Click(object sender, EventArgs e)
        {
            FrmSelectMessage frmSelectMessage = new FrmSelectMessage();
            frmSelectMessage.ShowDialog(this);
            if (frmSelectMessage.DialogResult == System.Windows.Forms.DialogResult.OK)
            {
                if (!String.IsNullOrEmpty(frmSelectMessage.CurrMsgId))
                {
                    this.cbMessages.SelectedItem = frmSelectMessage.CurrMsgId;
                }
            }
        }

        #endregion
    }
}