using KGIS.Framework.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
namespace Kingo.Plugin.BGSetting.View.ViewSystemSetting
{
    /// 
    /// 单图斑属性页面配置 的交互逻辑
    /// 
    public partial class ViewAttributeShowSetting : UserControl
    {
        private System.Xml.XmlDocument doc = null;
        private readonly string ArearNameCode = SysConfigsOprator.GetAppsetingValueByKey("ArearName");
        private readonly string strPath = SysAppPath.GetConfigPath() + "AttributeShowConfigs.xml";
        public ViewAttributeShowSetting()
        {
            InitializeComponent();
            this.Loaded += InitData;
        }
        private void InitData(object sender, RoutedEventArgs e)
        {
            try
            {
                List attributeShowInfos = new List();
                doc = new System.Xml.XmlDocument();
                doc.Load(strPath);
                System.Xml.XmlNode xmlSysNode = doc.SelectSingleNode("Attributes");
                if (xmlSysNode.ChildNodes != null && xmlSysNode.ChildNodes.Count > 0)
                {
                    foreach (System.Xml.XmlNode node in xmlSysNode.ChildNodes)
                    {
                        if (node.Attributes["Name"].Value.Contains(ArearNameCode))//绑定区域码
                        {
                            AttributeShowInfo attributeShow = new AttributeShowInfo();
                            foreach (System.Xml.XmlNode itemNode in node.ChildNodes)
                            {
                                if (itemNode.Attributes == null) return;//过滤注释项
                                attributeShow = new AttributeShowInfo
                                {
                                    Name = itemNode.Attributes["Name"].Value,
                                    ViewName = itemNode.Attributes["ViewName"].Value,
                                    Type = itemNode.Attributes["Type"].Value,
                                    IsShow = itemNode.Attributes["IsShow"].Value == "false" ? false : true
                                };
                                if (attributeShowInfos.FirstOrDefault(x => x.Name.Equals(attributeShow.Name)) == null)
                                    attributeShowInfos.Add(attributeShow);
                            }
                        }
                    }
                    dgCtrl.ItemsSource = null;
                    dgCtrl.ItemsSource = attributeShowInfos;
                }
            }
            catch (Exception ex)
            {
                LogAPI.Debug("属性页面配置异常:" + ex.Message);
            }
        }
        /// 
        /// 单元格修改
        /// 
        /// 
        /// 
        private void TableView_CellValueChanged(object sender, DevExpress.Xpf.Grid.CellValueChangedEventArgs e)
        {
            try
            {
                string AttributeName = (e.Source.FocusedRow as AttributeShowInfo).Name;
                bool AttributeIsShow = (e.Source.FocusedRow as AttributeShowInfo).IsShow;
                if (doc == null) return;
                System.Xml.XmlNode xmlSysNode = doc.SelectSingleNode("Attributes");
                if (xmlSysNode.ChildNodes != null && xmlSysNode.ChildNodes.Count > 0)
                {
                    foreach (System.Xml.XmlNode node in xmlSysNode.ChildNodes)
                    {
                        if (node.Attributes["Name"].Value.Contains(ArearNameCode))//绑定区域码
                        {
                            foreach (System.Xml.XmlNode itemNode in node.ChildNodes)
                            {
                                if (itemNode.Attributes == null) return;//过滤注释项
                                if (itemNode.Attributes["Name"].Value.Equals(AttributeName))
                                {
                                    itemNode.Attributes["IsShow"].Value = AttributeIsShow ? "false" : "True";
                                    break;
                                }
                            }
                        }
                    }
                    doc.Save(strPath);
                }
            }
            catch (Exception ex)
            {
                LogAPI.Debug("修改属性页面配置发生异常:" + ex);
            }
        }
    }
    public class AttributeShowInfo
    {
        public string Name { set; get; }
        public string ViewName { set; get; }
        public string Type { set; get; }
        public bool IsShow { set; get; }
    }
}