package com.sunzee.thread.myservice;

import com.sunzee.thread.debugparameter.DebugParameterTask;
import com.sunzee.thread.debugparameter.DebugParameterThreadType;

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

/**
 * 长时间或者推送要执行的线程池
 */
public class ThreadPoolMyservice {
    private ScheduledThreadPoolExecutor scheduled;

    private ScheduledFuture<?>
            mScheduledFutureM600, mScheduledFutureM8, mScheduledFutureM17,
            mScheduledFutureSet, mScheduledFutureRst, mScheduledFutureWrite,
            mScheduledFutureD120, mScheduledFutureX12, mScheduledFutureDD120,
            mScheduledFutureD2, mScheduledFutureM3, mScheduledFutureRstRM10,
            mScheduledFutureSetMM10,mScheduledFutureMMM3;
    private MyserviceTask
            mTaskRunM600, MTaskRunM8, MTaskRunM17,
            mTaskRunMRst, mTaskRunMSet, mTaskRunWrite,
            mTaskRunD120, mTaskRunnX12, mTaskRunDD120,
            mTaskRunD2, mTaskRunM3,mTaskRunMRstRM10,
            mTaskRunMSetMM10,mTaskRunMMM3;

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

    public void startWriteMM3(String address) {
        mTaskRunM3 = new MyserviceTask(MyserviceThreadType.runnMM3, address);
        mScheduledFutureM3 = scheduled.scheduleAtFixedRate(mTaskRunM3, 0, 1000, TimeUnit.MILLISECONDS);
    }

    public void stopWriteMM3() {
        stopRead(mTaskRunM3, mScheduledFutureM3);
    }


    public void startALLRead() {
        mTaskRunM600 = new MyserviceTask(MyserviceThreadType.runM600);
        mScheduledFutureM600 = scheduled.scheduleAtFixedRate(mTaskRunM600, 0, 1000, TimeUnit.MILLISECONDS);

        mTaskRunnX12 = new MyserviceTask(MyserviceThreadType.runX12);
        mScheduledFutureX12 = scheduled.scheduleAtFixedRate(mTaskRunnX12, 0, 1000, TimeUnit.MILLISECONDS);

        mTaskRunD2 = new MyserviceTask(MyserviceThreadType.runD2);
        mScheduledFutureD2 = scheduled.scheduleAtFixedRate(mTaskRunD2, 0, 1000, TimeUnit.MILLISECONDS);
    }

    public void stopReadD2() {
        stopRead(mTaskRunD2, mScheduledFutureD2);
    }

    public void startSetM8() {
        MTaskRunM8 = new MyserviceTask(MyserviceThreadType.runM8);
        mScheduledFutureM8 = scheduled.scheduleAtFixedRate(MTaskRunM8, 0, 1000, TimeUnit.MILLISECONDS);
    }

    public void startSetM17() {
        MTaskRunM17 = new MyserviceTask(MyserviceThreadType.runM17);
        mScheduledFutureM17 = scheduled.scheduleAtFixedRate(MTaskRunM17, 0, 1000, TimeUnit.MILLISECONDS);
    }

    public void startRst(String address) {
        mTaskRunMRst = new MyserviceTask(MyserviceThreadType.runRst, address);
        mScheduledFutureRst = scheduled.scheduleAtFixedRate(mTaskRunMRst, 0, 1000, TimeUnit.MILLISECONDS);
    }

    public void startWriteDD120(String paraAddress, String data) {
        mTaskRunDD120 = new MyserviceTask(MyserviceThreadType.runDD120, paraAddress, data);
        mScheduledFutureDD120 = scheduled.scheduleAtFixedRate(mTaskRunDD120, 0, 1000, TimeUnit.MILLISECONDS);
    }

    public void stopWriteDD120() {
        stopRead(mTaskRunDD120, mScheduledFutureDD120);
    }

    public void startWriteD120(String paraAddress, String data) {
        mTaskRunD120 = new MyserviceTask(MyserviceThreadType.runD120, paraAddress, data);
        mScheduledFutureD120 = scheduled.scheduleAtFixedRate(mTaskRunD120, 0, 1000, TimeUnit.MILLISECONDS);
    }

    public void stopWriteD120() {
        stopRead(mTaskRunD120, mScheduledFutureD120);
    }

    public void stopReadX20() {
        stopRead(mTaskRunnX12, mScheduledFutureX12);
    }

    public void startSet(String address) {
        mTaskRunMSet = new MyserviceTask(MyserviceThreadType.runSet, address);
        mScheduledFutureSet = scheduled.scheduleAtFixedRate(mTaskRunMSet, 0, 1000, TimeUnit.MILLISECONDS);
    }

    public void startWrite(String paraAddress, String data) {
        mTaskRunWrite = new MyserviceTask(MyserviceThreadType.runWrite, paraAddress, data);
        mScheduledFutureWrite = scheduled.scheduleAtFixedRate(mTaskRunWrite, 0, 1000, TimeUnit.MILLISECONDS);
    }

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

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

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


    public void stopReadM600() {
        stopRead(mTaskRunM600, mScheduledFutureM600);
    }


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

    public void stopSetM17() {
        stopRead(MTaskRunM17, mScheduledFutureM17);
    }

    public void stopSetM8() {
        stopRead(MTaskRunM8, mScheduledFutureM8);
    }

    public void stopALLRead() {
        stopWrite();
        stopRst();
        stopSet();
        stopReadM600();
        stopSetM8();
        stopSetM17();
        stopWriteD120();
        stopWriteDD120();
        stopReadX20();
        stopReadD2();
    }

    public void startRstRM10(String address) {
        mTaskRunMRstRM10 = new MyserviceTask(MyserviceThreadType.runRstRM10, address);
        mScheduledFutureRstRM10 = scheduled.scheduleAtFixedRate(mTaskRunMRstRM10, 10, 1000, TimeUnit.MILLISECONDS);
    }

    public void startSetMM10(String address) {
        mTaskRunMSetMM10 = new MyserviceTask(MyserviceThreadType.runSetMM10, address);
        mScheduledFutureSetMM10 = scheduled.scheduleAtFixedRate(mTaskRunMSetMM10, 10, 1000, TimeUnit.MILLISECONDS);

    }

    public void stopRstRM10() {
        stopRead(mTaskRunMRstRM10, mScheduledFutureRstRM10);
    }

    public void stopSetMM10() {
        stopRead(mTaskRunMSetMM10, mScheduledFutureSetMM10);
    }

    public void stopSetMMM3() {
        stopRead(mTaskRunMMM3, mScheduledFutureMMM3);
    }

    public void startSetMMM3(String address) {
        mTaskRunMMM3 = new MyserviceTask(MyserviceThreadType.runnMMM3,address);
        mScheduledFutureMMM3 = scheduled.scheduleAtFixedRate(mTaskRunMMM3, 0, 1000, TimeUnit.MILLISECONDS);
    }
}
