package com.sunzee.thread.debugparameter;

import com.hboxs.serialport.SerialPortSendQueue;
import com.hboxs.serialport.frame.ReadCommandFrame;
import com.hboxs.serialport.frame.RstCommandFrame;
import com.hboxs.serialport.frame.SetCommandFrame;

import static com.sunzee.thread.debugparameter.DebugParameterThreadType.runM10;
import static com.sunzee.thread.debugparameter.DebugParameterThreadType.runM1411;
import static com.sunzee.thread.debugparameter.DebugParameterThreadType.runM320;
import static com.sunzee.thread.debugparameter.DebugParameterThreadType.runRst;
import static com.sunzee.thread.debugparameter.DebugParameterThreadType.runSet;
import static com.sunzee.thread.debugparameter.DebugParameterThreadType.runX;
import static com.sunzee.thread.debugparameter.DebugParameterThreadType.runY;

/**
 * 调试页面参数任务
 */
public class DebugParameterTask implements Runnable {

    private int type;
    private volatile String address;

    public String getAddress() {
        return address;
    }

    public int getId() {
        return id;
    }

    private volatile int id;
    private volatile boolean isRun;
    private String name;
    private String data;

    public DebugParameterTask(int type) {
        clearPlcQuee();
        this.type = type;
    }

    public DebugParameterTask(int type, String address) {
        clearPlcQuee();
        this.type = type;
        this.address = address;
    }

    public DebugParameterTask(int type, String address, int id) {
        clearPlcQuee();
        this.type = type;
        this.address = address;
        this.id = id;
    }


    public DebugParameterTask(int type, String address, String data, int id, String name) {
        clearPlcQuee();
        this.type = type;
        this.address = address;
        this.data = data;
        this.id = id;
        this.name = name;
    }

    private void clearPlcQuee() {
        setIsRun(true);
        SerialPortSendQueue.clear();
    }

    public void setIsRun(boolean isRun) {
        this.isRun = isRun;
    }

    public boolean getIsRun() {
        return isRun;
    }

    @Override
    public void run() {
        switch (type) {
            case DebugParameterThreadType.runM311:
                runReadM311();
                break;
            case runRst:
                runRst();
                break;
            case runSet:
                runSet();
                break;
            case runX:
                runX();
                break;
            case runY:
                runY();
                break;
            case runM1411:
                runReadM1411();
                break;
            case runM320:
                runReadM320();
                break;
            case runM10:
                runReadM10();
                break;

        }
    }

    private void runReadM10() {
        if (getIsRun()) {
            SerialPortSendQueue.sendCommand(new ReadCommandFrame("M10", 2), 10, "M10");
        }
    }

    private void runReadM320() {
        if (getIsRun()) {
            SerialPortSendQueue.sendCommand(new ReadCommandFrame("M320", 2), 320, "M320");
        }
    }

    private void runReadM1411() {
        if (getIsRun()) {
            SerialPortSendQueue.sendCommand(new ReadCommandFrame("M1411", 2), 1411, "M1411");
        }
    }

    private void runY() {
        if (getIsRun()) {
            SerialPortSendQueue.sendCommand(new ReadCommandFrame("X0", 4), 2,"X0");
        }
    }

    private void runX() {
        if (getIsRun()) {
            SerialPortSendQueue.sendCommand(new ReadCommandFrame("Y0", 4),1,"Y0");
        }
    }

    private void runReadM311() {
        if (getIsRun()) {
            SerialPortSendQueue.sendCommand(new ReadCommandFrame("M311", 2), 311, "M311");
        }
    }

    private void runRst() {
        if (getIsRun()) {
            SerialPortSendQueue.sendCommand(new RstCommandFrame(getAddress()), 311, getAddress());
        }
    }

    private void runSet() {
        if (getIsRun()) {
            SerialPortSendQueue.sendCommand(new SetCommandFrame(getAddress()), 311, getAddress());

        }
    }

    public void setAddressAndId(String address, int id) {
        this.address = address;
        this.id = id;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getData() {
        return data;
    }

    public void setData(String data) {
        this.data = data;
    }
}
