package com.unibuy.smartdevice.controller;

import com.unibuy.smartdevice.devices.ComPort;
import com.unibuy.smartdevice.devices.PortTools;
import com.unibuy.smartdevice.devices.Rs232Dev;
import com.unibuy.smartdevice.exception.LogsIOException;
import com.unibuy.smartdevice.exception.LogsSecurityException;
import com.unibuy.smartdevice.exception.LogsUnsupportedOperationException;
import com.unibuy.smartdevice.tools.HandlerMain;

public class DevElectricController extends DevController {
    private Rs232Dev comPort;

    public DevElectricController(HandlerMain handlerMain) throws LogsSecurityException, LogsUnsupportedOperationException, LogsIOException {
        super( 10, handlerMain);
        this.comPort = new Rs232Dev(ComPort.COM_PORT_3);
    }

    public void devSwitch(int slot, int h, int m, int min) throws LogsIOException {
        byte[] sendBuffer = new byte[] {0x02, 0x01, 0x01};
        sendBuffer = PortTools.concatByteArrays(sendBuffer, PortTools.intToByte8(slot, 1));
        sendBuffer = PortTools.concatByteArrays(sendBuffer, PortTools.intToByte8(h, 1));
        sendBuffer = PortTools.concatByteArrays(sendBuffer, PortTools.intToByte8(m, 1));
        sendBuffer = PortTools.concatByteArrays(sendBuffer, PortTools.intToByte8(min, 1));
        sendBuffer = PortTools.concatByteArrays(sendBuffer, new byte[]{0x00, 0x00});
        addSendBuffer(sendBuffer);
    }

    public void devSearch(int slot) throws LogsIOException {
        byte[] sendBuffer = new byte[] {0x02, 0x02, 0x01};
        sendBuffer = PortTools.concatByteArrays(sendBuffer, PortTools.intToByte8(slot, 1));
        sendBuffer = PortTools.concatByteArrays(sendBuffer, PortTools.intToByte8(0, 1));
        sendBuffer = PortTools.concatByteArrays(sendBuffer, PortTools.intToByte8(0, 1));
        sendBuffer = PortTools.concatByteArrays(sendBuffer, PortTools.intToByte8(0, 1));
        sendBuffer = PortTools.concatByteArrays(sendBuffer, new byte[]{0x00, 0x00});
        addSendBuffer(sendBuffer);
    }

    @Override
    protected void close(HandlerMain handlerMain) {
        try {
            if (this.comPort != null) {
                this.comPort.closePort();
            }
        } catch (LogsIOException e) {
//            getLogs().warning(e);
            getLogs().warning(e);
        }
    }

    @Override
    protected HandlerMain setHandlerMain() {
        return getSrcHandlerMain();
    }

    @Override
    protected void execute(HandlerMain handlerMain) {
        try {
            while (isRun() && getCountBySendBuffer() == 0) {
                try {
                    Thread.sleep(200);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }

            if (isRun() && getCountBySendBuffer() > 0) {
                while (isRun() && getCountBySendBuffer() > 0) {
                    this.comPort.send(getSendBufferByFirst());
                    try {
                        Thread.sleep(200);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    removeSendBufferByFirst();
                    byte[] readBuffer = this.comPort.readListen(100, 5);
                    if (readBuffer.length > 0) {
                        publishReadBuffer(readBuffer);
                    }
                }
            }
        } catch (LogsIOException e) {
            getLogs().warning(e);
        }
    }

    @Override
    protected Class<?> setCls() {
        return getClass();
    }
}
