using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using SlnMesnac.Config;
using SlnMesnac.Model.domain;
using SlnMesnac.Repository.service;
using SlnMesnac.WPF;
using SlnMesnac.WPF.Page;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

#region << 版 本 注 释 >>
/*--------------------------------------------------------------------
* 版权所有 (c) 2024 WenJY 保留所有权利。
* CLR版本：4.0.30319.42000
* 机器名称：LAPTOP-E0N2L34V
* 命名空间：SlnMesnac.Model.dto
* 唯一标识：96940a51-66a8-4593-b68d-e4d7fdb16c9b
*
* 创建者：WenJY
* 电子邮箱：wenjy@mesnac.com
* 创建时间：2024-04-12 11:07:33
* 版本：V1.0.0
* 描述：
*
*--------------------------------------------------------------------
* 修改人：
* 时间：
* 修改说明：
*
* 版本：V1.0.0
*--------------------------------------------------------------------*/
#endregion << 版 本 注 释 >>
namespace SlnMesnac.WPF.Model
{
    /// <summary>
    /// 配方管理封装对象
    /// </summary>
    public class RecipeManageCache
    {
        #region 单例实现
        private static readonly RecipeManageCache lazy = new RecipeManageCache();
        public static RecipeManageCache Instance
        {
            get
            {
                return lazy;
            }
        }
        #endregion

        #region 变量定义
        private readonly ILogger<RecipeManageCache> _logger;
        private DebugConfig config = DebugConfig.Instance;
        private readonly IBaseRecipeManageService? baseRecipeManageService;

        /// <summary>
        /// 所有配方列表
        /// </summary>
        public List<RecipeManage> recipeManageList = null;


        /// <summary>
        /// 当前正在使用配方
        /// </summary>
        public RecipeManage useRecipeManage
        {
            get
            {
                if (recipeManageList != null)
                {
                    return recipeManageList.FirstOrDefault(x => x.RecipeName == config.RecipeName);
                }
                else
                {
                    return null;
                }
            }
           
        }


        /// <summary>
        /// 系统运行状态;点击一键启动按钮和急停复位，改1,点击一键停止和急停改0;1的状态下根据缓存区自动启动设备
        /// </summary>
        public string SystemStatus
        {
            get { return config.SystemStatus; }
            set { config.SystemStatus = value; }
        }

        /// <summary>
        /// 当前缓存区监测重量
        /// </summary>
        public double BufferWeight { 
            get { return double.Parse(config.BufferWeight); }
            set { config.BufferWeight = value.ToString(); }
        }


        /// <summary>
        /// 2楼缓存区最大停机重量
        /// </summary>
        public double MaxBufferWeight
        {
            get { return double.Parse(config.MaxBufferWeight); }
            set { config.MaxBufferWeight = value.ToString(); }
        }


        /// <summary>
        /// 2楼缓存区最小启动重量
        /// </summary>
        public double MinBufferWeight
        {
            get { return double.Parse(config.MinBufferWeight); }
            set { config.MinBufferWeight = value.ToString(); }
        }



        /// <summary>
        /// 当前使用模版名称
        /// </summary>
        public string RecipeName
        {
            get { return config.RecipeName; }
            set { config.RecipeName = value; }
        }
        /// <summary>
        /// 当前使用模式
        /// </summary>
        public string RecipeMode
        {
            get { return config.RecipeMode; }
            set { config.RecipeMode = value; }
        }

        /// <summary>
        /// 拆包机螺旋--烘干机螺旋切换间隔(单位:ms)
        /// </summary>
        public string UnpackToHotTime
        {
            get { return config.UnpackToHotTime; }
            set { config.UnpackToHotTime = value; }
        }

        /// <summary>
        /// 烘干机螺旋--螺旋1切换间隔(单位:ms)
        /// </summary>
        public string HotToSpiralTime
        {
            get { return config.HotToSpiralTime; }
            set { config.HotToSpiralTime = value; }
        }

        /// <summary>
        /// 螺旋1--螺旋2切换间隔(单位:ms)
        /// </summary>
        public string SpiralTwoTime
        {
            get { return config.SpiralTwoTime; }
            set { config.SpiralTwoTime = value; }
        }

        #endregion


        public RecipeManageCache()
        {
            baseRecipeManageService = App.ServiceProvider.GetService<IBaseRecipeManageService>();
            recipeManageList = baseRecipeManageService.GetRecipeManageList();
        }



        /// <summary>
        /// 更新配方
        /// </summary>
        /// <param name="recipeManage"></param>
        public void UpdateRecipeManage(RecipeManage recipeManage)
        {
            try
            {
                baseRecipeManageService.UpdateRecipeManage(recipeManage);
                //刷新缓存
                recipeManageList = baseRecipeManageService.GetRecipeManageList();
            }
            catch (Exception ex)
            {
                _logger.LogError($"UpdateRecipeManage异常:{ex.Message}");
            }
        }
    }
}
