﻿using System;
using System.ComponentModel;
using System.Drawing.Design;
using System.Windows.Forms;
using SGLibrary.Extend;

namespace SGLibrary.Framework.Criterion.WinFormControl
{
    [System.ComponentModel.ToolboxItem(typeof(ToolboxItem))]
    public partial class WhereCtlStringIn : WhereCtlBase, ICanRemWhereControl
    {
        public WhereCtlStringIn()
        {
            InitializeComponent();

            CompareCb.Items.Add(new KeyText<AtCompare>(AtCompare.Invalid, ""));
            CompareCb.Items.Add(new KeyText<AtCompare>(AtCompare.At, "属于"));
            CompareCb.Items.Add(new KeyText<AtCompare>(AtCompare.UnAt, "不属于"));

            cbCompare_SelectedIndexChanged(null, null);
        }

        protected override void _Resize()
        {
            base._Resize();
            if (ValueTb == null)
                return;

            ValueTb.Top = (Height - ValueTb.Height) / 2;
            ValueTb.Left = CompareCb.Left + CompareCb.Width + 10;
            ValueTb.Width = this.Width - ValueTb.Left - 28 - 8;
        }

        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public IWhereNode Value
        {
            get
            {
                return this.WhereAtom;
            }
            set
            {
                if (value is WhereNodeStringIn)
                    this.WhereAtom = value as WhereNodeStringIn;
                else
                    this.WhereAtom = null;
            }
        }

        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public WhereNodeStringIn WhereAtom
        {
            get
            {
                var whereAtom = new WhereNodeStringIn { Bind = this.BindProperty };

                if (CompareCb.SelectedItem == null)
                    whereAtom.Compare = AtCompare.Invalid;
                else
                    whereAtom.Compare = ((KeyText<AtCompare>)CompareCb.SelectedItem).Key;

                whereAtom.CompareValueString = this.ValueTb.Text;

                return whereAtom;
            }

            set
            {
                if (value == null)
                {
                    CompareCb.SelectedIndex = -1;
                    return;
                }

                foreach (KeyText<AtCompare> vt in CompareCb.Items)
                    if (vt.Key == value.Compare)
                    {
                        CompareCb.SelectedItem = vt;
                        break;
                    }

                this.ValueTb.Text = value.CompareValueString;
            }
        }

        /// <summary>
        /// 逗号分隔的待选项
        /// </summary>
        [Description("逗号分隔的待选项")]
        public String ItemsString { get; set; }

        public IWhereNode GetRemWhereNode()
        {
            var result = this.WhereAtom;
            return result.Compare == AtCompare.Invalid ? null : result;
        }

        private void cbCompare_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.DesignMode)
                return;

            ValueTb.Enabled = CompareCb.SelectedItem != null
                && ((KeyText<AtCompare>)CompareCb.SelectedItem).Key != AtCompare.Invalid;
        }

        private void tbValue_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter && this.Value.Enabled && this.EnterKeyDown != null)
                this.EnterKeyDown(this, e);
        }

        public void SetFocus()
        {
            if (this.CompareCb.SelectedItem != null)
                this.ValueTb.Focus();
        }

        public event EventHandler EnterKeyDown;

        private void ValueTb_DoubleClick(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(this.ItemsString))
                return;

            var fm = new WhereCtlStringIn_Slt();
            fm.Text = "选择 " + this.Text;
            fm.ItemsString = this.ItemsString;
            fm.OldValuesString = this.ValueTb.Text;
            if (fm.ShowDialog() == DialogResult.OK)
                this.ValueTb.Text = fm.SltValuesString;
        }

        private void WhereCtlStringIn_Load(object sender, EventArgs e)
        {
            if (this.DesignMode)
                return;

            this._Resize();
        }
    }
}
