using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using PostCardHitsuji.DataClient.Model;
using PostCardHitsuji.Global;

namespace PostCardHitsuji.Ui.RowPanel
{
    public partial class LayoutMsgItemRow : UserControl
    {
        public event EventHandler OnTitleCheckChangedEvent;

        /// <summary>
        /// プロパティ値の変更が完了したときに発火
        /// </summary>
        public event EventHandler OnPropertyEditEnd;


        public MsgItemContentModel MsgModel
        {
            get { return msgModel; }
            set
            {
                msgModel = value;
                onModelChanged();
            }
        }
        private MsgItemContentModel msgModel;

        public string CheckBoxTitleText
        {
            get { return checkBoxTitle.Text; }
            set { checkBoxTitle.Text = value; }
        }

        public bool IsChecked
        {
            get { return checkBoxTitle.Checked; }
            set { checkBoxTitle.Checked = value; }
        }

        public Color ButtonFontColor
        {
            get { return buttonColor.BackColor; }
            set { buttonColor.BackColor = value; }
        }

        public LayoutMsgItemRow()
        {
            InitializeComponent();
            comboBoxFontSize.ValueMember = "Key";
            comboBoxFontSize.DisplayMember = "Value";
            BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;

            registEvents();
        }

        private void registEvents()
        {
            this.checkBoxTitle.CheckedChanged += new EventHandler(checkBoxTitle_CheckedChanged);
            this.comboBoxFontName.SelectedValueChanged += new EventHandler(comboBoxFontName_SelectedValueChanged);
            this.comboBoxFontSize.SelectedValueChanged += new EventHandler(comboBoxFontSize_SelectedValueChanged);
            this.richTextBoxMsg.TextChanged += new EventHandler(richTextBoxMsg_TextChanged);
            this.comboBoxFontName.DropDown += new EventHandler(comboBoxFontName_DropDown);
            this.buttonColor.Click += new EventHandler(buttonColor_Click);
            this.checkBoxVertical.CheckedChanged += new EventHandler(checkBoxVertical_CheckedChanged);
        }

        private void removeEvents()
        {
            this.checkBoxTitle.CheckedChanged -= new EventHandler(checkBoxTitle_CheckedChanged);
            this.comboBoxFontName.SelectedValueChanged -= new EventHandler(comboBoxFontName_SelectedValueChanged);
            this.comboBoxFontSize.SelectedValueChanged -= new EventHandler(comboBoxFontSize_SelectedValueChanged);
            this.richTextBoxMsg.TextChanged -= new EventHandler(richTextBoxMsg_TextChanged);
            this.comboBoxFontName.DropDown -= new EventHandler(comboBoxFontName_DropDown);
            this.buttonColor.Click -= new EventHandler(buttonColor_Click);
            this.checkBoxVertical.CheckedChanged -= new EventHandler(checkBoxVertical_CheckedChanged);
        }

        private void onModelChanged()
        {
            if (this.msgModel == null) return;

            removeEvents();
            try
            {
                comboBoxFontName.DataSource = GlobalSettings.prepareFontNames();
                var sizeItems = new List<KeyValuePair<string, string>>();
                sizeItems.Add(new KeyValuePair<string, string>("", ""));
                foreach (var sizeItem in GlobalSettings.SizeItems)
                    sizeItems.Add(new KeyValuePair<string, string>(sizeItem.Key, sizeItem.Value));
                comboBoxFontSize.DataSource = sizeItems;
                comboBoxFontName.SelectedItem = msgModel.fontName;
                comboBoxFontSize.SelectedValue = msgModel.size;
                buttonColor.BackColor = msgModel.FontColor;
                if (buttonColor.BackColor.R > 190 &&
                    buttonColor.BackColor.G > 190 &&
                    buttonColor.BackColor.B > 190)
                    buttonColor.ForeColor = Color.Black;
                else
                    buttonColor.ForeColor = Color.White;
            }
            finally
            {
                removeEvents();
                registEvents();
            }
        }

        private void checkBoxTitle_CheckedChanged(object sender, EventArgs ev)
        {
            if (msgModel != null)
                MsgModel.Enable = checkBoxTitle.Checked.ToString().ToUpper();
            if (OnTitleCheckChangedEvent != null)
                OnTitleCheckChangedEvent(this, ev);

            notyfyPropertyChanged(this, ev);
        }

        private void comboBoxFontName_SelectedValueChanged(object sender, EventArgs ev)
        {
            if (msgModel != null)
                MsgModel.fontName = comboBoxFontName.SelectedItem as string;
            
            notyfyPropertyChanged(this, ev);
        }

        private void comboBoxFontSize_SelectedValueChanged(object sender, EventArgs ev)
        {
            if (msgModel != null)
                MsgModel.size = comboBoxFontSize.SelectedValue as string;
            
            notyfyPropertyChanged(this, ev);
        }

        private void richTextBoxMsg_TextChanged(object sender, EventArgs ev)
        {
            if (msgModel != null)
                MsgModel.message = richTextBoxMsg.Text;
            
            notyfyPropertyChanged(this, ev);
        }

        private void buttonColor_Click(object sender, EventArgs ev)
        {
            var colorDialog = new ColorDialog();
            if (colorDialog.ShowDialog(this) == DialogResult.OK &&
                colorDialog.Color != null)
            {
                msgModel.FontColor = colorDialog.Color;
            }
            onModelChanged();

            notyfyPropertyChanged(this, ev);
        }

        private void checkBoxVertical_CheckedChanged(object sender, EventArgs ev)
        {
            if (msgModel != null)
                msgModel.isMsgDirectVertical = checkBoxVertical.Checked;

            notyfyPropertyChanged(this, ev);
        }

        private void notyfyPropertyChanged(object sender, EventArgs ev)
        {
            if (OnPropertyEditEnd != null)
                OnPropertyEditEnd(this, ev);
        }

        private void comboBoxFontName_DropDown(object sender, EventArgs ev)
        {
            comboBoxFontName.DataSource = GlobalSettings.prepareFontNames();
        }

    }
}
