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<?>
            mScheduledFutureM311, mScheduledFutureRst, mScheduledFutureSet;
    private DebugParameterTask
            mTaskRunM311, mTaskRunMRst, mTaskRunMSet;

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

    private ArrayList<String> values = new ArrayList<>(Arrays.asList("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() {
        mTaskRunM311 = new DebugParameterTask(DebugParameterThreadType.runM311);
        mScheduledFutureM311 = scheduled.scheduleAtFixedRate(mTaskRunM311, 10, 1000, TimeUnit.MILLISECONDS);
    }

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

    private boolean isReceive1 = false;

    public boolean isReceive1() {
        return isReceive1;
    }

    //-------------------------------------------stop read-----------------------------------------------------
    public void stopReadM311() {
        stopRead(mTaskRunM311, mScheduledFutureM311);
    }

    //-------------------------------------------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);
    }

}
