package com.bonait.bnframework.modules.home.fragment.from;

import static com.bonait.bnframework.MainApplication.getContext;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.SystemClock;
import android.text.InputType;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.Chronometer;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bonait.bnframework.R;
import com.bonait.bnframework.business.ExecuteTheRecipe;
import com.bonait.bnframework.common.base.BaseActivity;
import com.bonait.bnframework.common.constant.ConfigName;
import com.bonait.bnframework.common.constant.DataBus;
import com.bonait.bnframework.common.constant.MessageName;
import com.bonait.bnframework.common.db.QueryDB;
import com.bonait.bnframework.common.db.mode.BPA_GOODS;
import com.bonait.bnframework.common.db.mode.BPA_GOODSRECIPE;
import com.bonait.bnframework.common.helper.I.IWriteCallBack;
import com.bonait.bnframework.common.helper.I.MyClickListener;
import com.bonait.bnframework.common.message.MessageManager;
import com.bonait.bnframework.common.model.mode.MouthFacingEnum;
import com.bonait.bnframework.common.model.mode.SpeedEnum;
import com.bonait.bnframework.common.utils.AlertDialogUtils;
import com.bonait.bnframework.common.utils.ToastUtils;
import com.bonait.bnframework.modules.home.adapter.gongxubuzhou_adapter;
import com.bonait.bnframework.modules.home.adapter.gongxubuzhou_adapter1;
import com.bonait.bnframework.modules.home.adapter.gongxubuzhou_adapter2;
import com.bonait.bnframework.modules.home.fragment.mode.fragment_gx;
import com.bonait.bnframework.modules.home.fragment.mode.imagebuttom;
import com.bonait.bnframework.modules.home.fragment.mode.imagebuttom3;
import com.bonait.bnframework.modules.home.fragment.mode.jingdutiao;
import com.bonait.bnframework.modules.home.fragment.mode.newhuoli_control;
import com.qmuiteam.qmui.widget.QMUITopBarLayout;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class DishTestActivity extends BaseActivity {

    //region 变量
    @BindView(R.id.device_image)
    ImageView device_image;

    @BindView(R.id.touliao_k)
    LinearLayout touliao_k;

    @BindView(R.id.touliao_m)
    LinearLayout touliao_m;

    @BindView(R.id.topbar)
    QMUITopBarLayout mTopBar;

    @BindView(R.id.chaoguo_wendu)
    TextView chaoguo_wendu;

    @BindView(R.id.text_ms)
    TextView text_ms;

    @BindView(R.id.fanchaoshudu_1)
    LinearLayout fanchaoshudu_1;

    @BindView(R.id.fanchaoshudu)
    imagebuttom3 fanchaoshudu;

    @BindView(R.id.fanchaoshudu_k)
    imagebuttom3 fanchaoshudu_k;

    @BindView(R.id.fanchaoshudu_z)
    imagebuttom3 fanchaoshudu_z;

    @BindView(R.id.fanchaoshudu_m)
    imagebuttom3 fanchaoshudu_m;

    @BindView(R.id.fanchaoshudu_t)
    imagebuttom3 fanchaoshudu_t;

//    @BindView(R.id.chushui)
//    imagebuttom3 chushui;

    @BindView(R.id.touliao1)
    imagebuttom3 touliao1;

    @BindView(R.id.touliao2)
    imagebuttom3 touliao2;

    @BindView(R.id.touliao3)
    imagebuttom3 touliao3;

    @BindView(R.id.touliao4)
    imagebuttom3 touliao4;

    @BindView(R.id.yeliao1)
    imagebuttom3 yeliao1;
    @BindView(R.id.yeliao2)
    imagebuttom3 yeliao2;
    @BindView(R.id.yeliao3)
    imagebuttom3 yeliao3;

    @BindView(R.id.yeliao4)
    imagebuttom3 yeliao4;

    @BindView(R.id.huoli)
    newhuoli_control huoli;

    @BindView(R.id.start_goodmake)
    imagebuttom start_goodmake;//开始

    @BindView(R.id.zanting_goodmake)
    public imagebuttom zanting_goodmake;//暂停

    @BindView(R.id.runtime)
    Chronometer runtime;//计时器 计时器

    @BindView(R.id.kaishiorstop)
    RelativeLayout kaishiorstop;//开始或结束

    @BindView(R.id.zhixingzhong)
    RelativeLayout zhixingzhong;//执行中

    @BindView(R.id.guoneichoushui)
    RelativeLayout guoneichoushui;//锅内抽水

    @BindView(R.id.runtime_cs)
    Chronometer runtime_cs;

    @BindView(R.id.datatab_gxbz)
    ListView datatab_gxbz;

    @BindView(R.id.datatab_gxbz1)
    ListView datatab_gxbz1;

    public int ShowIndex = 0;

    @BindView(R.id.goodname)
    TextView goodname;
    @BindView(R.id.caipingmingchen)
    RelativeLayout caipingmingchen;

    @BindView(R.id.caipinmingchen_text)
    EditText caipinmingchen_text;

    @BindView(R.id.liaohedata)
    RelativeLayout liaohedata;

    @BindView(R.id.liaohedata_num)
    TextView liaohedata_num;

    @BindView(R.id.liaohedata_text)
    EditText liaohedata_text;

    @BindView(R.id.liaohe1)
    TextView liaohe1;
    public String liaohetext1 = "";

    @BindView(R.id.liaohe2)
    TextView liaohe2;
    public String liaohetext2 = "";

    @BindView(R.id.liaohe3)
    TextView liaohe3;
    public String liaohetext3 = "";


    @BindView(R.id.liaohe4)
    TextView liaohe4;
    public String liaohetext4 = "";


    @BindView(R.id.gongyibuzhoujilu)
    RelativeLayout gongyibuzhoujilu;//记录

    @BindView(R.id.shoudong_touliao)
    imagebuttom3 shoudong_touliao;//手动投料


    @BindView(R.id.guokoucaoxiang_1)
    LinearLayout guokoucaoxiang_1;

    @BindView(R.id.guokoucaoxiang)
    imagebuttom3 guokoucaoxiang;

    @BindView(R.id.guokoucaoxiang_cc)
    imagebuttom3 guokoucaoxiang_cc;

    @BindView(R.id.guokoucaoxiang_cl)
    imagebuttom3 guokoucaoxiang_cl;

    @BindView(R.id.guokoucaoxiang_dc)
    imagebuttom3 guokoucaoxiang_dc;

    @BindView(R.id.guokoucaoxiang_qx)
    imagebuttom3 guokoucaoxiang_qx;

    @BindView(R.id.check_PF)
    CheckBox check_PF;
    //endregion

    //region 界面实例
    private Context context;
    public Activity activity = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dishtest);
        ButterKnife.bind(this);
        context = this;
        activity = this;
        initTopBar();
        initData();
        SxUIThread();
    }
    //endregion

    //region 私有方法

    /**
     * 初始化顶部弹框
     */
    private void initTopBar() {
        mTopBar.setBackgroundColor(ContextCompat.getColor(context, R.color.topbj1));
        mTopBar.setTitle("菜品研发");
        mTopBar.addLeftImageButton(R.mipmap.fanhui, R.id.topbar).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (IsStart) {
                    ToastUtils.warning("请等待制作完成后退出...");
                    return;
                }
                finish();
            }
        });


        fanchaoshudu.mListener = myClickListener;
        fanchaoshudu_k.mListener = myClickListener;
        fanchaoshudu_z.mListener = myClickListener;
        fanchaoshudu_m.mListener = myClickListener;
        fanchaoshudu_t.mListener = myClickListener;

        guokoucaoxiang.mListener = myClickListener;
        guokoucaoxiang_cc.mListener = myClickListener;
        guokoucaoxiang_cl.mListener = myClickListener;
        guokoucaoxiang_dc.mListener = myClickListener;
        guokoucaoxiang_qx.mListener = myClickListener;

        //chushui.mListener = myClickListener;

        touliao1.mListener = myClickListener;
        touliao2.mListener = myClickListener;
        touliao3.mListener = myClickListener;
        touliao4.mListener = myClickListener;

        yeliao1.mListener = myClickListener;
        yeliao2.mListener = myClickListener;
        yeliao3.mListener = myClickListener;
        yeliao4.mListener = myClickListener;

        huoli.mListener = myClickListener;

        start_goodmake.mListener = myClickListener;
        zanting_goodmake.mListener = myClickListener;

        shoudong_touliao.mListener = myClickListener;
    }

    /**
     * 初始化显示数据
     */
    private void initData() {
        if (ConfigName.getInstance().versionSelectionEnum.equals("大炒版本") || ConfigName.getInstance().versionSelectionEnum.contains("一拖")) {
            touliao_k.setVisibility(View.GONE);
            touliao_m.setVisibility(View.GONE);
            device_image.setImageResource(R.mipmap.device00);
        } else {
            touliao_k.setVisibility(View.VISIBLE);
            touliao_m.setVisibility(View.VISIBLE);
            device_image.setImageResource(R.mipmap.device1);
        }

        DataBus.getInstance().ShowGoodName = "";
        SetStirFrySpeed(SpeedEnum.停, false);

        yeliao1.setTag(ConfigName.getInstance().SilosDisplayMaterial.get(1) + "-按钮");
        yeliao1.Init();

        yeliao2.setTag(ConfigName.getInstance().SilosDisplayMaterial.get(2) + "-按钮");
        yeliao2.Init();

        yeliao3.setTag(ConfigName.getInstance().SilosDisplayMaterial.get(3) + "-按钮");
        yeliao3.Init();

        yeliao4.setTag(ConfigName.getInstance().SilosDisplayMaterial.get(4) + "-按钮");
        yeliao4.Init();

        ShowIndex = 0;
        SetShowNameData(ShowIndex);
        gongyibuzhoujilu.setVisibility(View.GONE);
    }

    public gongxubuzhou_adapter1 gxbz_adapter = null;
    public gongxubuzhou_adapter2 gxbz_adapter2 = null;

    /**
     * 刷新工序
     */
    public void RefreshProcedure() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                ArrayList<BPA_GOODSRECIPE> cipe = new ArrayList<>();
                for (BPA_GOODSRECIPE item : DataBus.getInstance().bpa_goodsrecipes) {
                    cipe.add(item);
                }
                Collections.reverse(cipe);
                gxbz_adapter = new gongxubuzhou_adapter1(getContext(), R.layout.newgx_item, cipe);
                datatab_gxbz.setAdapter(gxbz_adapter);

                gxbz_adapter2 = new gongxubuzhou_adapter2(getContext(), R.layout.newgx_item1, DataBus.getInstance().bpa_goodsrecipes);
                datatab_gxbz1.setAdapter(gxbz_adapter2);
            }
        });
    }

    /**
     * 刷新UI界面
     */
    public void SxUIThread() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    try {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Object val_wd = ExecuteTheRecipe.getListingValue("当前锅底温度");
                                if (val_wd != null) {
                                    chaoguo_wendu.setText(val_wd + "°C");
                                } else {
                                    chaoguo_wendu.setText("??°C");
                                }
                            }
                        });
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        ToastUtils.info("异常信息:" + e.getMessage());
                    }
                }
            }
        }).start();
    }

    /**
     * 点击事件
     */
    public MyClickListener myClickListener = new MyClickListener() {
        @Override
        public void clickListener(View v, Object data) {
            boolean isclick = false;
            int hl = 0;
            if (v.getId() == R.id.huoli) {
                hl = (int) data;
            } else {
                isclick = (boolean) data;
            }
            switch (v.getId()) {
                case R.id.fanchaoshudu:
                    fanchaoshudu_1.setVisibility(isclick ? View.VISIBLE : View.GONE);
                    break;
                case R.id.fanchaoshudu_k:
                    SetStirFrySpeed(SpeedEnum.快, isclick);
                    break;
                case R.id.fanchaoshudu_z:
                    SetStirFrySpeed(SpeedEnum.中, isclick);
                    break;
                case R.id.fanchaoshudu_m:
                    SetStirFrySpeed(SpeedEnum.慢, isclick);
                    break;
                case R.id.fanchaoshudu_t:
                    SetStirFrySpeed(SpeedEnum.停, isclick);
                    break;
                case R.id.chushui:
                    PumpWater(true);
                    break;
                case R.id.touliao1:
                    MainDish(1);
                    break;
                case R.id.touliao2:
                    MainDish(2);
                    break;
                case R.id.touliao3:
                    MainDish(3);
                    break;
                case R.id.touliao4:
                    MainDish(4);
                    break;

                case R.id.yeliao1:
                    EditTextDialogBuilder(1);
                    break;
                case R.id.yeliao2:
                    EditTextDialogBuilder(2);
                    break;
                case R.id.yeliao3:
                    EditTextDialogBuilder(3);
                    break;
                case R.id.yeliao4:
                    EditTextDialogBuilder(4);
                    break;
                case R.id.huoli:
                    SetFire(hl);
                    break;
                case R.id.start_goodmake:
                    if (isclick) {
                        onRecordStart();
                    } else {
                        onRecordStop();
                    }
                    break;
                case R.id.zanting_goodmake:
                    if (isclick) {
                        onRecordPause();
                    } else {
                        onRecordPauseStart();
                    }
                    break;
                case R.id.shoudong_touliao://手动投料
                    ShouDongMainDish();
                    break;

                case R.id.guokoucaoxiang:
                    guokoucaoxiang_1.setVisibility(isclick ? View.VISIBLE : View.GONE);
                    break;
                case R.id.guokoucaoxiang_cc:
                    SetMouthFacing(MouthFacingEnum.炒菜位);
                    break;
                case R.id.guokoucaoxiang_cl:
                    SetMouthFacing(MouthFacingEnum.抽料位);
                    break;
                case R.id.guokoucaoxiang_dc:
                    SetMouthFacing(MouthFacingEnum.倒菜位);
                    break;
                case R.id.guokoucaoxiang_qx:
                    SetMouthFacing(MouthFacingEnum.清洗位);
                    break;

            }
        }

        @Override
        public void clickListenerNew(View v, int k, Object data) {

        }
    };

    /**
     * 点击事件
     *
     * @param view
     */
    @OnClick({R.id.tingzhijiashui, R.id.click_cpmc, R.id.click_lhdata, R.id.click_savemncz
            , R.id.click_lhdata_fh, R.id.goodname, R.id.close_from})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tingzhijiashui://停止加水
                PumpWater(false);
                break;
            case R.id.click_cpmc://菜品名称保存
                String data = caipinmingchen_text.getText().toString();
                if (data.isEmpty()) {
                    ToastUtils.warning("菜品名称不能为空！");
                    return;
                }
                DataBus.getInstance().ShowGoodName = data;
                ShowIndex = 1;
                SetShowNameData(1);
                break;
            case R.id.click_lhdata:
                String data1 = liaohedata_text.getText().toString();

                switch (ShowIndex) {
                    case 1:
                        liaohetext1 = data1 + "";
                        break;
                    case 2:
                        liaohetext2 = data1 + "";
                        break;
                    case 3:
                        liaohetext3 = data1 + "";
                        break;
                    case 4:
                        liaohetext4 = data1 + "";
                        break;
                }
                ShowIndex++;
                SetShowNameData(ShowIndex);
                break;
            case R.id.click_lhdata_fh:
                ShowIndex--;
                SetShowNameData(ShowIndex);
                break;
            case R.id.goodname:
                ShowIndex = 0;
                SetShowNameData(ShowIndex);
                break;
            case R.id.click_savemncz://保存菜谱并模拟炒制
                //按钮点击
                String title = "生成菜谱操作提示！";
                String message = "请问客官确定要生成[" + DataBus.getInstance().ShowGoodName + "]菜谱吗?";
                AlertDialogUtils.showDialog(view.getContext(), title, message, new QMUIDialogAction.ActionListener() {
                    @Override
                    public void onClick(QMUIDialog dialog, int index) {
                        boolean isSucess = QueryDB.GetGoodsIs(DataBus.getInstance().ShowGoodName);
                        if (isSucess) {
                            DataBus.getInstance().ShowGoodName = DataBus.getInstance().ShowGoodName + "_副本";
                        }

                        int sc = DataBus.getInstance().TimeOut <= 0 ? 180 : DataBus.getInstance().TimeOut;

                        ArrayList<BPA_GOODS> goods = QueryDB.GetGoodsALL();
                        BPA_GOODS good1 = new BPA_GOODS();
                        good1.name = DataBus.getInstance().ShowGoodName;
                        good1.status = 1;
                        good1.sort = goods.size() + 1;
                        good1.maketime = sc;
                        good1.issc = 0;
                        good1.url = "";
                        QueryDB.AddGoods(good1);
                        for (int k = 0; k < DataBus.getInstance().bpa_goodsrecipes.size(); k++) {
                            BPA_GOODSRECIPE item = DataBus.getInstance().bpa_goodsrecipes.get(k);
                            item.id = java.util.UUID.randomUUID().toString();
                            item.goodsID = good1.id;
                            item.sort = k + 1;
                            QueryDB.AddGoodsSrecipe(item);
                        }
                        ToastUtils.info("菜谱保存成功!");
                        dialog.dismiss();
                        MessageManager.getInstance().sendMessage(MessageName.ScGood, "Good");

                        Intent intent = new Intent(getContext(), CookingSimulatedActivity.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        intent.putExtra("id", good1.id);
                        startActivity(intent);
                        finish();
                    }
                });
                break;
            case R.id.close_from:
                gongyibuzhoujilu.setVisibility(View.GONE);
                break;
        }
    }

    /**
     * 设置显示数据名称
     *
     * @param index
     */
    public void SetShowNameData(int index) {
        switch (index) {
            case 0:
                caipingmingchen.setVisibility(View.VISIBLE);
                liaohedata.setVisibility(View.GONE);
                break;
            case 1:
                caipingmingchen.setVisibility(View.GONE);
                liaohedata.setVisibility(View.VISIBLE);

                liaohe1.setVisibility(liaohetext1.isEmpty() ? View.INVISIBLE : View.VISIBLE);
                liaohe2.setVisibility(liaohetext2.isEmpty() ? View.INVISIBLE : View.VISIBLE);
                liaohe3.setVisibility(liaohetext3.isEmpty() ? View.INVISIBLE : View.VISIBLE);
                liaohe4.setVisibility(liaohetext4.isEmpty() ? View.INVISIBLE : View.VISIBLE);

                liaohe1.setText(liaohetext1 + "");
                liaohe2.setText(liaohetext2 + "");
                liaohe3.setText(liaohetext3 + "");
                liaohe4.setText(liaohetext4 + "");

                liaohedata_num.setText("①号料盒");
                liaohedata_text.setText(liaohetext1 + "");
                break;
            case 2:
                caipingmingchen.setVisibility(View.GONE);
                liaohedata.setVisibility(View.VISIBLE);

                liaohe1.setVisibility(liaohetext1.isEmpty() ? View.INVISIBLE : View.VISIBLE);
                liaohe2.setVisibility(liaohetext2.isEmpty() ? View.INVISIBLE : View.VISIBLE);
                liaohe3.setVisibility(liaohetext3.isEmpty() ? View.INVISIBLE : View.VISIBLE);
                liaohe4.setVisibility(liaohetext4.isEmpty() ? View.INVISIBLE : View.VISIBLE);
                liaohe1.setText(liaohetext1 + "");
                liaohe2.setText(liaohetext2 + "");
                liaohe3.setText(liaohetext3 + "");
                liaohe4.setText(liaohetext4 + "");
                liaohedata_num.setText("②号料盒");
                liaohedata_text.setText(liaohetext2 + "");
                break;
            case 3:
                caipingmingchen.setVisibility(View.GONE);
                liaohedata.setVisibility(View.VISIBLE);

                liaohe1.setVisibility(liaohetext1.isEmpty() ? View.INVISIBLE : View.VISIBLE);
                liaohe2.setVisibility(liaohetext2.isEmpty() ? View.INVISIBLE : View.VISIBLE);
                liaohe3.setVisibility(liaohetext3.isEmpty() ? View.INVISIBLE : View.VISIBLE);
                liaohe4.setVisibility(liaohetext4.isEmpty() ? View.INVISIBLE : View.VISIBLE);
                liaohe1.setText(liaohetext1 + "");
                liaohe2.setText(liaohetext2 + "");
                liaohe3.setText(liaohetext3 + "");
                liaohe4.setText(liaohetext4 + "");
                liaohedata_num.setText("③号料盒");
                liaohedata_text.setText(liaohetext3 + "");
                break;
            case 4:
                caipingmingchen.setVisibility(View.GONE);
                liaohedata.setVisibility(View.VISIBLE);

                liaohe1.setVisibility(liaohetext1.isEmpty() ? View.INVISIBLE : View.VISIBLE);
                liaohe2.setVisibility(liaohetext2.isEmpty() ? View.INVISIBLE : View.VISIBLE);
                liaohe3.setVisibility(liaohetext3.isEmpty() ? View.INVISIBLE : View.VISIBLE);
                liaohe4.setVisibility(liaohetext4.isEmpty() ? View.INVISIBLE : View.VISIBLE);
                liaohe1.setText(liaohetext1 + "");
                liaohe2.setText(liaohetext2 + "");
                liaohe3.setText(liaohetext3 + "");
                liaohe4.setText(liaohetext4 + "");
                liaohedata_num.setText("④号料盒");
                liaohedata_text.setText(liaohetext4 + "");
                break;
            case 5:
                caipingmingchen.setVisibility(View.GONE);
                liaohedata.setVisibility(View.GONE);

                goodname.setText(DataBus.getInstance().ShowGoodName + "");
                touliao1.setTag(liaohetext1 + "-按钮");
                touliao1.Init();

                touliao2.setTag(liaohetext2 + "-按钮");
                touliao2.Init();

                touliao3.setTag(liaohetext3 + "-按钮");
                touliao3.Init();

                touliao4.setTag(liaohetext4 + "-按钮");
                touliao4.Init();
                break;
        }
    }
    //endregion

    //region 计时器
    /**
     * 是否启动
     */
    public static boolean IsStart = false;
    /**
     * 是否暂停
     */
    public static boolean IsPause = false;
    /**
     * 当前运行总时长
     */
    public int TimeOut = 0;
    /**
     * 上一次点击时间
     */
    public int ClikTime = 0;
    /**
     * 记录下来的总时间
     */
    private long recordingTime = 0;

    /**
     * 启动定时器
     */
    public void onRecordStart() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                runtime.setBase(SystemClock.elapsedRealtime() - recordingTime);// 跳过已经记录了的时间，起到继续计时的作用
                runtime.start();
                text_ms.setText("请先{开始}录制");
                //重置暂停开关
                IsPause = false;
                zanting_goodmake.SetStatus(false);
                zanting_goodmake.setVisibility(View.VISIBLE);
                ExecuteTheRecipe.WritePLC("暂停开关", false, null);

                IsStart = true;
                kaishiorstop.setVisibility(View.GONE);

                DataBus.getInstance().bpa_goodsrecipes.clear();//清空所有工序
                //刷新工序
                RefreshProcedure();
                TimeOut = 0;
                ClikTime = 0;
            }
        });
    }

    /**
     * 停止计时器
     */
    public void onRecordStop() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                runtime.stop();
                start_goodmake.SetStatus(false);
                IsStart = false;
                kaishiorstop.setVisibility(View.VISIBLE);
                text_ms.setText("请先{开始}录制");


                recordingTime = 0;
                long elapsedMillis = SystemClock.elapsedRealtime() - runtime.getBase();
                TimeOut = (int) (elapsedMillis / 1000);
                DataBus.getInstance().TimeOut = TimeOut;

                //重置暂停开关
                IsPause = false;
                zanting_goodmake.SetStatus(false);
                zanting_goodmake.setVisibility(View.INVISIBLE);
                ExecuteTheRecipe.WritePLC("暂停开关", false, null);

                ExecuteTheRecipe.WritePLC("搅拌", false, null);
                ExecuteTheRecipe.WritePLC("加热", false, null);
                ConfigName.getInstance().IsOpenHuoLi = false;
                ExecuteTheRecipe.BottomClick("平移-去1号位");
            }
        });
        //自动出餐
        AutomaticServing();
        gongyibuzhoujilu.setVisibility(View.VISIBLE);
    }

    /**
     * 暂停计时器
     */
    public void onRecordPause() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                runtime.stop();
                text_ms.setText("录制{暂停}中");
                recordingTime = SystemClock.elapsedRealtime() - runtime.getBase();// 保存这次记录了的时间
                IsPause = true;
                kaishiorstop.setVisibility(View.VISIBLE);

                ExecuteTheRecipe.WritePLC("暂停开关", true, null);
            }
        });
    }

    /**
     * 继续计时器
     */
    public void onRecordPauseStart() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                runtime.setBase(SystemClock.elapsedRealtime() - recordingTime);// 跳过已经记录了的时间，起到继续计时的作用
                runtime.start();
                text_ms.setText("请先{开始}录制");
                IsPause = false;
                kaishiorstop.setVisibility(View.GONE);
                ExecuteTheRecipe.WritePLC("暂停开关", false, null);
            }
        });
    }

    /**
     * 启动定时器
     */
    public void onRecordStart_CS() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                guoneichoushui.setVisibility(View.VISIBLE);
                runtime_cs.setBase(SystemClock.elapsedRealtime());
                runtime_cs.start();
                IsChouShui = true;
            }
        });
    }

    /**
     * 停止计时器
     */
    public void onRecordStop_CS() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                guoneichoushui.setVisibility(View.GONE);
                runtime_cs.stop();

                long elapsedMillis = SystemClock.elapsedRealtime() - runtime_cs.getBase();
                DataBus.getInstance().PumpWaterTimeOut = (int) (elapsedMillis / 1000);

                ClikTime = (int) ((SystemClock.elapsedRealtime() - runtime.getBase()) / 1000);
                if (IsChouShui) {
                    BPA_GOODSRECIPE data = Get加水(DataBus.getInstance().PumpWaterTimeOut);
                    DataBus.getInstance().bpa_goodsrecipes.add(data);
                }
                IsChouShui = false;
            }
        });
    }
    //endregion

    //region 公共方法

    /**
     * 设置翻炒速度
     * int 60 快 中 慢 停
     *
     * @param
     */
    public void SetStirFrySpeed(SpeedEnum speed, boolean isselectd) {
        int _speed = 0;
        if (isselectd) {
            switch (speed) {
                case 快:
                    _speed = 60;
                    //fanchaoshudu_k.SetStatus(true);
                    fanchaoshudu_z.SetStatus(false);
                    fanchaoshudu_m.SetStatus(false);
                    fanchaoshudu_t.SetStatus(false);
                    fanchaoshudu.setTag("翻炒速度 快-按钮");
                    break;
                case 中:
                    _speed = 40;
                    fanchaoshudu_k.SetStatus(false);
                    //fanchaoshudu_z.SetStatus(true);
                    fanchaoshudu_m.SetStatus(false);
                    fanchaoshudu_t.SetStatus(false);
                    fanchaoshudu.setTag("翻炒速度 中-按钮");
                    break;
                case 慢:
                    _speed = 20;
                    fanchaoshudu_k.SetStatus(false);
                    fanchaoshudu_z.SetStatus(false);
                    //fanchaoshudu_m.SetStatus(true);
                    fanchaoshudu_t.SetStatus(false);
                    fanchaoshudu.setTag("翻炒速度 慢-按钮");
                    break;
                case 停:
                    _speed = 0;
                    fanchaoshudu_k.SetStatus(false);
                    fanchaoshudu_z.SetStatus(false);
                    fanchaoshudu_m.SetStatus(false);
                    //fanchaoshudu_t.SetStatus(true);
                    fanchaoshudu.setTag("翻炒速度 停-按钮");
                    break;
            }
        } else {
            _speed = 0;
            fanchaoshudu_k.SetStatus(false);
            fanchaoshudu_z.SetStatus(false);
            fanchaoshudu_m.SetStatus(false);
            fanchaoshudu_t.SetStatus(true);
            fanchaoshudu.setTag("翻炒速度 停-按钮");
        }
        fanchaoshudu_1.setVisibility(View.GONE);
        fanchaoshudu.Init();

        if (IsStart) {
            SetZhiXingZhong(true);

            int time = (int) ((SystemClock.elapsedRealtime() - runtime.getBase()) / 1000);//当前点击多少秒
            int time_c = time - ClikTime;

            if (time_c > 0)//如果大于0，那么加一个延迟
            {
                DataBus.getInstance().bpa_goodsrecipes.add(Get延迟(time_c));
            }

            String sdstr = Get速度(_speed);
            BPA_GOODSRECIPE data = Get搅拌(sdstr);
            DataBus.getInstance().bpa_goodsrecipes.add(data);
            ExecuteTheRecipe.ExecuteOperationSteps(data.processname, data.processvalue);

            SetZhiXingZhong(false);

            //刷新工序
            RefreshProcedure();
        }
    }

    /**
     * 投主菜品
     *
     * @param num
     */
    public void MainDish(int num) {
//        Object sb = ExecuteTheRecipe.ReadPLC("平移轴在1号位");
//        if (sb != null) {
//            if ((boolean) (sb)) {
//
//            } else {
//                ToastUtils.warning("平移轴未复位!!!");
//            }
//        } else {
//            ToastUtils.warning("PLC未连接!!!");
//        }

        SetZhiXingZhong(true);

        int time = (int) ((SystemClock.elapsedRealtime() - runtime.getBase()) / 1000);//当前点击多少秒
        int time_c = time - ClikTime;
        new Thread(new Runnable() {
            @Override
            public void run() {

                BPA_GOODSRECIPE data1 = Get位置("高速", "炒菜位1");
                //DataBus.getInstance().bpa_goodsrecipes.add(data1);
                ExecuteTheRecipe.ExecuteOperationSteps(data1.processname, data1.processvalue);

                BPA_GOODSRECIPE data = Get主料(num);

                ExecuteTheRecipe.ExecuteOperationSteps(data.processname, data.processvalue);

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (time_c > 0)//如果大于0，那么加一个延迟
                        {
                            DataBus.getInstance().bpa_goodsrecipes.add(Get延迟(time_c));
                        }
                        DataBus.getInstance().bpa_goodsrecipes.add(data);
                        ToastUtils.warning("主料：" + num + "号仓投放");
                        SetZhiXingZhong(false);
                        //刷新工序
                        RefreshProcedure();
                    }
                });
            }
        }).start();

    }

    /**
     * 手动投主菜品
     *
     * @param
     */
    public void ShouDongMainDish() {
        int time = (int) ((SystemClock.elapsedRealtime() - runtime.getBase()) / 1000);//当前点击多少秒
        int time_c = time - ClikTime;

        if (time_c > 0)//如果大于0，那么加一个延迟
        {
            DataBus.getInstance().bpa_goodsrecipes.add(Get延迟(time_c));
        }
        DataBus.getInstance().bpa_goodsrecipes.add(Get手动主料());
        ExecuteTheRecipe.WritePLC("暂停开关", true, null);
        //刷新工序
        RefreshProcedure();
        String title = "手动投料-温馨提示！";
        String message = "客官请投入主料,投入后点击[确定]继续流程！";
        new QMUIDialog.MessageDialogBuilder(context)
                .setCancelable(false)
                .setCanceledOnTouchOutside(false)
                .setTitle(title)
                .setMessage(message)
                .addAction("确定", new QMUIDialogAction.ActionListener() {
                    @Override
                    public void onClick(QMUIDialog dialog, int index) {
                        ExecuteTheRecipe.WritePLC("暂停开关", false, null);
                        ClikTime = (int) ((SystemClock.elapsedRealtime() - runtime.getBase()) / 1000);
                        dialog.dismiss();
                    }
                })
                .create(com.qmuiteam.qmui.R.style.QMUI_Dialog).show();

    }

    /**
     * 设置锅口朝向
     *
     * @param
     */
    public void SetMouthFacing(MouthFacingEnum mouth) {
        SetZhiXingZhong(true);
        guokoucaoxiang_1.setVisibility(View.GONE);
        String name="原点位";
        switch (mouth) {
            case 炒菜位:
                name="炒菜位1";
                break;
            case 抽料位:
                name="抽料位";
                break;
            case 倒菜位:
                name="倒菜位";
                break;
            case 清洗位:
                name="清洗位";
                break;
        }
        int time = (int) ((SystemClock.elapsedRealtime() - runtime.getBase()) / 1000);//当前点击多少秒
        int time_c = time - ClikTime;
        String finalName = name;
        new Thread(new Runnable() {
            @Override
            public void run() {
                BPA_GOODSRECIPE data1 = Get位置("高速", finalName);
                ExecuteTheRecipe.ExecuteOperationSteps(data1.processname, data1.processvalue);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (time_c > 0)//如果大于0，那么加一个延迟
                        {
                            DataBus.getInstance().bpa_goodsrecipes.add(Get延迟(time_c));
                        }
                        DataBus.getInstance().bpa_goodsrecipes.add(data1);
                        SetZhiXingZhong(false);
                        //刷新工序
                        RefreshProcedure();
                    }
                });
            }
        }).start();
    }

    /**
     * 抽料
     *
     * @param
     */
    public void Pumping(String name, String yl) {
        SetZhiXingZhong(true);
        int time = (int) ((SystemClock.elapsedRealtime() - runtime.getBase()) / 1000);//当前点击多少秒
        int time_c = time - ClikTime;

        new Thread(new Runnable() {
            @Override
            public void run() {


                BPA_GOODSRECIPE data1 = Get位置("高速", "抽料位");
                //DataBus.getInstance().bpa_goodsrecipes.add(data1);
                ExecuteTheRecipe.ExecuteOperationSteps(data1.processname, data1.processvalue);

                BPA_GOODSRECIPE cipe = GetSelectItemFromValue(name, yl);

                ExecuteTheRecipe.ExecuteMaterialIssuance(cipe.processvalue);

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (time_c > 0)//如果大于0，那么加一个延迟
                        {
                            DataBus.getInstance().bpa_goodsrecipes.add(Get延迟(time_c));
                        }
                        DataBus.getInstance().bpa_goodsrecipes.add(cipe);

                        ToastUtils.warning("投料：" + name + ",用量：" + yl);
                        SetZhiXingZhong(false);
                        RefreshProcedure();
                    }
                });
            }
        }).start();
    }

    boolean IsChouShui = false;

    /**
     * 抽水
     */
    public void PumpWater(boolean isselectd) {
        if (isselectd) {

            int time = (int) ((SystemClock.elapsedRealtime() - runtime.getBase()) / 1000);//当前点击多少秒
            int time_c = time - ClikTime;
            new Thread(new Runnable() {
                @Override
                public void run() {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            guoneichoushui.setVisibility(View.VISIBLE);
                        }
                    });
                }
            }).start();
            new Thread(new Runnable() {
                @Override
                public void run() {
                    BPA_GOODSRECIPE data1 = Get位置("高速", "抽料位");
                    ExecuteTheRecipe.ExecuteOperationSteps(data1.processname, data1.processvalue);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (time_c > 0)//如果大于0，那么加一个延迟
                            {
                                DataBus.getInstance().bpa_goodsrecipes.add(Get延迟(time_c));
                            }
                            onRecordStart_CS();
                        }
                    });
                }
            }).start();
        } else {
            onRecordStop_CS();
        }
        ExecuteTheRecipe.WritePLC("炒锅抽水", isselectd, null);
        ToastUtils.warning("锅内加水：" + isselectd);
        //刷新工序
        RefreshProcedure();
    }

    /**
     * 设置火力
     *
     * @param data
     */
    public void SetFire(int data) {
        long elapsedMillis = SystemClock.elapsedRealtime() - runtime.getBase();
        int time = (int) (elapsedMillis / 1000);//当前点击多少秒
        int time_c = time - ClikTime;

        if (time_c > 0)//如果大于0，那么加一个延迟
        {
            DataBus.getInstance().bpa_goodsrecipes.add(Get延迟(time_c));
        }

        String sdstr = Get挡位((int) data);
        BPA_GOODSRECIPE data1 = Get加热(sdstr);
        DataBus.getInstance().bpa_goodsrecipes.add(data1);
        ClikTime = time;
        ExecuteTheRecipe.Write_PLC_HuoLI((int) data);

        //刷新工序
        RefreshProcedure();
    }

    /**
     * 自动出餐
     */
    public void AutomaticServing() {
        int time = (int) ((SystemClock.elapsedRealtime() - runtime.getBase()) / 1000);//当前点击多少秒
        int time_c = time - ClikTime;
        BPA_GOODSRECIPE data = Get出菜(time_c);
        DataBus.getInstance().bpa_goodsrecipes.add(data);

        BPA_GOODSRECIPE data2 = Get清洗(0);
        if(check_PF.isChecked())
        {
            DataBus.getInstance().bpa_goodsrecipes.add(data2);
        }

        new Thread(new Runnable() {
            @Override
            public void run() {
                ExecuteTheRecipe.ExecuteOperationSteps(data.processname, data.processvalue);
                if(check_PF.isChecked())
                {
                    ExecuteTheRecipe.ExecuteOperationSteps(data2.processname, data2.processvalue);
                }
            }
        }).start();
    }

    /**
     * 获取延迟
     *
     * @param time
     * @return
     */
    public BPA_GOODSRECIPE Get延迟(int time) {
        BPA_GOODSRECIPE pf = new BPA_GOODSRECIPE();
        pf.materialType = 1;
        pf.processname = "延迟";
        pf.processms = "延迟(" + time + ")";
        pf.processvalue = "延迟(秒)," + time;
        return pf;
    }

    /**
     * 获取加水
     *
     * @param time
     * @return
     */
    public BPA_GOODSRECIPE Get加水(int time) {
        BPA_GOODSRECIPE pf = new BPA_GOODSRECIPE();
        pf.materialType = 1;
        pf.processname = "加水";
        pf.processms = "加水(" + time + ")";
        pf.processvalue = "时长(秒)," + time;
        return pf;
    }

    /**
     * 获取主料
     *
     * @param wz
     * @return
     */
    public BPA_GOODSRECIPE Get主料(int wz) {
        String ms = "未知";
        if (wz == 1) {
            if (!liaohetext1.isEmpty()) {
                ms = liaohetext1;
            }
        } else if (wz == 2) {
            if (!liaohetext2.isEmpty()) {
                ms = liaohetext2;
            }
        } else if (wz == 3) {
            if (!liaohetext3.isEmpty()) {
                ms = liaohetext3;
            }
        } else if (wz == 4) {
            if (!liaohetext4.isEmpty()) {
                ms = liaohetext4;
            }
        }

        BPA_GOODSRECIPE pf = new BPA_GOODSRECIPE();
        pf.materialType = 1;
        pf.processname = "主料";

        pf.processms = "主料(" + ms + "," + wz + "号位,0,直接投出,0)";
        pf.processvalue = "主料名称," + ms + "|主料位置," + wz + "号位|主料重量,0|投料动作,直接投出|烹饪(秒),0";
        return pf;
    }

    /**
     * 获取手动主料
     *
     * @param
     * @return
     */
    public BPA_GOODSRECIPE Get手动主料() {
        String ms = "未知";
        BPA_GOODSRECIPE pf = new BPA_GOODSRECIPE();
        pf.materialType = 1;
        pf.processname = "主料";
        pf.processms = "主料(" + ms + ",1号位,0,手动投料,0)";
        pf.processvalue = "主料名称," + ms + "|主料位置,1号位|主料重量,0|投料动作,手动投料|烹饪(秒),0";
        return pf;
    }

    /**
     * 获取位置
     *
     * @param sd
     * @return
     */
    public BPA_GOODSRECIPE Get位置(String sd, String name) {
        BPA_GOODSRECIPE pf = new BPA_GOODSRECIPE();
        pf.materialType = 1;
        pf.processname = "位置";
        pf.processms = "位置(" + sd + "," + name + ",0)";
        pf.processvalue = "转动速度," + sd + "|位置动作," + name + "|延迟(秒),0";
        return pf;
    }

    /**
     * 获取搅拌
     *
     * @param sd
     * @return
     */
    public BPA_GOODSRECIPE Get搅拌(String sd) {
        BPA_GOODSRECIPE pf = new BPA_GOODSRECIPE();
        pf.materialType = 1;
        pf.processname = "搅拌";
        pf.processms = "搅拌(" + sd + ",0)";
        pf.processvalue = "搅拌速度," + sd + "|延迟(秒),0";
        return pf;
    }

    /**
     * 获取加热
     *
     * @param sd
     * @return
     */
    public BPA_GOODSRECIPE Get加热(String sd) {
        BPA_GOODSRECIPE pf = new BPA_GOODSRECIPE();
        pf.materialType = 1;
        pf.processname = "加热";
        pf.processms = "加热(" + sd + ",0)";
        pf.processvalue = "加热功率," + sd + "|延迟(秒),0";
        return pf;
    }

    /**
     * 获取出菜
     *
     * @return
     */
    public BPA_GOODSRECIPE Get出菜(int k) {
        BPA_GOODSRECIPE pf = new BPA_GOODSRECIPE();
        pf.materialType = 1;
        pf.processname = "出菜";
        pf.processms = "出菜(" + k + ")";
        pf.processvalue = "等待(秒)," + k;
        return pf;
    }

    /**
     * 获取清洗
     *
     * @return
     */
    public BPA_GOODSRECIPE Get清洗(int k) {
        BPA_GOODSRECIPE pf = new BPA_GOODSRECIPE();
        pf.materialType = 1;
        pf.processname = "清洗";
        pf.processms = "清洗(" + k + ")";
        pf.processvalue = "等待(秒)," + k;
        return pf;
    }

    /**
     * 获取选中行的变量
     *
     * @return
     */
    public BPA_GOODSRECIPE GetSelectItemFromValue(String name, String yl) {
        BPA_GOODSRECIPE pf = new BPA_GOODSRECIPE();
        try {
            //  延迟,100|延迟,100|延迟,100|延迟,100|
            String data = "";
            String desc = "";

            pf.materialType = 0;
            data += name + "," + yl + "|";
            desc += yl + ",";

            pf.processname = "液体料";
            pf.processms = "液体料" + "(" + desc.substring(0, desc.length() - 1) + ")";
            pf.processvalue = data.substring(0, data.length() - 1);
            return pf;
        } catch (Exception ex) {
            return null;
        }
    }

    /**
     * 获取速度
     *
     * @return
     */
    public String Get速度(int speed) {
        if (speed >= 0 && speed < 5) {
            return "停止";
        } else if (speed >= 5 && speed < 15) {
            return "最低";
        } else if (speed >= 15 && speed < 25) {
            return "低速";
        } else if (speed >= 25 && speed < 35) {
            return "中速";
        } else if (speed >= 35 && speed < 45) {
            return "高速";
        } else if (speed >= 45 && speed < 55) {
            return "最高";
        } else {
            return "极高";
        }
    }

    /**
     * 获取挡位
     *
     * @return
     */
    public String Get挡位(int speed) {
        if (speed == 0) {
            return "停止";
        } else if (speed == 1) {
            return "一档";
        } else if (speed == 2) {
            return "二档";
        } else if (speed == 3) {
            return "三档";
        } else if (speed == 4) {
            return "四档";
        } else if (speed == 5) {
            return "五档";
        } else if (speed == 6) {
            return "六档";
        } else if (speed == 7) {
            return "七档";
        } else {
            return "八档";
        }
    }

    /**
     * 设置执行中
     *
     * @param status
     */
    public void SetZhiXingZhong(boolean status) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                zhixingzhong.setVisibility(status ? View.VISIBLE : View.GONE);
                if (!status) {
                    ClikTime = (int) ((SystemClock.elapsedRealtime() - runtime.getBase()) / 1000);
                }
            }
        });
    }

    /**
     * 用量
     *
     * @param index
     * @param
     */
    public void EditTextDialogBuilder(int index) {
        String name = ConfigName.getInstance().SilosDisplayMaterial.get(index);
        final QMUIDialog.EditTextDialogBuilder builder = new QMUIDialog.EditTextDialogBuilder(this);
        builder.setTitle("料仓" + index + "[" + name + "]用量")
                .setPlaceholder("在此输入您的用量")
                .setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_CLASS_NUMBER)
                .addAction("取消", new QMUIDialogAction.ActionListener() {
                    @Override
                    public void onClick(QMUIDialog dialog, int index) {
                        dialog.dismiss();
                    }
                })
                .addAction("确定", new QMUIDialogAction.ActionListener() {
                    @Override
                    public void onClick(QMUIDialog dialog, int index) {
                        CharSequence text = builder.getEditText().getText();
                        if (text != null && text.length() > 0) {
                            String t = text.toString();
                            try {
                                Double.parseDouble(t);
                            } catch (Exception ex) {
                                ToastUtils.warning("输入格式出错！");
                                return;
                            }

                            Pumping(name, t);

                            dialog.dismiss();
                        } else {
                            ToastUtils.warning("用量不能为空！");
                        }
                    }
                })
                .show();
    }

    //endregion
}