using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using SlnMesnac.Business;
using SlnMesnac.Business.@base;
using SlnMesnac.Model.domain;
using SlnMesnac.Model.dto;
using SlnMesnac.Plc;
using SlnMesnac.Repository.service;
using SlnMesnac.WPF.Model;
using SlnMesnac.WPF.Page;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Timers;
using System.Windows;

#region << 版 本 注 释 >>
/*--------------------------------------------------------------------
* 版权所有 (c) 2024 WenJY 保留所有权利。
* CLR版本：4.0.30319.42000
* 机器名称：LAPTOP-E0N2L34V
* 命名空间：SlnMesnac.WPF.ViewModel
* 唯一标识：14008fcc-0a31-4f1e-bc80-9f9ea84d3de5
*
* 创建者：WenJY
* 电子邮箱：wenjy@mesnac.com
* 创建时间：2024-04-10 16:18:57
* 版本：V1.0.0
* 描述：
*
*--------------------------------------------------------------------
* 修改人：
* 时间：
* 修改说明：
*
* 版本：V1.0.0
*--------------------------------------------------------------------*/
#endregion << 版 本 注 释 >>
namespace SlnMesnac.WPF.ViewModel
{
    /// <summary>
    /// 设备参数监控
    /// </summary>
    internal class RecipeManageViewModel : ViewModelBase
    {
        private BaseBusiness baseBusiness = null;

        private PlcAbsractFactory plc = null;

        private RecipeManageCache recipeManageCache = RecipeManageCache.Instance;

        private  System.Timers.Timer refreshWeightTimer = new System.Timers.Timer(1000 * 5);

        private readonly ILogger<RecipeManageViewModel> _logger;
        /// <summary>
        /// 修改配方按钮
        /// </summary>
        public RelayCommand<int> UpdateRecipeCommand { get; set; }

        /// <summary>
        /// 手自动模式切换按钮
        /// </summary>
        public RelayCommand<string> ModeChangeCommand { get; set; }

      


        public RecipeManageViewModel()
        {
            _logger = App.ServiceProvider.GetService<ILogger<RecipeManageViewModel>>();

            baseBusiness = App.ServiceProvider.GetService<BaseBusiness>();
            UpdateRecipeCommand = new RelayCommand<int>(obj => UpdateRecipe(obj));
            ModeChangeCommand = new RelayCommand<string>(obj => ModeChange(obj));
            RrfreshDataGrid();

            refreshWeightTimer.Elapsed += new System.Timers.ElapsedEventHandler(RefreshWeight);
            refreshWeightTimer.AutoReset = true;
            refreshWeightTimer.Enabled = true;
            refreshWeightTimer.Start();


        }

        private void RefreshWeight(object? sender, ElapsedEventArgs e)
        {
            BufferWeight = recipeManageCache.BufferWeight.ToString("F2");
            RecipeName = recipeManageCache.RecipeName;
            RecipeMode = recipeManageCache.RecipeMode;
        }

            public void RrfreshDataGrid()
        {
            BufferWeight = recipeManageCache.BufferWeight.ToString("F2");
            RecipeName = recipeManageCache.RecipeName;
            RecipeMode = recipeManageCache.RecipeMode;
            List<RecipeManage> list = recipeManageCache.recipeManageList;
            if (list != null && list.Count > 0)
            {
                RecipeDataGrid.Clear();
                foreach (var item in list)
                {
                    RecipeDataGrid.Add(item);
                }
            }
        }

        #region 参数定义
        /// <summary>
        /// 配方DataGrid
        /// </summary>
        private ObservableCollection<RecipeManage> recipeDataGrid = new ObservableCollection<RecipeManage>();

        public ObservableCollection<RecipeManage> RecipeDataGrid
        {
            get { return recipeDataGrid; }
            set { recipeDataGrid = value; RaisePropertyChanged(() => RecipeDataGrid); }
        }


        //缓存区重量
        private string bufferWeight = string.Empty;
        public string BufferWeight
        {
            get { return bufferWeight; }
            set { bufferWeight = value; RaisePropertyChanged(() => BufferWeight); }
        }

        //当前配方
        private string recipeName = string.Empty;
        public string RecipeName
        {
            get { return recipeName; }
            set { recipeName = value; RaisePropertyChanged(() => RecipeName); }
        }

        //运行模式
        private string recipeMode = string.Empty;
        public string RecipeMode
        {
            get { return recipeMode; }
            set { recipeMode = value; RaisePropertyChanged(() => RecipeMode); }
        }

        #endregion



        /// <summary>
        /// 更新配方
        /// </summary>
        /// <param name="RecipeKey"></param>
        public void UpdateRecipe(int RecipeKey)
        {
           
           RecipeManageSetWindow window = new RecipeManageSetWindow(RecipeKey);
            window.ShowDialog();
            RrfreshDataGrid();
        }
        /// <summary>
        /// 模式切换
        /// </summary>
        /// <param name="RecipeKey"></param>
        public void ModeChange(string param)
        {
            try
            {
                //切换自动模式
                if ("auto".Equals(param))
                {
                    var result = MessageBox.Show("是否确认切换自动模式？", "确认", MessageBoxButton.YesNo, MessageBoxImage.Information);
                    if (result == MessageBoxResult.Yes)
                    {
                        recipeManageCache.RecipeMode = "自动模式";
                        RecipeMode = "自动模式";
                    }
                }
                else if ("manual".Equals(param))
                {
                    RecipeModeSetWindow window = new RecipeModeSetWindow();
                    window.ShowDialog();
                }

                RecipeName = recipeManageCache.RecipeName;
                RecipeMode = recipeManageCache.RecipeMode;

            }catch (Exception ex)
            {
                _logger.LogError("ModeChange异常:" + ex.Message);
            }
        }

        
    }
}
