using ICSharpCode.Core;
using Mesnac.Action.Base;
using Mesnac.Action.ChemicalWeighing.Product.PptPlan.entity;
using Mesnac.Action.ChemicalWeighing.Technical.PmtRecipe;
using Mesnac.Action.ChemicalWeighing.Technical.PmtRecipe.entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Mesnac.Action.ChemicalWeighing.Product.PptPlan
{
    public class PlanDownloadAction : ChemicalWeighingAction, IAction
    {
        public static event EventHandler DownloadPlan;
        private RuntimeParameter _runtime;
        private DbMCControl _clientGridControl = null;      //网格计划控件

        private string selectedPlanId = null;//选中计划号
        private string selectedPlanState = null;


        public void Run(RuntimeParameter runtime)
        {
            base.RunIni(runtime);                   //必须要调用的
            ICSharpCode.Core.LoggingService<AutoDownloadAction>.Debug("下传计划信息...");

            //获取界面控件
            DbMCControl clientGridControl = this.GetDbMCControlByKey(Mesnac.Basic.DataSourceFactory.MCDbType.Local, "Base_PlanInfo").FirstOrDefault();
            if (clientGridControl == null)
            {
                ICSharpCode.Core.LoggingService<PlanDownloadAction>.Error("{生产计划-修改计划信息}  缺少计划列表网格控件...");
                return;
            }
            this._runtime = runtime;
            this._clientGridControl = clientGridControl;
            DataGridView clientGridView = this._clientGridControl.BaseControl as DataGridView;

            if (clientGridView.SelectedRows.Count != 1)
            {
                string msg1_1 = StringParser.Parse(ResourceService.GetString("Mesnac_Action_ChemicalWeighing_Product_PptPlan_ModifyPlanNumAction_msg1_1"));          //请选择
                MessageBox.Show(msg1_1, Mesnac.Basic.LanguageHelper.Caption, MessageBoxButtons.OK, MessageBoxIcon.Information);
                this._runtime.IsReturn = true;
                return;
            }
            
            //获取选中的计划号
            this.selectedPlanId = clientGridView.SelectedRows[0].Cells["plan_Id"].Value as string;
            this.selectedPlanState = clientGridView.SelectedRows[0].Cells["plan_state"].Value as string;


            if (this.selectedPlanState != "待执行")
            {
                if (this.selectedPlanState == "已完成")
                {
                    MessageBox.Show("计划已执行完成请勿重复下传", Mesnac.Basic.LanguageHelper.Caption, MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
            }
         
            //判断下位机准备好信号,正式调试时需取消注释
            /*if (!(BasePlcHelper.Instance.GH_PC_ConfirmRemote.NowValue.ToInt() == 1 && BasePlcHelper.Instance.GH_PC_ConfirmReady.NowValue.ToInt() == 1))
            {
                MessageBox.Show("下位机未准备好!!!");
                return;
            }*/

            //获取配方编号 recipe_id
            //string reciprId = clientGridView.SelectedRows[0].Cells["recipe_id"].Value as string;

            // writeToPlc(reciprId, this.selectedPlanId);
            //判断下传是否成功

            //if (reciprId == readRecipeId())
            //{
            //    ICSharpCode.Core.LoggingService<StopAction>.Debug("{生产计划-计划下传}  计划下传成功...");
            //    //下传成功后下传RpFinished信号
            //    bool writeRpFinished = BasePlcHelper.Instance.PlcWriteByDataKey(BasePlcHelper.Instance.ChemicalWeighing_PC_RpFinished, new object[] { true });
            //}
            //else
            //{
            //    MessageBox.Show("计划下传失败!!!");
            //}

            //开启线程读取计划执行状态
            //Thread t = new Thread(readPlanState);
            //t.Start();
        }

        /// <summary>
        /// 读取PLC执行的配方编号
        /// </summary>
        /// <returns></returns>
       

        /// <summary>
        /// 读取plc计划执行状态
        /// </summary>
        public void readPlanState()
        {
           
        }

    

        /// <summary>
        /// 将配方数据写入PLC地址
        /// </summary>
        /// <param name="recipe">配方编号</param>
        /// <param name="planId">计划号</param>
        public void writeToPlc(string recipe,string planId)
        {
            //获取配方信息、获取工艺参数信息
            Base_RecipeInfo info = RecipeHelper.GetRecipeById(recipe);

            //通过计划编号获取选中计划
            Base_PlanInfo planInfo = PlanHelper.getPlanInfoByPlanId(planId);

            

        }

        public string intArrayToStr(int[] array)
        {
            string str = "";
            if (array.Length > 0)
            {
                for (int i = 0; i < array.Length; i++)
                {
                    if (array[i] > 0)
                    {
                        str = str + array[i];
                    }
                }
            }
            return str;
        }
    }
}