using MWS.Library.Common;
using MWS.Model.Model;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;

namespace MWS.Domain.Common
{
    public class ConfigHelper
    {
        private static readonly string configFilePath = @"Assets/Config/Config.xml";

        /// <summary>
        /// 获取系统设定项数据
        /// </summary>
        /// <returns></returns>
        public static SystemConfig GetSystemConfig()
        {
            var xDocument = XDocument.Load(configFilePath);
            var xelement = xDocument.Root.Element("System");
            if (xelement != null)
            {
                SystemConfig systemConfig = new SystemConfig()
                {
                    DataSaveInterval = ushort.Parse(xelement.Element("DataSaveInterval").Value),
                    IsFullScreen = bool.Parse(xelement.Element("IsFullScreen").Value),
                    PageRefreshInterval = ushort.Parse(xelement.Element("PageRefreshInterval").Value),
                    DataReadInterval = ushort.Parse(xelement.Element("DataReadInterval").Value),
                    WeightItem = new SystemConfig.Weighting
                    {
                        EffectCount = int.Parse(xelement.Element("Weighting").Element("EffectCount").Value),
                        FrontTime = int.Parse(xelement.Element("Weighting").Element("FrontTime").Value),
                        Threshold = float.Parse(xelement.Element("Weighting").Element("Threshold").Value),
                        PutThreshold = float.Parse(xelement.Element("Weighting").Element("PutThreshold").Value),
                        LoadMode = short.Parse(xelement.Element("Weighting").Element("LoadMode").Value)
                    }
                };
                return systemConfig;
            }
            return null;

        }
        /// <summary>
        /// 获取称重仪表配置项
        /// </summary>
        /// <returns></returns>
        public static IndicatorConfigModel GetIndicatorConfig()
        {
            var xDocument = XDocument.Load(configFilePath);
            var xelement = xDocument.Root.Element("APP").Element("Weighing");
            if (xelement != null)
            {
                return new IndicatorConfigModel
                {
                    Type = xelement.Attribute("Type").Value.ToSafeInt(),
                    Devices = xelement.Elements("Indicator").Select(c => new IndicatorConfigModel.Device
                    {
                        DeviceId = c.Attribute("DeviceId").Value.ToSafeInt16(),
                        SteadyTagId = c.Attribute("SteadyTagId").Value.ToSafeInt(),
                        GrabOpenTagId = c.Attribute("GrabOpenTagId").Value.ToSafeInt(),
                        LoadWeightTagId = c.Attribute("LoadWeightTagId").Value.ToSafeInt(),
                        NetWeightTagId = c.Attribute("NetWeightTagId").Value.ToSafeInt(),
                        TareWeightTagId = c.Attribute("TareWeightTagId").Value.ToSafeInt(),
                        RatedWeightTagId = c.Attribute("RatedWeightTagId").Value.ToSafeInt(),
                        SmallCarInplaceTagId = (c.Attribute("SmallCarInplaceTagId")?.Value ?? "0").ToSafeInt(),
                        CarInplaceTagId = (c.Attribute("CarInplaceTagId")?.Value ?? "0").ToSafeInt(),
                        BigCarInplaces = c.Elements("BigCarInplace").Select(e => new IndicatorConfigModel.Device.BigCarInplace
                        {
                            TagId = e.Attribute("TagId").Value.ToSafeInt(),
                            EntranceId = e.Attribute("EntranceId").Value.ToSafeInt16()
                        }).ToList()
                    }).ToList()
                };
            }
            return null;
        }

        /// <summary>
        /// 获取设备总接触器变量
        /// </summary>
        /// <returns></returns>
        public static List<TotalContactorModel> GetTotalContactor()
        {
            var xDocument = XDocument.Load(configFilePath);
            var xElement = xDocument.Root.Element("APP").Element("TotalContactor");
            if (xElement != null)
            {
                return xElement.Elements("Device").Select(c => new TotalContactorModel
                {
                    DeviceId = short.Parse(c.Attribute("Id").Value),
                    TagIds = c.Attribute("TagIds").Value.Split(',').Select(e => int.Parse(e)).ToList()
                }).ToList();
            }
            return new List<TotalContactorModel>();

        }

        /// <summary>
        /// 获取档位换算配置项
        /// </summary>
        /// <returns></returns>
        public static List<GearConverConfig> GearConverConfig()
        {
            var xDocument = XDocument.Load(configFilePath);
            var xElement = xDocument.Root.Element("APP").Element("GearConver");
            if (xElement != null)
            {
                var config = xElement.Elements("Device").Elements("Mechanism").Select(c => new GearConverConfig
                {
                    MecType = int.Parse(c.Attribute("Type").Value),
                    TagId = int.Parse(c.Attribute("TagId").Value),
                    TagName = c.Attribute("TagName").Value,
                    Instantly = new List<int>(),
                    Gears = c.Elements("Gear").Select(g => new GearConverConfig.Gear
                    {
                        Name = g.Attribute("Name").Value,
                        Value = short.Parse(g.Attribute("Value").Value),
                        Condition = g.Attribute("Condition").Value.Split(',').Select(e => int.Parse(e)).ToList()
                    }).ToList()
                }).ToList();
                return config;
            }
            return new List<GearConverConfig>();

        }

        /// <summary>
        /// 获取设备主要数据变量Id
        /// </summary>
        /// <param name="deviceId">设备Id 如为null则获取全部</param>
        /// <returns></returns>
        public static List<DeviceMainDataModel> GetDeviceMainDataConfig(int? deviceId = null)
        {
            var xDocument = XDocument.Load(configFilePath);
            var xElement = xDocument.Root.Element("APP").Element("Layout");
            if (xElement != null)
            {
                List<DeviceMainDataModel> output = new List<DeviceMainDataModel>();
                DeviceMainDataModel model;
                var devices = xElement.Elements("Device");
                foreach (var device in devices)
                {
                    model = new DeviceMainDataModel
                    {
                        WeightTagId = device.Attribute("WeightTagId").Value.ToInt(),
                        DeviceId = device.Attribute("Id").Value.ToInt()
                    };
                    var bigCar = device.Element("BigCar");
                    model.BigCar = new MecMainDataModel
                    {
                        BrakeTagId = bigCar.Attribute("BrakeTagId").Value.ToInt(),
                        GearTagId = bigCar.Attribute("GearTagId").Value.ToInt(),
                        StrokeTagId = bigCar.Attribute("StrokeTagId").Value.ToInt(),
                    };
                    var smallCar = device.Element("SmallCar");
                    model.SmallCar = new MecMainDataModel
                    {
                        BrakeTagId = smallCar.Attribute("BrakeTagId").Value.ToInt(),
                        GearTagId = smallCar.Attribute("GearTagId").Value.ToInt(),
                        StrokeTagId = smallCar.Attribute("StrokeTagId").Value.ToInt(),
                    };
                    var hoist = device.Element("Hoist");
                    model.Hoist = new MecMainDataModel
                    {
                        BrakeTagId = hoist.Attribute("BrakeTagId").Value.ToInt(),
                        GearTagId = hoist.Attribute("GearTagId").Value.ToInt(),
                        StrokeTagId = hoist.Attribute("StrokeTagId").Value.ToInt(),
                    };
                }
                return output;
            }
            return null;
        }

    }
}