package com.sunzee.thread.advanceparameter;

import android.util.Log;

import com.hboxs.serialport.SerialPortManager;
import com.hboxs.serialport.frame.WriteCommandFrame;
import com.hboxs.serialport.util.HexUtils;

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

/**
 * 进阶参数线程池
 */
public class ThreadPoolAdvanceParameter {
    private final String[] paraAddress;
    private ScheduledThreadPoolExecutor scheduled;

    public String[] getValues() {
        return values;
    }

    private String[] values = new String[6];

    private ScheduledFuture<?>
            mScheduledFutureD426, mScheduledFutureD427, mScheduledFutureD428,
            mScheduledFutureD429, mScheduledFutureD430, mScheduledFutureD431,
            mScheduledFutureWrite;

    private AdvanceParameterTask
            mTaskRunD426, mTaskRunD427, mTaskRunD428, mTaskRunD429,
            mTaskRunD430, mTaskRunD431, mTaskRunWrite;

    public ThreadPoolAdvanceParameter(String[] paraAddress) {
        this.paraAddress = paraAddress;
        scheduled = new ScheduledThreadPoolExecutor(2);
        //设置线程池的策略  抛弃旧的任务
        scheduled.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardOldestPolicy());
    }

    public boolean isReceive1() {
        return isReceive1;
    }

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

    public boolean isReceive2() {
        return isReceive2;
    }

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

    public boolean isReceive3() {
        return isReceive3;
    }

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

    public boolean isReceive4() {
        return isReceive4;
    }

    public void setReceive4(boolean receive4) {
        isReceive4 = receive4;
    }

    public boolean isReceive5() {
        return isReceive5;
    }

    public void setReceive5(boolean receive5) {
        isReceive5 = receive5;
    }

    public boolean isReceive6() {
        return isReceive6;
    }

    public void setReceive6(boolean receive6) {
        isReceive6 = receive6;
    }

    private boolean isReceive1 = false;
    private boolean isReceive2 = false;
    private boolean isReceive3 = false;
    private boolean isReceive4 = false;
    private boolean isReceive5 = false;
    private boolean isReceive6 = false;

    public void startALLRead() {

        mTaskRunD426 = new AdvanceParameterTask(AdvanceParameterThreadType.runD426);
        mScheduledFutureD426 = scheduled.scheduleAtFixedRate(mTaskRunD426, 10, 1000, TimeUnit.MILLISECONDS);

        mTaskRunD427 = new AdvanceParameterTask(AdvanceParameterThreadType.runD427);
        mScheduledFutureD427 = scheduled.scheduleAtFixedRate(mTaskRunD427, 10, 1000, TimeUnit.MILLISECONDS);

        mTaskRunD428 = new AdvanceParameterTask(AdvanceParameterThreadType.runD428);
        mScheduledFutureD428 = scheduled.scheduleAtFixedRate(mTaskRunD428, 10, 1000, TimeUnit.MILLISECONDS);

        mTaskRunD429 = new AdvanceParameterTask(AdvanceParameterThreadType.runD429);
        mScheduledFutureD429 = scheduled.scheduleAtFixedRate(mTaskRunD429, 10, 1000, TimeUnit.MILLISECONDS);

        mTaskRunD430 = new AdvanceParameterTask(AdvanceParameterThreadType.runD430);
        mScheduledFutureD430 = scheduled.scheduleAtFixedRate(mTaskRunD430, 10, 1000, TimeUnit.MILLISECONDS);

        mTaskRunD431 = new AdvanceParameterTask(AdvanceParameterThreadType.runD431);
        mScheduledFutureD431 = scheduled.scheduleAtFixedRate(mTaskRunD431, 10, 1000, TimeUnit.MILLISECONDS);
    }

    public void stopReadD426() {
        stopRead(mTaskRunD426, mScheduledFutureD426);
    }

    public void stopReadD427() {
        stopRead(mTaskRunD427, mScheduledFutureD427);
    }

    public void stopReadD428() {
        stopRead(mTaskRunD428, mScheduledFutureD428);
    }

    public void stopReadD429() {
        stopRead(mTaskRunD429, mScheduledFutureD429);
    }

    public void stopReadD430() {
        stopRead(mTaskRunD430, mScheduledFutureD430);
    }

    public void stopReadD431() {
        stopRead(mTaskRunD431, mScheduledFutureD431);
    }


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

    public void startWrite(String paraAddress, String data) {
        mTaskRunWrite = new AdvanceParameterTask(AdvanceParameterThreadType.runWrite, paraAddress, data);
        mScheduledFutureWrite = scheduled.scheduleAtFixedRate(mTaskRunWrite, 10, 1000, TimeUnit.MILLISECONDS);
    }

    public void stopWrite() {
        stopRead(mTaskRunWrite, mScheduledFutureWrite);
    }
}
