package ru.upos;

import java.io.IOException;

/**
 * Выполнение инструкции команды MASTERCALL на устройстве терминала
 */
public class DeviceInstructionProcess {

    private DeviceInstruction instruction;
    private DeviceCode deviceCode;
    private HostConnection hostConnection;
    private PrinterEmulation printer;

    private String ipAddress;
    private int ipPort;
    private long timeout;
    private int maxDataLen;
    private byte[] data;

    private boolean isCopyReceipt;

    public DeviceInstructionProcess() {
        printer = null;
    }

    public DeviceInstruction getInstruction() {
        return instruction;
    }

    public void setInstruction(DeviceInstruction instruction) {
        this.instruction = instruction;
    }

    public DeviceCode getDeviceCode() {
        return deviceCode;
    }

    public void setDeviceCode(DeviceCode deviceCode) {
        this.deviceCode = deviceCode;
    }

    public void setIpAddress(String ipAddress) {
        this.ipAddress = ipAddress;
    }

    public void setIpPort(int ipPort) {
        this.ipPort = ipPort;
    }

    public void setTimeout(long timeout) {
        this.timeout = timeout;
    }

    public void setMaxDataLen(int maxDataLen) {
        this.maxDataLen = maxDataLen;
    }

    public byte[] getData() {
        return data;
    }

    public void setData(byte[] data) {
        this.data = data;
    }

    public void setCopyReceipt(boolean copyReceipt) {
        isCopyReceipt = copyReceipt;
    }

    private boolean runInstructionLan() throws IOException {
        boolean result = false;

        switch (instruction) {
            case Open:
                hostConnection = new HostConnection();
                result = hostConnection.createSocket(ipAddress, ipPort);
                break;
            case Read:
                result = hostConnection.read(maxDataLen, timeout);
                setData(hostConnection.getData());
                break;
            case Write:
                hostConnection.write(data);
                result = true;
                break;
            case Close:
                hostConnection.closeSocket();
                result = true;
                break;
        }
        return result;
    }

    private boolean runInstructionPingInfo() {
        boolean result = false;

        switch (instruction) {
            case Read:
                break;
            case Write:
                break;
        }
        return  result;
    }

    private boolean runInstructionPrinter() throws IOException {
        boolean result = false;

        switch (instruction) {
            case Open:
                if (printer == null) {
                    printer = new PrinterEmulation();
                }
                result = printer.open(isCopyReceipt);
                break;
            case Write:
                printer.write(data);
                result = true;
                break;
            case Close:
                printer.close();
                result = true;
                break;
        }
        return result;
    }

    public boolean runInstruction() throws IOException {
        boolean result = false;

        switch (deviceCode) {
        case Lan:
            result = runInstructionLan();
            break;
        case PingInfo:
            break;
        case Printer:
            result = runInstructionPrinter();
            break;
        }
        return result;
    }
}
