package com.sunzee.thread.debugparameter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 调试页参数线程池
 */
public class ThreadPoolDebugParameter {
    private ScheduledThreadPoolExecutor scheduled;

    private ScheduledFuture<?>
            mScheduledFutureM1411, mScheduledFutureM311, mScheduledFutureRst, mScheduledFutureSet,
            mScheduledFutureX, mScheduledFutureY,mScheduledFutureM10;
    private DebugParameterTask
            mTaskRunM1411, mTaskRunM311, mTaskRunMRst, mTaskRunMSet,
            mTaskRunX, mTaskRunY,mTaskRunM10;
    private DebugParameterTask mTaskRunM320;
    private ScheduledFuture<?> mScheduledFutureM320;

    public ArrayList<String> getValues() {
        return values;
    }

    private ArrayList<String> values = new ArrayList<>(Arrays.asList(
            "0", "0", "0", "0", "0", "0", "0",
            "0", "0", "0", "0", "0", "0", "0",
            "0", "0", "0", "0", "0", "0", "0",
            "0", "0", "0", "0", "0", "0", "0",
            "0"));

    public ThreadPoolDebugParameter() {
        scheduled = new ScheduledThreadPoolExecutor(7);
        //设置线程池的策略  抛弃旧的任务
        scheduled.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardOldestPolicy());
    }

    private static final String TAG = "ThreadPoolGeneralParame";

    /**
     * 读取全部参数
     */
    public void startALLRead() {
        //读取 A轴正转 A轴反转
        mTaskRunM1411 = new DebugParameterTask(DebugParameterThreadType.runM1411);
        mScheduledFutureM1411 = scheduled.scheduleAtFixedRate(mTaskRunM1411, 0, 1000, TimeUnit.MILLISECONDS);

        //读取喷水泵启动、气泵启动、清洁消毒喷水泵、清洗洁厕灵+水、消毒消毒液+水
        mTaskRunM311 = new DebugParameterTask(DebugParameterThreadType.runM311);
        mScheduledFutureM311 = scheduled.scheduleAtFixedRate(mTaskRunM311, 10, 1000, TimeUnit.MILLISECONDS);

        //X的信号
        mTaskRunX = new DebugParameterTask(DebugParameterThreadType.runX);
        mScheduledFutureX = scheduled.scheduleAtFixedRate(mTaskRunX, 30, 1000, TimeUnit.MILLISECONDS);

        mTaskRunY = new DebugParameterTask(DebugParameterThreadType.runY);
        mScheduledFutureY = scheduled.scheduleAtFixedRate(mTaskRunY, 40, 1000, TimeUnit.MILLISECONDS);

        //读取喷水泵启动、气泵启动、清洁消毒喷水泵、清洗洁厕灵+水、消毒消毒液+水
        mTaskRunM320 = new DebugParameterTask(DebugParameterThreadType.runM320);
        mScheduledFutureM320 = scheduled.scheduleAtFixedRate(mTaskRunM320, 50, 1000, TimeUnit.MILLISECONDS);


        //读取急停
        mTaskRunM10 = new DebugParameterTask(DebugParameterThreadType.runM10);
        mScheduledFutureM10 = scheduled.scheduleAtFixedRate(mTaskRunM10, 60, 1000, TimeUnit.MILLISECONDS);


    }

    public void stopAll() {
        stopReadM1411();
        stopReadM311();
        stopReadM320();
        stopRst();
        stopSet();
        stopReadX();
        stopReadY();
        stopReadM10();
    }



    private boolean isReceive1 = false;
    private boolean isReceive2 = false;
    private boolean isReceive3 = false;



    public boolean isReceive2() {
        return isReceive2;
    }

    public void setReceive2(boolean receive2) {
        isReceive2 = receive2;
    }

    public boolean isReceive1() {
        return isReceive1;
    }

    public void setReceive1(boolean receive1) {
        isReceive1 = receive1;
    }

    public boolean isReceive3() {
        return isReceive3;
    }

    public void setReceive3(boolean receive3) {
        isReceive3 = receive3;
    }

    public void stopReadM320() {
        stopRead(mTaskRunM320, mScheduledFutureM320);
    }

    public void stopReadM311() {
        stopRead(mTaskRunM311, mScheduledFutureM311);
    }

    public void stopReadM10() {
        stopRead(mTaskRunM10, mScheduledFutureM10);
    }

    public void stopReadM1411() {
        stopRead(mTaskRunM1411, mScheduledFutureM1411);
    }

    public void stopReadX() {
        stopRead(mTaskRunX, mScheduledFutureX);
    }

    public void stopReadY() {
        stopRead(mTaskRunY, mScheduledFutureY);
    }

    //-------------------------------------------stop read-----------------------------------------------------

    public static void stopRead(DebugParameterTask threadTask, ScheduledFuture<?> scheduledFuture1) {
        if (threadTask != null) {
            threadTask.setIsRun(false);
        }
        if (scheduledFuture1 != null) {
            scheduledFuture1.cancel(true);
        }
    }

    public void startRst(String address) {
        mTaskRunMRst = new DebugParameterTask(DebugParameterThreadType.runRst, address);
        mScheduledFutureRst = scheduled.scheduleAtFixedRate(mTaskRunMRst, 10, 1000, TimeUnit.MILLISECONDS);
    }

    public void startSet(String address) {
        mTaskRunMSet = new DebugParameterTask(DebugParameterThreadType.runSet, address);
        mScheduledFutureSet = scheduled.scheduleAtFixedRate(mTaskRunMSet, 10, 1000, TimeUnit.MILLISECONDS);

    }

    public void stopRst() {
        stopRead(mTaskRunMRst, mScheduledFutureRst);
    }

    public void stopSet() {
        stopRead(mTaskRunMSet, mScheduledFutureSet);
    }

}
