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

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

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


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

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

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

        public List<KeyValuePair<string, string>> FontNameItems
        {
            get { return comboBoxFontName.DataSource as List<KeyValuePair<string, string>>; }
            set  { comboBoxFontName.DataSource = value; }
        }
        private List<KeyValuePair<string, string>> fontNameItems_;

        public List<KeyValuePair<string, string>> FontSizeItems
        {
            get { return comboBoxFontSize.DataSource as List<KeyValuePair<string, string>>; }
            set { comboBoxFontSize.DataSource = value; }
        }
        private List<KeyValuePair<string, string>> fontSizeItems_;

        private int width_ = 0;
        private int height_ = 0;

        public LayoutLineItemRow()
        {
            InitializeComponent();
            comboBoxFontName.ValueMember = "key";
            comboBoxFontName.DisplayMember = "value";
            comboBoxFontSize.ValueMember = "Key";
            comboBoxFontSize.DisplayMember = "Value";
            msgModel = new Hashtable();
            fontNameItems_ = new List<KeyValuePair<string, string>>();
            fontSizeItems_ = new List<KeyValuePair<string, string>>();
            BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            width_ = this.Width;
            height_ = this.Height;

            init();
        }

        private void init()
        {
            this.checkBoxTitle.CheckedChanged += new EventHandler(checkBoxTitle_CheckedChanged);
            this.comboBoxFontName.SelectedValueChanged += new EventHandler(comboBoxFontName_SelectedValueChanged);
            this.comboBoxFontSize.SelectedValueChanged += new EventHandler(comboBoxFontSize_SelectedValueChanged);
            this.buttonColor.Click += new EventHandler(buttonColor_Click);
            this.Load += new EventHandler(onLoad);
        }

        private void onLoad(object sender, EventArgs ev)
        {
            checkBoxTitle.Checked = false;
            settingSize();
        }

        private void settingSize()
        {
            this.Size = !checkBoxTitle.Checked
                ? new Size(checkBoxTitle.Width + checkBoxTitle.Location.X + 1, checkBoxTitle.Height + checkBoxTitle.Location.Y + 1)
                : new Size(width_, height_);
        }

        private void onModelChanged()
        {
            if (msgModel == null || msgModel.Count == 0) return;

            this.checkBoxTitle.CheckedChanged -= new EventHandler(checkBoxTitle_CheckedChanged);
            this.comboBoxFontName.SelectedValueChanged -= new EventHandler(comboBoxFontName_SelectedValueChanged);
            this.comboBoxFontSize.SelectedValueChanged -= new EventHandler(comboBoxFontSize_SelectedValueChanged);
            
            MsgItemContentModel mod = null;
            foreach (MsgItemContentModel topItem in msgModel.Values) { mod = topItem; break; }

            checkBoxTitle.Checked = bool.Parse(mod.Enable);
            textBoxFontName.Text = mod.fontName;
            textBoxFontSize.Text = mod.size;
            if (buttonColor.BackColor.R > 190 &&
                    buttonColor.BackColor.G > 190 &&
                    buttonColor.BackColor.B > 190)
                buttonColor.ForeColor = Color.Black;
            else
                buttonColor.ForeColor = Color.White;

            settingSize();

            this.checkBoxTitle.CheckedChanged += new EventHandler(checkBoxTitle_CheckedChanged);
            this.comboBoxFontName.SelectedValueChanged += new EventHandler(comboBoxFontName_SelectedValueChanged);
            this.comboBoxFontSize.SelectedValueChanged += new EventHandler(comboBoxFontSize_SelectedValueChanged);

            this.IsChecked = checkBoxTitle.Checked;

            if (OnTitleCheckChangedEvent != null)
                OnTitleCheckChangedEvent(this, EventArgs.Empty);
        }

        private void checkBoxTitle_CheckedChanged(object sender, EventArgs ev)
        {
            settingSize();

            if (msgModel == null || msgModel.Count == 0) return;

            foreach (MsgItemContentModel mod in msgModel.Values) mod.Enable = checkBoxTitle.Checked.ToString().ToUpper();
            onModelChanged();

            if (OnTitleCheckChangedEvent != null)
                OnTitleCheckChangedEvent(this, ev);

        }

        private void comboBoxFontName_SelectedValueChanged(object sender, EventArgs ev)
        {
            if (msgModel == null || msgModel.Count == 0) return;

            foreach (MsgItemContentModel mod in msgModel.Values) mod.fontName = comboBoxFontName.SelectedItem as string;
            onModelChanged();

            notyfyPropertyChanged(this, ev);
        }

        private void comboBoxFontSize_SelectedValueChanged(object sender, EventArgs ev)
        {
            if (msgModel == null || msgModel.Count == 0) return;

            foreach (MsgItemContentModel mod in msgModel.Values) mod.size = comboBoxFontSize.SelectedValue as string;
            onModelChanged();

            notyfyPropertyChanged(this, ev);
        }

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

            notyfyPropertyChanged(this, ev);
        }

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

    }
}
