using KGIS.Framework.Platform;
using Kingo.Plugin.MapView.Enum;
using Kingo.Plugin.MapView.Model;
using Kingo.PluginServiceInterface.Model;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
namespace Kingo.Plugin.MapView.Views.FeatureAttribute
{
    /// 
    /// UCFieldsInfo.xaml 的交互逻辑
    /// 
    public partial class UCFieldsInfo : UserControl, IPropertyStruct
    {
        private LayerFieldCfg Cfg = null;
        private List _Items;
        private string key;
        private string val;
        public enumPropertyType PropertyType
        {
            get { return (enumPropertyType)GetValue(PropertyTypeProperty); }
            set { SetValue(PropertyTypeProperty, value); }
        }
        public IEnumerable ItemsSource
        {
            get { return _Items; }
            set
            {
                try
                {
                    _Items = (List)value;
                }
                catch (Exception ex)
                {
                    _Items = new List();
                    throw ex;
                }
                finally
                {
                    //RefreshView();
                }
            }
        }
        public static readonly DependencyProperty KeyProperty =
            DependencyProperty.Register("Key", typeof(string), typeof(UCFieldsInfo), new PropertyMetadata(null));
        public static readonly DependencyProperty ValueProperty =
            DependencyProperty.Register("Value", typeof(string), typeof(UCFieldsInfo), new UIPropertyMetadata(null, new PropertyChangedCallback(OnReCalcLayout)));
        public static readonly DependencyProperty PropertyTypeProperty =
            DependencyProperty.Register("PropertyType", typeof(enumPropertyType), typeof(UCFieldsInfo), new UIPropertyMetadata(enumPropertyType.TextBox, new PropertyChangedCallback(OnReBindingLayout)));
        public string Key
        {
            get { return (string)GetValue(KeyProperty); }
            set { SetValue(KeyProperty, value); }
        }
        public string Value
        {
            get { return (string)GetValue(ValueProperty); }
            set { SetValue(ValueProperty, value); }
        }
        public List DicData { get; set; }
        public string DisplayMemberPath { get; set; }
        public string ValuePath { get; set; }
        public bool IsReadOnly { get; set; }
        public bool Visible { get; set; }
        public UCFieldsInfo()
        {
            InitializeComponent();
            //_Items = new List();
            //string cfgFilePath = System.IO.Path.Combine(SysAppPath.GetConfigPath(), "LayerFieldsCfg.xml");
            //Cfg = SerializeAPI.DeserializeToObject2(cfgFilePath);
        }
        private static void OnReCalcLayout(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            //(sender as UCFieldsInfo).ReCalcLayout();
            (sender as UCFieldsInfo).ChangeView();
        }
        private static void OnReBindingLayout(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            //(sender as UCFieldsInfo).ReCalcLayout();
            (sender as UCFieldsInfo).SetBinding();
        }
        private void SetBinding()
        {
            //Binding bind = new Binding();
            //bind.Path = new PropertyPath("Value");
            //bind.Mode = BindingMode.TwoWay;
            //bind.UpdateSourceTrigger = UpdateSourceTrigger.LostFocus;
            //bind.RelativeSource = new RelativeSource(RelativeSourceMode.TemplatedParent);
            switch (PropertyType)
            {
                case enumPropertyType.Default:
                    break;
                case enumPropertyType.TextBox:
                    //TextBox.Visibility = Visibility.Visible;
                    //TextBox.SetBinding(TextBox.TextProperty, bind);
                    break;
                case enumPropertyType.ComboBox:
                    break;
                case enumPropertyType.CheckBox:
                    break;
                case enumPropertyType.DatePicker:
                    break;
                case enumPropertyType.SpinEdit:
                    break;
                case enumPropertyType.ButtonEdit:
                    break;
                case enumPropertyType.ButtonEditFJ:
                    break;
                case enumPropertyType.AutoCompleteBox:
                    break;
                default:
                    break;
            }
        }
        private void ChangeView()
        {
            lable.Content = Key;
            TextBox.Text = Value;
        }
        //private void RefreshView()
        //{
        //    SetVisible(_Items);
        //    #region
        //    LayerInfo layer = Cfg.Layers[0];
        //    grid.Children.Clear();
        //    grid.RowDefinitions.Clear();
        //    foreach (var item in _Items)
        //    {
        //        if (item.Visible)
        //        {
        //            grid.RowDefinitions.Add(new RowDefinition());
        //            Label lab = new Label();
        //            lab.Content = item.Key;
        //            lab.SetValue(Grid.RowProperty, grid.RowDefinitions.Count - 1);
        //            lab.HorizontalContentAlignment = HorizontalAlignment.Right;
        //            lab.Margin = new Thickness(4);
        //            grid.Children.Add(lab);
        //            Control ctrl = new TextBox();
        //            Binding bind = new Binding();
        //            bind.Path = new PropertyPath("Value");
        //            bind.Mode = BindingMode.TwoWay;
        //            bind.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
        //            switch (item.PropertyType)
        //            {
        //                case enumPropertyType.Default:
        //                case enumPropertyType.TextBox:
        //                    ctrl = new TextBox();
        //                    //ctrl.DataContext = item;
        //                    ctrl.SetBinding(TextBox.TextProperty, bind);
        //                    break;
        //                case enumPropertyType.CheckBox:
        //                    ctrl = new CheckBox();
        //                    //ctrl.DataContext = item;
        //                    ctrl.SetBinding(CheckBox.IsCheckedProperty, bind);
        //                    break;
        //                case enumPropertyType.DatePicker:
        //                    ctrl = new DatePicker();
        //                    //ctrl.DataContext = item;
        //                    ctrl.SetBinding(DatePicker.TextProperty, bind);
        //                    break;
        //                case enumPropertyType.SpinEdit:
        //                    break;
        //                case enumPropertyType.ButtonEdit:
        //                    break;
        //                case enumPropertyType.ComboBox:
        //                case enumPropertyType.AutoCompleteBox:
        //                    ctrl = new AutoCompleteBox();
        //                    //ctrl.DataContext = item;
        //                    ctrl.SetValue(AutoCompleteBox.ItemsSourceProperty, item.DicData);
        //                    ctrl.SetValue(AutoCompleteBox.ValueMemberPathProperty, item.ValuePath);
        //                    ctrl.SetValue(AutoCompleteBox.DisplayMemberPathProperty, item.DisplayMemberPath);
        //                    ctrl.SetBinding(AutoCompleteBox.SelectedValueProperty, bind);
        //                    break;
        //                default:
        //                    break;
        //            }
        //            ctrl.SetValue(Grid.RowProperty, grid.RowDefinitions.Count - 1);
        //            ctrl.SetValue(Grid.ColumnProperty, 1);
        //            ctrl.HorizontalAlignment = HorizontalAlignment.Stretch;
        //            ctrl.Margin = new Thickness(2);
        //            grid.Children.Add(ctrl);
        //        }
        //    }
        //    #endregion
        //}
        //private void SetVisible(List pList)
        //{
        //    if (Cfg == null) return;
        //    if (Cfg.Layers.Count == 0) return;
        //    LayerInfo layer = Cfg.Layers[0];
        //    foreach (var item in layer.Fields)
        //    {
        //        string val = string.Empty;
        //        int idx = -1;
        //        List TempDic = null;
        //        var GroupField = pList.FirstOrDefault(f => f.Key == item.Name || f.Key == item.AliasName);
        //        if (GroupField == null)
        //        {
        //            GroupField = new PropertyGridDataStruct();
        //            GroupField.PropertyType = enumPropertyType.ComboBox;
        //        }
        //        GroupField.Key = item.AliasName;
        //        foreach (var subItem in item.Fields)
        //        {
        //            var field = pList.FirstOrDefault(f => f.Key == subItem.Name || f.Key == subItem.AliasName);
        //            if (field == null) return;
        //            if (!string.IsNullOrWhiteSpace(field.Value))
        //            {
        //                //if (!string.IsNullOrWhiteSpace(val.ToTrim()))
        //                //    val += " - ";
        //                //val += field.Value;
        //            }
        //            if (GroupField.PropertyType == field.PropertyType)
        //            {
        //                GroupField.Value = field.Value;
        //                GroupField.DicData = field.DicData;
        //                GroupField.ValuePath = field.ValuePath;
        //                GroupField.DisplayMemberPath = field.DisplayMemberPath;
        //            }
        //            if (!string.IsNullOrWhiteSpace(subItem.DisplayWhere.ToTrim()))
        //            {
        //                //字段显示条件
        //                string[] wheres = subItem.DisplayWhere.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
        //                for (int i = 0; i < wheres.Length; i++)
        //                {
        //                    string[] kv = wheres[i].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
        //                    if (kv.Length != 2)
        //                    {
        //                        LogAPI.Debug(string.Format("图层字段配置错误,FieldCfg中Name={0}", subItem.Name));
        //                        continue;
        //                    }
        //                    var tempField = pList.FirstOrDefault(f => f.Key == kv[0] || f.Key == kv[0]);
        //                    if (tempField != null)
        //                    {
        //                        if (kv[1].ToTrim().Contains("%"))
        //                            subItem.Display = tempField.Value.ToTrim().StartsWith(kv[1].ToTrim().Replace("%", ""));
        //                        else
        //                            subItem.Display = tempField.Value.ToTrim() == kv[1].ToTrim();
        //                    }
        //                }
        //            }
        //            field.Visible = subItem.Display;
        //            if (field.Visible)
        //            {
        //                item.Display = false;
        //            }
        //            if (idx == -1)
        //            {
        //                idx = pList.IndexOf(field);
        //            }
        //        }
        //        if (item.Display)
        //        {
        //            if (idx == -1) idx = 0;
        //            var field = pList.FirstOrDefault(f => f.Key == item.Name || f.Key == item.AliasName);
        //            if (field == null)
        //            {
        //                GroupField.Value = val;
        //                GroupField.Visible = item.Display;
        //                pList.Insert(idx, GroupField);
        //            }
        //            GroupField.Visible = item.Display;
        //        }
        //    }
        //}
    }
}