package de.newsarea.homecockpit.connector.fsuipc.facade.eventhandler;

import de.newsarea.homecockpit.connector.facade.eventhandler.InboundEventHandler;
import de.newsarea.homecockpit.connector.fsuipc.FSUIPCConnector;
import de.newsarea.homecockpit.connector.fsuipc.facade.eventhandler.domain.FSUIPCOffset;
import de.newsarea.homecockpit.fsuipc.domain.OffsetIdent;
import de.newsarea.homecockpit.fsuipc.domain.OffsetItem;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.AbstractMap;
import java.util.Map;
import java.util.concurrent.TimeoutException;

public class InboundToggleBitEventHandler extends AbstractFSUIPCEventHandler implements InboundEventHandler {
	
	private static final Logger log = LoggerFactory.getLogger(InboundToggleBitEventHandler.class);

    private Byte bitIdx;

    public Byte getBitIdx() {
        if(bitIdx == null) {
            bitIdx = Byte.parseByte(getParameterValue("bitIdx"));
        }
        return bitIdx;
    }

    public InboundToggleBitEventHandler(FSUIPCConnector connector, Map<String, String> parameters) {
        super(connector, parameters);
    }

    public InboundToggleBitEventHandler(FSUIPCConnector connector, FSUIPCOffset offset, int size, Byte bitIdx) {
        this(connector,
                toParameters(
                        new AbstractMap.SimpleEntry<>("offset", offset.toHexString()),
                        new AbstractMap.SimpleEntry<>("size", String.valueOf(size)),
                        new AbstractMap.SimpleEntry<>("bitIdx", String.valueOf(bitIdx))
                )
        );
    }

    @Override
    public void handleInboundEvent(Object value) throws IOException {
		log.debug("handleOutput - {}", value);
        try {
            toggleBit(getOffset().getValue(), getSize(), getBitIdx());
        } catch (TimeoutException e) {
            log.error(e.getMessage(), e);
        }
    }
	
	/* HELPER */

    private void toggleBit(int offset, int size, byte bitIdx) throws IOException, TimeoutException {
    	OffsetItem offsetItem = getConnector().read(new OffsetIdent(offset, size));
        byte[] value = offsetItem.getValue().toByteArray();
    	int bidx = (bitIdx / 8);
		byte nidx = (byte)(bitIdx % 8);
		byte newByteValue = value[value.length - 1 - bidx];
		newByteValue ^= (1 << nidx);
		value[value.length - 1 - bidx] = newByteValue;
        getConnector().write(new OffsetItem(offset, size, value));
    }

}

@@ -304,123 +304,8 @@
 et;%0A
-import de.newsarea.homecockpit.fsuipc.domain.OffsetIdent;%0Aimport de.newsarea.homecockpit.fsuipc.domain.OffsetItem;%0A
 impo
@@ -441,54 +441,8 @@
 Map;
-%0Aimport java.util.concurrent.TimeoutException;
 %0A%0Apu
@@ -1585,702 +1585,78 @@
     
-try %7B%0A            toggleBit(getOffset().getValue(), getSize(), getBitIdx());%0A        %7D catch (TimeoutException e) %7B%0A            log.error(e.getMessage(), e);%0A        %7D%0A    %7D%0A%09%0A%09/* HELPER */%0A%0A    private void toggleBit(int offset, int size, byte bitIdx) throws IOException, TimeoutException %7B%0A    %09OffsetItem offsetItem = getConnector().read(new OffsetIdent(offset, size));%0A        byte%5B%5D value = offsetItem.getValue().toByteArray();%0A    %09int bidx = (bitIdx / 8);%0A%09%09byte nidx = (byte)(bitIdx %25 8);%0A%09%09byte newByteValue = value%5Bvalue.length - 1 - bidx%5D;%0A%09%09newByteValue %5E= (1 %3C%3C nidx);%0A%09%09value%5Bvalue.length - 1 - bidx%5D = newByteValue;%0A        getConnector().write(new OffsetItem(offset, size, value
+getConnector().toggleBit(getOffset().getValue(), getSize(), getBitIdx(
 ));%0A

add direct toggleBit function to InboundToggleBitEventHandler