using KGIS.Framework.Utils;
using KGIS.Framework.Utils.Helper;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Windows.Controls;
using System.Xml.Linq;
namespace Kingo.Plugin.SystemSetting.View.ViewSystemSetting
{
    /// 
    /// 系统设置-变更图斑检查规则设置 的交互逻辑
    /// 
    public partial class ViewRuleSetting : UserControl
    {
        public List FieldsTempData = null;
        public ViewRuleSetting()
        {
            InitializeComponent();
            BindData();
        }
        private void BindData()
        {
            try
            {
                FieldsTempData = new List();
                string SDM = KGIS.Framework.Utils.SysConfigsOprator.GetAppsetingValueByKey("ArearName");
                string matchingRegionFilePath = System.IO.Path.Combine(SysAppPath.GetConfigPath(), "CheckRuleConfigs.xml");
                XDocument pXDoc = XDocument.Load(matchingRegionFilePath);
                var elements = pXDoc.Element("CheckRules").Elements();
                foreach (XElement item in elements)
                {
                    if (!string.IsNullOrEmpty(SDM) && item.Attribute("Name").Value.Equals(SDM))
                    {
                        var element = item.Elements();
                        foreach (XElement xElement in element)
                        {
                            Rule rule = new Rule();
                            rule.RuleName = xElement.Attribute("RuleName") == null ? "" : xElement.Attribute("RuleName").Value;
                            rule.RuleCode = xElement.Attribute("RuleCode") == null ? "" : xElement.Attribute("RuleCode").Value;
                            rule.RuleContent = xElement.Attribute("RuleContent") == null ? "" : xElement.Attribute("RuleContent").Value;
                            rule.ErrorType = xElement.Attribute("ErrorType") == null ? "" : xElement.Attribute("ErrorType").Value;
                            rule.ErrorTip = xElement.Attribute("ErrorTip") == null ? "" : xElement.Attribute("ErrorTip").Value;
                            rule.IsCheck = xElement.Attribute("IsCheck") == null ? false : xElement.Attribute("IsCheck").Value.ToBoolean();
                            FieldsTempData.Add(rule);
                        }
                    }
                }
                treeList.ItemsSource = FieldsTempData;
            }
            catch (Exception ex)
            {
                LogAPI.Debug("GetMatchingRegionMapping异常:" + ex.Message);
                LogAPI.Debug("GetMatchingRegionMapping异常:" + ex.StackTrace);
            }
        }
        private void View_CellValueChanged(object sender, DevExpress.Xpf.Grid.TreeList.TreeListCellValueChangedEventArgs e)
        {
            try
            {
                var checkingRule = e.Cell.Row as Rule;
                if (checkingRule != null)
                {
                    if (checkingRule.IsCheck)
                    {
                        checkingRule.IsCheck = false;
                        UpdateConfigs(checkingRule.RuleCode, "false");
                    }
                    else
                    {
                        checkingRule.IsCheck = true;
                        UpdateConfigs(checkingRule.RuleCode, "true");
                    }
                    treeList.RefreshData();
                }
            }
            catch (Exception ex)
            {
                LogAPI.Debug("View_CellValueChanged异常:" + ex.Message);
                LogAPI.Debug("View_CellValueChanged异常:" + ex.StackTrace);
                MessageHelper.Show("单图斑质检规则设置失败!");
            }
        }
        public static void UpdateConfigs(string RuleCode, string IsCheck)
        {
            try
            {
                string SDM = KGIS.Framework.Utils.SysConfigsOprator.GetAppsetingValueByKey("ArearName");
                string strPath = SysAppPath.GetConfigPath() + "CheckRuleConfigs.xml";
                XDocument pXDoc = XDocument.Load(strPath);
                var elements = pXDoc.Element("CheckRules").Elements();
                foreach (XElement item in elements)
                {
                    if (!string.IsNullOrEmpty(SDM) && item.Attribute("Name").Value.Equals(SDM))
                    {
                        var element = item.Elements().Where(x => x.Attribute("RuleCode").Value.Equals(RuleCode));
                        if (element != null)
                            element.FirstOrDefault().Attribute("IsCheck").Value = IsCheck;
                        pXDoc.Save(strPath);
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                LogAPI.Debug("UpdateConfigs异常:" + ex.Message);
                LogAPI.Debug("UpdateConfigs异常:" + ex.StackTrace);
                throw ex;
            }
        }
    }
    public class Rule
    {
        public string ClassName { get; set; }
        public string CheckObject { get; set; }
        public string ErrorTip { get; set; }
        public string RuleCode { get; set; }
        public string ErrorType { get; set; }
        public string RuleName { get; set; }
        public bool IsCheck { get; set; }
        public string RuleContent { get; set; }
    }
}