using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Data;
using System.Globalization;

namespace Platform.Controls.TableContainer
{
    public class ItemTemplate : DataTemplateSelector
    {
        public static TradeMenu Instance;        

        public HierarchicalDataTemplate DefaultTreeViewTemplate { get; set; }

        public HierarchicalDataTemplate DefaultMenuTemplate { get; set; }

        public HierarchicalDataTemplate DefaultExpanderTemplate { get; set; }

        public override DataTemplate SelectTemplate(object item, DependencyObject container)
        {
            HierarchicalDataTemplate DefaultTemplate = null;

            #region 获取主控件
            //TradeMenu控件
            DependencyObject obj;

            //通过可视化树找到TradeMenu控件
            for (obj = container; !(obj is TradeMenu); obj = VisualTreeHelper.GetParent(obj))
            {
                //超过可视化树外围即退出
                if (obj == null)
                    break;
            }

            //存储TradeMenu控件，节约查找时间
            if (obj is TradeMenu)
            {
                Instance = obj as TradeMenu;
            }
            #endregion

            //实例设置模版是采用实例的模版
            if (Instance != null && Instance.ItemTemplate != null)
            {
                return Instance.ItemTemplate;
            }

            //从资源字段获取对应样式
            switch (Instance.TemplateType)
            {
                case TemplateTypeList.TreeView:
                    DefaultTemplate = DefaultTreeViewTemplate;
                    break;

                case TemplateTypeList.Menu:
                    DefaultTemplate = DefaultMenuTemplate;
                    break;

                case TemplateTypeList.Expander:

                    DefaultTemplate = DefaultExpanderTemplate;
                    break;
            }

            return DefaultTemplate;
        }
    }

    public class MenuToIndentConverter : IValueConverter
    {
        
        private static double _MenuIndentSize = 10.0;

        /// <summary>
        /// 获取或设置缩进
        /// </summary>
        public static double IndentSize
        {
            get { return MenuToIndentConverter._MenuIndentSize; }
            set { MenuToIndentConverter._MenuIndentSize = value; }
        }

        public object Convert(object o, Type type, object parameter, CultureInfo culture)
        {
            double indent=default(double);

            if (ItemTemplate.Instance != null)
            {
                indent = ItemTemplate.Instance.IndentSize;
            }
            return new Thickness((int)indent, 0, 0, 0);
        }

        public object ConvertBack(object o, Type type, object parameter, CultureInfo culture)
        {
            throw new NotSupportedException();
        }
    }
}
