using System;
using System.Collections.Generic;
using System.Windows.Forms;
using PostCardHitsuji.DataClient.Model;
using Hitsuji.Framework.Utils;

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

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

        public IllustItemContentModel IllustModel
        {
            get { return illustModel; }
            set
            {
                illustModel = value;
                onModelChanged();
            }
        }
        private IllustItemContentModel illustModel;

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

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

        public List<KeyValuePair<string, string>> IllustSizeItems
        {
            get { return comboBoxMultiple.DataSource as List<KeyValuePair<string, string>>; }
            set { comboBoxMultiple.DataSource = value; }
        }

        public LayoutIllustItemRow()
        {
            InitializeComponent();
            comboBoxMultiple.ValueMember = "key";
            comboBoxMultiple.DisplayMember = "value";
            this.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;

            init();
        }

        private void init()
        {
            registEvent();
        }

        private void registEvent()
        {
            this.checkBoxTitle.CheckedChanged += new EventHandler(checkBoxTitle_CheckedChanged);
            this.comboBoxMultiple.SelectedValueChanged += new EventHandler(comboBoxMultiple_SelectedValueChanged);
            this.comboBoxFilePath.SelectedValueChanged += new EventHandler(comboBoxFilePath_SelectedValueChanged);
            this.buttonFileRef.Click += new EventHandler(buttonFileRef_Clicked);
        }

        private void killEvent()
        {
            this.checkBoxTitle.CheckedChanged -= new EventHandler(checkBoxTitle_CheckedChanged);
            this.comboBoxMultiple.SelectedValueChanged -= new EventHandler(comboBoxMultiple_SelectedValueChanged);
            this.comboBoxFilePath.SelectedValueChanged -= new EventHandler(comboBoxFilePath_SelectedValueChanged);
            this.buttonFileRef.Click -= new EventHandler(buttonFileRef_Clicked);
        }

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

            // モデルのデータ値を表示コントロールに反映する
            killEvent();

            //有効フラグ
            checkBoxTitle.Checked = bool.Parse(illustModel.Enable);
            //ファイルパスコンボにパスを追加
            addFilePathComboItem(illustModel.filepath);
            if (!ValueUtil.tryGetString(illustModel.filepath).Equals(""))
                comboBoxFilePath.SelectedValue = illustModel.filepath;
            //倍率設定
            comboBoxMultiple.SelectedValue = illustModel.multiple;

            registEvent();

        }

        private void checkBoxTitle_CheckedChanged(object sender, EventArgs ev)
        {
            if (illustModel == null) return;
            
            illustModel.Enable = checkBoxTitle.Checked ? true.ToString().ToUpper() : false.ToString().ToUpper();
            
            if (OnTitleCheckChangedEvent != null)
                OnTitleCheckChangedEvent(this, ev);

            notyfyPropertyChanged(this, ev);
        }

        private void comboBoxMultiple_SelectedValueChanged(object sender, EventArgs ev)
        {
            if (illustModel == null) return;

            illustModel.multiple = comboBoxMultiple.SelectedItem == null 
                                    ? "1" : ((KeyValuePair<string, string>)comboBoxMultiple.SelectedItem).Key;
            
            notyfyPropertyChanged(this, ev);
        }

        private void comboBoxFilePath_SelectedValueChanged(object sender, EventArgs ev)
        {
            if (illustModel == null) return;
            
            illustModel.filepath = ValueUtil.tryGetString(comboBoxFilePath.SelectedItem);
            
            notyfyPropertyChanged(this, ev);
        }

        private void addFilePathComboItem(string itemText)
        {
            if (ValueUtil.tryGetString(itemText).Equals("") ||
                comboBoxFilePath.Items.Contains(itemText)) return;
            comboBoxFilePath.Items.Add(itemText);
        }

        private void buttonFileRef_Clicked(object sender, EventArgs ev)
        {
            var dialog = PostCardHitsuji.Utils.WinControlAttacher.DialogAttacher.createOpenFileDialog("イラスト|*.jpg;*jpeg;*.png;*.gif");
            var result = dialog.ShowDialog(this);

            if (result == DialogResult.Yes || result == DialogResult.OK)
            {
                if (dialog.FileName.Equals("")) return;

                illustModel.filepath = dialog.FileName;
                addFilePathComboItem(illustModel.filepath);
                comboBoxFilePath.SelectedItem = illustModel.filepath;

                notyfyPropertyChanged(this, ev);
            }
        }

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

    }
}
