using Aitex.Common.Util;
using Aitex.Core.RT.DataCenter;
using Aitex.Core.Util;
using MECF.Framework.Common.CommonData;
using SciChart.Charting.Visuals.RenderableSeries;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Xml.Serialization;

namespace MECF.Framework.Common.Aitex.Core.Common.DeviceData
{
    public class ProcessHistoryItemsDefault : Singleton<ProcessHistoryItemsDefault>
    {
        /// <summary>
        /// 查询数据源，
        /// </summary>
        private List<ProcessDeviceItem> PM1ItemList { get; set; }

        private List<ProcessDeviceItem> PM2ItemList { get; set; }

        private string _path = PathManager.GetAppDir() + "\\Export";

        public ProcessHistoryItemsDefault()
        {

        }

        public void Initialize()
        {
            var phi1 = CustomXmlSerializer.Deserialize<ProcessDeviceItems>(
                new FileInfo($"Config\\PM\\PM1\\ProcessDeviceItemsPM1.xml"));
            PM1ItemList = phi1.ItemList;
            DATA.Subscribe($"PM1.ProcessHistoryItemsDefault", () => PM1ItemList,
                SubscriptionAttribute.FLAG.IgnoreSaveDB);

            var phi2 = CustomXmlSerializer.Deserialize<ProcessDeviceItems>(
                new FileInfo($"Config\\PM\\PM2\\ProcessDeviceItemsPM2.xml"));
            PM2ItemList = phi2.ItemList;
            DATA.Subscribe($"PM2.ProcessHistoryItemsDefault", () => PM2ItemList,
                SubscriptionAttribute.FLAG.IgnoreSaveDB);

            DATA.Subscribe($"RT.AutoExpProcessHistory.Path", () => _path,
                SubscriptionAttribute.FLAG.IgnoreSaveDB);

        }
    }

    /// <summary>
    /// 根据XML配置生成ProcessHistory多个单元的集合对象
    /// </summary>
    [XmlType(typeName: "ProcessDeviceItems")]
    public class ProcessDeviceItems
    {
        /// <summary>
        /// ProcessDeviceItems默认对象集合
        /// </summary>
        [XmlElement("ProcessDeviceItem")]
        public List<ProcessDeviceItem> ItemList { get; set; }


    }

    /// <summary>
    /// ProcessHistory默认对象集合数据
    /// </summary>
    [Serializable]
    [DataContract]
    [XmlType(typeName: "ProcessDeviceItem")]
    public class ProcessDeviceItem
    {
        /// <summary>
        /// 客户期望界面显示项
        /// </summary>
        [XmlAttribute()]
        [DataMember]
        public string Display { get; set; }

        /// <summary>
        /// 界面显示项，SIC数据库属性名称
        /// </summary>
        [XmlAttribute()]
        [DataMember]
        public string Property { get; set; }

        /// <summary>
        /// 单位，显示用
        /// </summary>
        [XmlAttribute()]
        [DataMember]
        public string Unit { get; set; }

        /// <summary>
        /// 数据表中是否包含当前属性，不包含的时候，此属性不能作为查询项
        /// </summary>
        [DataMember]
        public bool IsPropertyExists { get; set; }
    }

    /// <summary>
    /// Recipe生产历史记录
    /// </summary>
    public class RecipeProcessHistoryData
    {
        public string PmModel { get; set; }

        private string xPath;

        public string XPath
        {
            get => xPath;
            set
            {
                xPath = value;
                Name = value == null || value == "" ? "" : System.IO.Path.GetFileName(value); //数据库保存的是路加带文件名
            }
        }

        public string Name { get; set; }

        public string Status { get; set; }

        public string ProcessGuid { get; set; }

        private string _beginTime;

        public string BeginTime
        {
            get => _beginTime;
            set => _beginTime =
                String.IsNullOrEmpty(value) ? "" : Convert.ToDateTime(value).ToString("yyyy-MM-dd HH:mm:ss"); //数据库时间格式 "2023-11-21 17:13:40.292"
        }

        private string endTime;

        public string EndTime
        {
            get => endTime;
            set => endTime =
                String.IsNullOrEmpty(value) ? "" : Convert.ToDateTime(value).ToString("HH:mm:ss"); //数据库时间格式 "2023-11-21 17:13:40.292"
        }

        public string WaferDataGuid { get; set; }

        /// <summary>
        /// wafer首次创建时的信息
        /// </summary>
        public string WaferInf { get; set; }

        /// <summary>
        /// wafer传片时，使用的Tray名称
        /// </summary>
        public string TrayInf { get; set; }
    }

    /// <summary>
    /// Recipe执行时Process对应的步骤流程
    /// </summary>
    public class ProcessHistoryData : NotifiableItem
    {
        private bool _isExport;

        public bool IsExport
        {
            get => _isExport;
            set
            {
                _isExport = value;
                InvokePropertyChanged("IsExport");
            }
        }

        private string _stepBeginTime;

        public string StepBeginTime
        {
            get => _stepBeginTime;
            set => _stepBeginTime = 
                String.IsNullOrEmpty(value) ? "": Convert.ToDateTime(value).ToString("yyyy-MM-dd HH:mm:ss"); //数据库时间格式 "2023-11-21 17:13:40.292"
            
        }

        private string stepEndTime;

        public string StepEndTime
        {
            get => stepEndTime;
            set
            {
                stepEndTime = value;
                StepEndTimeDisp = 
                    String.IsNullOrEmpty(value) ? "" : Convert.ToDateTime(value).ToString("HH:mm:ss"); //数据库时间格式 "2023-11-21 17:13:40.292"
            }
        }

        public string StepEndTimeDisp { get; set; }

        public string RecipeName { get; set; }

        public string StepName { get; set; }

        public string StepTime { get; set; }

        public string StepNumber { get; set; }

        public List<ProcessHistoryDeviceData> DeviceData { get; set; }
    }

    /// <summary>
    /// Recipe执行时Process对应的步骤运行期间内，单种数据记录
    /// </summary>
    public class ProcessHistoryDeviceData : ObservableCollection<IRenderableSeries>
    {
        /// <summary>
        /// Device当前所属Step
        /// </summary>
        public string StepNumber { get; set; }

        /// <summary>
        /// Device当前所属Step
        /// </summary>
        public string StepName { get; set; }

        /// <summary>
        /// 列表中展示的名称
        /// </summary>
        public string DeviceName { get; set; }

        /// <summary>
        /// 实际数据注册到数据库的属性名
        /// </summary>
        public string DATASubscribeName { get; set; }

        /// <summary>
        /// 在时间段内，查询到的数据集合
        /// </summary>
        public List<float> DeviceValue { get; set; } = new List<float>();

        /// <summary>
        /// 在时间段内，查询到的数据当时的时间戳，每次获取数据都会记录时间戳
        /// </summary>
        public List<string> TimeStamp { get; set; } = new List<string>();

        /// <summary>
        /// 数据单位
        /// </summary>
        public string Unit { get; set; }

        public string Min { get; set; }

        public string Average { get; set; }

        public string Max { get; set; }

        /// <summary>
        /// 数据表中是否含有此属性
        /// </summary>
        public bool IsPropertyExists { get; set; }

        /// <summary>
        /// 根据查询的数据集合计算出对应的数据
        /// </summary>
        public void CalculateData()
        {
            Min = (DeviceValue.Count > 0 ? DeviceValue.Min(t => t) : 0).ToString("0.000");
            Average = (DeviceValue.Count > 0 ? DeviceValue.Average(t => t) : 0).ToString("0.000");
            Max = (DeviceValue.Count > 0 ? DeviceValue.Max(t => t) : 0).ToString("0.000");
        }
    }
}
