using Aitex.Core.RT.Event;
using Aitex.Core.RT.OperationCenter;
using Aitex.Core.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Threading;
using System.Windows;
using DocumentFormat.OpenXml.Office2010.Excel;


namespace MECF.Framework.UI.Client.TrayThickness.HistoryData
{
    public class HistoryCoatingManager:Singleton<HistoryCoatingManager>
    {
        public List<CoatingData> CoatingData { get; set; } = new List<CoatingData>();
       
        HistoryCoatingSqlHelp historyCoatingSqlHelp { get; set; }=new HistoryCoatingSqlHelp();
        public HistoryCoatingManager()
        {
            
            
        }
        public void Initialize()
        {
            InitData();
        }
        private void InitData()
        {
            OP.Subscribe("HistoryCoatingManager.Grow", (string cmd, object[] args) => Grow(args));
            OP.Subscribe("HistoryCoatingManager.GrowCheck", (string cmd, object[] args) => GrowCheck(args));
        }
        /// <summary>
        /// 更新Tray和腔体的厚度，arg0 TrayInnerID arg1生长速率 arg2生长时间 arg3PM腔
        /// </summary>
        /// <param name="args"></param>
        /// <returns></returns>
        public bool Grow(object[] args)//Process完成后更新生长厚度，操作对象Tray PM
        {
            try
            {

                double GrowthRate = Convert.ToDouble(args[1]);
                double Seconds = Convert.ToDouble(args[2]);

                if (GrowthRate* Seconds==0)
                {
                    return true;
                }
                historyCoatingSqlHelp.GrowTray(args[0].ToString(), GrowthRate, Seconds);
                historyCoatingSqlHelp.GrowPM(args[3].ToString(), GrowthRate, Seconds);
                historyCoatingSqlHelp.GrowPMForMaintenance(args[3].ToString(), GrowthRate, Seconds);
                
                return true;
            }
            catch (Exception ex)
            {
                EV.PostInfoLog("System", $"CoatingManager.Grow_Error {ex.ToString()}");
                return false;
            }

        }
        public bool GrowCheck(object[] args)
        {
            string lab = "";
            if (historyCoatingSqlHelp.CheckExceedTray(args[0].ToString(), args[1].ToString(), out lab))
                CheckIsClear(lab);
            if (historyCoatingSqlHelp.CheckExceedPM(args[1].ToString(), out lab))
                CheckIsClear(lab);
            return true;
        }
        InspectCleanView inspectCleanView = new InspectCleanView();
        private void CheckIsClear(string lab) //检查当前厚度是否大于设定厚度，操作对象Tray PM,需要清理时弹窗提示
        {
            //在异步中弹窗
            Application.Current.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(() =>
            {
                inspectCleanView.AddToolTip(lab);
                //弹窗提示，不关闭时，文本内容递增，关闭后重新提示
                var wins = Application.Current.Windows.OfType<InspectCleanView>().ToArray();
                if (wins.Any())
                {
                    foreach (InspectCleanView w in wins)
                    {
                        if (w.WindowState == WindowState.Minimized)
                            w.WindowState = WindowState.Normal;
                        w.Show();
                        w.Activate();
                    }
                }
                else
                {
                    if (!inspectCleanView.IsActive)
                    {
                        inspectCleanView = new InspectCleanView();
                        inspectCleanView.AddToolTip(lab);
                        inspectCleanView.Show();
                    }
                }
            }));
        }
    }
}
