using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data;
using ICSharpCode.Core;
using Mesnac.Controls.Base;
using Mesnac.Action.Base;
using Mesnac.Codd.Session;
using Mesnac.Action.ChemicalWeighing.Entity;
using Mesnac.Action.ChemicalWeighing.Technical;

namespace Mesnac.Action.ChemicalWeighing.Product.PptPlan
{
    public class RefreshAction : ChemicalWeighingAction,IAction
    {
        #region 事件定义

        /// <summary>
        /// 刷新计划事件
        /// </summary>
        public static event EventHandler OnRefreshPlan;

        #endregion

        #region 字段定义

        private static bool IsFirstRun = true;  //是否首次执行
        private RuntimeParameter _runtime;
        //private DbMCControl _planDateControl = null;        //计划日期
        //private DbMCControl _pptShiftControl = null;        //班次
        private DbMCControl _clientGridControl = null;      //网格计划控件

        #endregion

        #region IAction接口实现
        public void Run(RuntimeParameter runtime)
        {
            base.RunIni(runtime);       //必须要调用的
            this._runtime = runtime;

            ICSharpCode.Core.LoggingService<RefreshAction>.Debug("当班计划—刷新计划业务...");

            #region 事件订阅

            if (true)
            {
                //通用调用刷新计划事件订阅

                Mesnac.Basic.InvokeHelper.OnRefreshPlan -= Process_Event;
                Mesnac.Basic.InvokeHelper.OnRefreshPlan += Process_Event;

                //添加计划后,要刷新计划
                InsertAction.OnInsertPlan -= Process_Event;
                InsertAction.OnInsertPlan += Process_Event;

                //下传计划后,要刷新计划
                DownloadAction.DownloadPlan -= Process_Event;
                DownloadAction.DownloadPlan += Process_Event;

                //删除计划后,要刷新本地计划
                DeleteAction.OnDeletePlan -= Process_Event;
                DeleteAction.OnDeletePlan += Process_Event;

                //移动计划后,要刷新本地计划
                AbstractMoveAction.OnMovePlan -= Process_Event;
                AbstractMoveAction.OnMovePlan += Process_Event;

                //修改次数数后,要刷新本地计划
                ModifyPlanNumAction.OnModifyPlanNum -= Process_Event;
                ModifyPlanNumAction.OnModifyPlanNum += Process_Event;

                //终止计划时,刷新本地计划
                StopAction.OnStopPlan -= Process_Event;
                StopAction.OnStopPlan += Process_Event;

                //冠合自动下传计划成功时,刷新本地计划
                ChemicalWeighingPlc.PlcPlanHelper.sender = runtime;
                ChemicalWeighingPlc.PlcPlanHelper.GHAutoDownloadPlan -= Process_Event;
                ChemicalWeighingPlc.PlcPlanHelper.GHAutoDownloadPlan += Process_Event;

                //冠合把PLC计划状态更新至数据库时,刷新本地计划
                FinishBatch.FinishBatchHelper.sender = runtime;
                FinishBatch.FinishBatchHelper.GHOnUpdatePlanStateFromPlc -= Process_Event;
                FinishBatch.FinishBatchHelper.GHOnUpdatePlanStateFromPlc += Process_Event;

                //自动下传计划成功时,刷新本地计划
                ChemicalWeighingPlc.PlcPlanHelper.sender = runtime;
                ChemicalWeighingPlc.PlcPlanHelper.AutoDownloadPlan -= Process_Event;
                ChemicalWeighingPlc.PlcPlanHelper.AutoDownloadPlan += Process_Event;

                //把PLC计划状态更新至数据库时,刷新本地计划
                ChemicalWeighingPlc.PlcPlanHelper.sender = runtime;
                ChemicalWeighingPlc.PlcPlanHelper.OnUpdatePlanStateFromPlc -= Process_Event;
                ChemicalWeighingPlc.PlcPlanHelper.OnUpdatePlanStateFromPlc += Process_Event;

                //HMI请求添加计划完成后,刷新本地计划
                ChemicalWeighingPlc.PlcPlanHelper.sender = runtime;
                ChemicalWeighingPlc.PlcPlanHelper.OnAutoAddPlan -= Process_Event;
                ChemicalWeighingPlc.PlcPlanHelper.OnAutoAddPlan += Process_Event;

                //HMI请求修改计划完成后,刷新本地计划
                ChemicalWeighingPlc.PlcPlanHelper.sender = runtime;
                ChemicalWeighingPlc.PlcPlanHelper.OnChangePlan -= Process_Event;
                ChemicalWeighingPlc.PlcPlanHelper.OnChangePlan += Process_Event;

                ModifyPlanInfoAction.OnModifyPlanInfo -= Process_Event;
                ModifyPlanInfoAction.OnModifyPlanInfo += Process_Event;

                //计划下传刷新本地计划
                PlanDownloadAction.DownloadPlan -= Process_Event;
                PlanDownloadAction.DownloadPlan += Process_Event;

                ReloadPlanInfo.ReloadPlan -= Process_Event;
                ReloadPlanInfo.ReloadPlan += Process_Event;
                IsFirstRun = false;
            }

            #endregion

            //DbMCControl planDateControl = this.GetDbMCControlByKey(Mesnac.Basic.DataSourceFactory.MCDbType.Local, "[RT_plan].[Plan_Date]").FirstOrDefault();
            //DbMCControl pptShiftControl = this.GetDbMCControlByKey(Mesnac.Basic.DataSourceFactory.MCDbType.Local, "Pmt_Shiftime").FirstOrDefault();
            DbMCControl clientGridControl = this.GetDbMCControlByKey(Mesnac.Basic.DataSourceFactory.MCDbType.Local, "Base_PlanInfo").FirstOrDefault();         //获取本机台计划控件

            //if (planDateControl == null)
            //{
            //    ICSharpCode.Core.LoggingService<RefreshAction>.Error("{当班计划-刷新计划} 缺少日期选择控件...");
            //    return;
            //}
            //if (pptShiftControl == null)
            //{
            //    ICSharpCode.Core.LoggingService<RefreshAction>.Error("{当班计划-刷新计划} 缺少班次组合框控件...");
            //    return;
            //}
            if (clientGridControl == null || !(clientGridControl.BaseControl is DataGridView))
            {
                ICSharpCode.Core.LoggingService<RefreshAction>.Error("{当班计划—刷新计划}缺少本机台计划网格控件...");
                return;
            }
            //this._planDateControl = planDateControl;
            //this._pptShiftControl = pptShiftControl;
            this._clientGridControl = clientGridControl;

            this.DoWork();
        }

        #endregion

        #region 方法定义

        /// <summary>
        /// 刷新计划
        /// </summary>
        protected void DoWork()
        {
            #region 业务实现

            //int shiftID = Global.PublicVar.Instance.globalShiftID;
            //int.TryParse(this._pptShiftControl.BaseControl.MCValue.ToString(), out shiftID);
            string equipCode = base.CurrEquipCode;      //当前机台
            //DateTime selectedDate = Convert.ToDateTime(this._planDateControl.BaseControl.MCValue); 
            //DataTable table = PlanHelper.GetPlanData(selectedDate, shiftID);
            //DataTable table = PlanHelper.GetPlanData(selectedDate);
            DataTable table = PlanHelper.GetPlanData();
            lock (String.Empty)
            {
                //本地计划
                if (this._clientGridControl != null && this._clientGridControl.BaseControl != null)
                {
                    this._clientGridControl.BaseControl.BindDataSource = null;
                    this._clientGridControl.BaseControl.BindDataSource = table;

                    #region 根据计划状态处理背景色

                    DataGridView clientGrid = this._clientGridControl.BaseControl as DataGridView;
                    PlanHelper.SetBackColor(clientGrid);

                    #endregion
                }
                else
                {
                    ICSharpCode.Core.LoggingService<RefreshAction>.Warn("刷新本地计划失败:本地计划控件为Null...");
                }
            }
            #endregion

            #region 触发事件, 刷新客户端计划

            Global.PublicVar.Instance.LocalPlanIsRefresh = true;            //更新机台计划刷新标志

            if (OnRefreshPlan != null)
            {
                OnRefreshPlan(this._runtime.BaseControl.MCRoot, System.EventArgs.Empty);
            }

            #endregion
        }

        #endregion

        #region 事件处理方法

        private void Process_Event(object sender, EventArgs e)
        {
            if (sender is RuntimeParameter)
            {
                this.Run(sender as RuntimeParameter);
            }
            else
            {
                this.Run(this._runtime);
            }
        }

        #endregion

    }
}