using Aitex.Common.Util;
using Aitex.Core.RT.Log;
using Aitex.Core.Util;
using MECF.Framework.Common.Persistent.Prewet;
using MECF.Framework.Common.Persistent.Reservoirs;
using MECF.Framework.Common.Persistent.Rinse;
using MECF.Framework.Common.Persistent.SRD;
using MECF.Framework.Common.ToolLayout;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MECF.Framework.Common.Persistent.Dryer
{
    public class DryerPersistentManager: Singleton<DryerPersistentManager>
    {
        #region 内部变量
        private Dictionary<string, DryerPersistentValue> _persistentValueDic = new Dictionary<string, DryerPersistentValue>();
        private Dictionary<string, string> _persistentValuePathDic = new Dictionary<string, string>();
        #endregion
        /// <summary>
        /// 初始化
        /// </summary>
        public void Initialize()
        {
            try
            {
                List<string> lst = DryerItemManager.Instance.InstalledModules;
                foreach (string item in lst)
                {
                    string foldStr = PathManager.GetCfgDir() + $"Persistent\\Dryer";
                    if (!Directory.Exists(foldStr))
                    {
                        Directory.CreateDirectory(foldStr);
                    }
                    string path = PathManager.GetCfgDir() + $"Persistent\\Dryer\\{item}Persistent.xml";
                    _persistentValuePathDic[item] = path;
                    if (File.Exists(path))
                    {
                        DryerPersistentValue reservoirs1PersistentValue = CustomXmlSerializer.Deserialize<DryerPersistentValue>(new FileInfo(path));
                        if (reservoirs1PersistentValue != null)
                        {
                            _persistentValueDic[item] = reservoirs1PersistentValue;
                        }
                    }
                    else
                    {
                        DryerPersistentValue persistentValue = new DryerPersistentValue();
                        persistentValue.Name = item;
                        persistentValue.OperatingMode = "Manual";
                        persistentValue.RecipeOperatingMode = "Engineering";
                        _persistentValueDic[item] = persistentValue;
                        UpdatePersistentValue(item);
                    }
                }
            }
            catch (Exception ex)
            {
                LOG.WriteLog(eEvent.ERR_DRYER, "System", "Load DryerPersistent xml exception");
            }
        }
        /// <summary>
        /// 获取Dryer Persistent数值
        /// </summary>
        /// <returns></returns>
        public DryerPersistentValue GetDryerPersistentValue(string module)
        {
            DryerPersistentValue value = new DryerPersistentValue();
            if (_persistentValueDic.ContainsKey(module)==false)
            {
                return null;
            }
            else
            {
                value = _persistentValueDic[module];
            }
            return value;
        }
        /// <summary>
        /// 更新PersistentValue
        /// </summary>
        /// <returns></returns>
        public void UpdatePersistentValue(string module)
        {
            if (_persistentValueDic.ContainsKey(module))
            {
                try
                {
                    CustomXmlSerializer.Serialize(_persistentValueDic[module], _persistentValuePathDic[module]);
                }
                catch (Exception ex)
                {
                    LOG.WriteLog(eEvent.ERR_METAL, module, "Update DryersPersistent xml file excepetion");
                }
            }
            else
            {
                LOG.WriteLog(eEvent.ERR_METAL, module, "Update DyersPersistent xml file excepetion");
            }
        }
    }
}