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 AttachLayoutItemPanel : UserControl
    {
        public event EventHandler OnPropertyChangedEvent;


        public List<MsgItemContentModel> Models 
        {
            get { return models_; }
            set
            {
                models_ = value;
                onModelChanged();
            }
        }
        private List<MsgItemContentModel> models_ = new List<MsgItemContentModel>();


        public AttachLayoutItemPanel()
        {
            InitializeComponent();
            comboBoxFontName.ValueMember = "key";
            comboBoxFontName.DisplayMember = "value";
            comboBoxFontSize.ValueMember = "Key";
            comboBoxFontSize.DisplayMember = "Value";
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            models_ = new List<MsgItemContentModel>();

            var sizes = new List<KeyValuePair<string, string>>();
            foreach (var s in GlobalSettings.SizeItems)
                sizes.Add(new KeyValuePair<string, string>(s.Key, s.Value));
            comboBoxFontSize.DataSource = sizes;

            var names = new List<KeyValuePair<string, string>>();
            foreach (var n in GlobalSettings.prepareFontNames())
                names.Add(new KeyValuePair<string, string>(n, n));
            comboBoxFontName.DataSource = names;
        }

        private void onModelChanged()
        {
            if (models_ == null) return;

            checkBoxVisibility.CheckedChanged -= new EventHandler(checkBoxVisibility_CheckedChanged);
            comboBoxFontName.SelectedValueChanged -= new EventHandler(comboBoxFontName_SelectedValueChanged);
            comboBoxFontSize.SelectedValueChanged -= new EventHandler(comboBoxFontSize_SelectedValueChanged);
            
            MsgItemContentModel mod = null;
            foreach (var m in models_) mod = m;
            checkBoxVisibility.Checked = mod == null ? false : bool.Parse(mod.Enable);
            comboBoxFontSize.SelectedValue = textBoxFontSize.Text = mod == null ? "" : mod.size;
            comboBoxFontName.SelectedValue = textBoxFontName.Text = mod == null ? "" : mod.fontName;
            buttonColor.BackColor = mod == null ? Color.Black : mod.FontColor;

            checkBoxVisibility.CheckedChanged += new EventHandler(checkBoxVisibility_CheckedChanged);
            comboBoxFontName.SelectedValueChanged += new EventHandler(comboBoxFontName_SelectedValueChanged);
            comboBoxFontSize.SelectedValueChanged += new EventHandler(comboBoxFontSize_SelectedValueChanged);
            
        }

        private void checkBoxVisibility_CheckedChanged(object sender, EventArgs e)
        {
            if (models_ == null || models_.Count == 0) return;
            foreach (var mod in Models)
                mod.Enable = this.checkBoxVisibility.Checked.ToString().ToUpper();

            if (OnPropertyChangedEvent != null)
                OnPropertyChangedEvent(this, e);
        }

        private void comboBoxFontName_SelectedValueChanged(object sender, EventArgs e)
        {
            if (models_ == null || models_.Count == 0) return;
            textBoxFontName.Text = comboBoxFontName.SelectedValue.ToString();
            foreach (var mod in Models)
                mod.fontName = comboBoxFontName.SelectedValue.ToString();

            if (OnPropertyChangedEvent != null)
                OnPropertyChangedEvent(this, e);
        }

        private void comboBoxFontSize_SelectedValueChanged(object sender, EventArgs e)
        {
            if (models_ == null || models_.Count == 0) return;
            textBoxFontSize.Text = ((KeyValuePair<string, string>)comboBoxFontSize.SelectedItem).Key;
            foreach (var mod in Models)
                mod.size = textBoxFontSize.Text;

            if (OnPropertyChangedEvent != null)
                OnPropertyChangedEvent(this, e);
        }

        private void buttonColor_Click(object sender, EventArgs e)
        {
            if (models_ == null || models_.Count == 0) return;
 
            ColorDialog dialog = new ColorDialog();
            if (dialog.ShowDialog(this) == DialogResult.OK)
            {
                if (dialog.Color == null) return;

                var res = dialog.Color;
                foreach (var model in models_)
                    model.FontColor = dialog.Color;
                buttonColor.BackColor = res;

                if (OnPropertyChangedEvent != null)
                    OnPropertyChangedEvent(this, e);
            }
        }

        private void comboBoxFontName_DropDown(object sender, EventArgs e)
        {

        }



    }
}
