package com.sunzee.thread.advanceparameter;

import android.util.Log;

import com.sunzee.adapter.AdvanceParameterAdapter;

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

/**
 * 进阶参数线程池
 */
public class ThreadPoolAdvanceParameter {
    private ScheduledThreadPoolExecutor scheduled;

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

    private String[] values = new String[AdvanceParameterAdapter.paraName.length];

    private ScheduledFuture<?>
            mScheduledFutureD426, mScheduledFutureWrite, mScheduledFutureD403, mScheduledFutureD418,
            mScheduledFutureD79;

    private AdvanceParameterTask
            mTaskRunD426, mTaskRunWrite, mTaskRunD403, mTaskRunD418,
            mTaskRunD79;

    public ThreadPoolAdvanceParameter() {
        scheduled = new ScheduledThreadPoolExecutor(6);
        //设置线程池的策略  抛弃旧的任务
        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;
    }

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


    public void startALLRead() {

        mTaskRunD418 = new AdvanceParameterTask(AdvanceParameterThreadType.runD418);
        mScheduledFutureD418 = scheduled.scheduleAtFixedRate(mTaskRunD418, 10, 1000, TimeUnit.MILLISECONDS);

        mTaskRunD426 = new AdvanceParameterTask(AdvanceParameterThreadType.runD426);
        mScheduledFutureD426 = scheduled.scheduleAtFixedRate(mTaskRunD426, 10, 1000, TimeUnit.MILLISECONDS);
    }
    public void startReadD79() {
        mTaskRunD79 = new AdvanceParameterTask(AdvanceParameterThreadType.runD79);
        mScheduledFutureD79 = scheduled.scheduleAtFixedRate(mTaskRunD79, 10, 1000, TimeUnit.MILLISECONDS);
    }

    public void startReadD403() {
        mTaskRunD403 = new AdvanceParameterTask(AdvanceParameterThreadType.runD403);
        mScheduledFutureD403 = scheduled.scheduleAtFixedRate(mTaskRunD403, 10, 1000, TimeUnit.MILLISECONDS);
    }

    public void stopAll() {
        Log.d("ThreadPoolAdvanceParr", "stopAll:  "+mTaskRunD418);
//        stopReadD79();
        stopReadD426();
        stopWrite();
        stopReadD403();
        stopReadD418();
    }

    public void stopReadD418() {
        stopRead(mTaskRunD418, mScheduledFutureD418);
    }

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

    public void stopReadD403() {
        stopRead(mTaskRunD403, mScheduledFutureD403);
    }

    public void stopReadD79() {
        stopRead(mTaskRunD79, mScheduledFutureD79);
    }

    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, 0, 1000, TimeUnit.MILLISECONDS);
    }

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


}
