﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing.Design;
using SGLibrary.Extend;

namespace SGLibrary.Framework.Criterion
{
    /// <summary>
    /// Guid代码查询条件，Guid代码不可为Guid.Empty
    /// </summary>
    [DefaultEvent("LoadItems")]
    [System.ComponentModel.ToolboxItem(typeof(ToolboxItem))]
    public partial class WhereCtlCodeCb_Guid : WhereCtlBase, ICanRemWhereControl
    {
        public WhereCtlCodeCb_Guid()
        {
            InitializeComponent();
        }

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

            CompareCb.Width = this.Width - CompareCb.Left - 12;  //12 凑宽度180能对齐WhereCtlDate
        }

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

            this._Resize();
            this.ReLoadItems();
        }

        private Object _ParentItem;

        public Object ParentItem
        {
            get
            {
                return _ParentItem;
            }
            set
            {
                if (this.DesignMode 
                    || value == _ParentItem)
                    return;

                _ParentItem = value;
                ReLoadItems();
            }
        }

        protected void ReLoadItems()
        {
            var e = new WhereCodeEventArgsGuid();
            e.Parent = this.ParentItem;
            e.Value = new List<KeyText<Guid>>();
            if (LoadItems != null)
                LoadItems(this, e);

            var oldSelected = GetSelectedValue();
            var selectedIndex = -1;
            CompareCb.Items.Clear();
            CompareCb.Items.Add(new KeyText<Guid>(Guid.Empty, ""));
            foreach (var item in e.Value)
            {
                if (String.IsNullOrEmpty(item.Text))
                    throw new Exception("空行特殊约定为全部, Text不允许为空");

                var idx = CompareCb.Items.Add(new KeyText<Guid>(item.Key, item.Text));
                if (selectedIndex < 0 && oldSelected != Guid.Empty && oldSelected == item.Key)
                    selectedIndex = idx;
            }

            CompareCb.SelectedIndex = selectedIndex;
        }

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

        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public WhereNodeCode_Guid WhereAtom
        {
            get
            {
                var result = new WhereNodeCode_Guid { Bind = this.BindProperty };
                var value = GetSelectedValue();
                if (value == Guid.Empty)
                    result.Compare = CodeCompare_Value.Invalid;
                else
                {
                    result.Compare = CodeCompare_Value.Equal;
                    result.CompareValue = (Guid)value;
                }

                return result;
            }
            set
            {
                if (value == null)
                {
                    CompareCb.SelectedIndex = -1;
                    return;
                }
                var selectValue = value.CompareValue;
                CompareCb.SelectedIndex = CompareCb.Items.Add(new KeyText<Guid>(selectValue, selectValue.ToString()));
                this.ReLoadItems();
            }
        }

        protected Guid GetSelectedValue()
        {
            var selectedItem = CompareCb.SelectedItem as KeyText<Guid>;
            if (selectedItem == null)
                return Guid.Empty;
            else
                return selectedItem.Key;
        }

        public IWhereNode GetRemWhereNode()
        {
            var result = this.Value;
            return (result as WhereNodeCode_Guid).Compare == CodeCompare_Value.Invalid ? null : result;
        }

        public event EventHandler<WhereCodeEventArgsGuid> LoadItems;

        private void CompareCb_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.OnWhereChanged(this.Value);
        }
    }

    public class WhereCodeEventArgsGuid : EventArgs<List<KeyText<Guid>>>
    {
        public Object Parent { get; set; }
    }
}
