using Com.FirmLib.Entity;
using FWindSoft.Wpf.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Markup;
using System.Windows.Media;
using WPG;

namespace Com.FirmLib.UI.Common
{
    /// <summary>
    /// 信息点模板管理
    /// </summary>
    public class InfoPointTemplateParse : ITemplateParse
    {
        private static Dictionary<string, DataTemplate> m_RefDataTemplates = new Dictionary<string, DataTemplate>();
        static InfoPointTemplateParse()
        {
            ResourceDictionary dic = new ResourceDictionary();
            PropertyGrid gr = new PropertyGrid();
            dic.Source = new Uri("/Com.FirmLib.UI;component/Common/CellTemplates.xaml", UriKind.Relative);
            //dic.Source = new Uri("/TemplateLib;component/DataGrid/CellTemplates.xaml", UriKind.Relative);
            //dic.MergedDictionaries.Add(ResourceManager.DataGridCellTemplate);
            foreach (var key in dic.Keys)
            {
                DataTemplate tempTemplate = dic[key] as DataTemplate;
                if (tempTemplate != null)
                {
                    m_RefDataTemplates.Add(key.ToString(), tempTemplate);
                }
            }
        }
        /// <summary>
        /// 根据信息点的信息去创建某值
        /// </summary>
        /// <returns></returns>
        public DataTemplate SelectTemplate(object ob)
        {
            var geDefinition = ob as EquipmentInfoPointItem;
            if (geDefinition == null)
                return CreateDefaultTemplate();
            //var id = geDefinition.CollectionCmptCode;
            //if(string.IsNullOrEmpty(id))
            //    return CreateDefaultTemplate();
            string controlId = geDefinition.CollectionCmptCode;
            if (string.IsNullOrEmpty(geDefinition.CollectionCmptCode))
            {
                controlId = geDefinition.InputType;
            }
            if (controlId == "E1")
            {
                controlId = "Enum_D1";
            }
            DataTemplate template;
            if (!m_RefDataTemplates.TryGetValue(controlId, out template))
            {
                template = CreateDefaultTemplate();
            }
            return template;

        }
        private DataTemplate CreateDefaultTemplate()
        {
            DataTemplate temp = new DataTemplate();
            FrameworkElementFactory root = new FrameworkElementFactory(typeof(TextBlock));
            Binding binding = new Binding("Value");
            root.SetBinding(TextBlock.TextProperty, binding);
            temp.VisualTree = root;
            temp.Seal();
            return temp;
        }
        #region 构造解析器
        /// <summary>
        /// 创建模板解析器
        /// </summary>
        /// <returns></returns>
        public static CommonTemplateProvider CreateTemplateManager()
        {
            CommonTemplateProvider commonTemplate = new CommonTemplateProvider();
            InfoPointTemplateParse pasrse = new InfoPointTemplateParse();
            commonTemplate.Add(typeof(EquipmentInfoPointItem), pasrse);
            return commonTemplate;
        }
        #endregion
    }

  
}
