package net.northking.cctp.upperComputer.driver.harmony.hyppium.data;


import net.northking.cctp.upperComputer.driver.harmony.ui.UiComponent;
import net.northking.cctp.upperComputer.driver.protocol.packet.ICommandData;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

public class UiEventResponse implements ICommandData {
    private int code;
    private UiEventData data = null;
    private UiComponent layout = null;

    public UiEventStage getStage() {
        return UiEventStage.fromValue(code);
    }

    public UiEventData getData() {
        return data;
    }

    public UiComponent getLayout() {
        return layout;
    }

    @Override
    public void buildFromInput(DataInput dataInput) throws IOException {
        code = dataInput.readInt();
        if (dataInput.readBoolean()) {
            data = new UiEventData(dataInput);
        } else {
            data = null;
        }
        if (dataInput.readBoolean()) {
            layout = new UiComponent(dataInput);
        } else {
            layout = null;
        }
    }

    @Override
    public void encodeToOutput(DataOutput dataOutput) throws IOException {
        dataOutput.writeInt(code);
        dataOutput.writeBoolean(data != null);
        if (data != null) {
            data.encodeToOutput(dataOutput);
        }
        dataOutput.writeBoolean(layout != null);
        if (layout != null) {
            layout.encodeToOutput(dataOutput);
        }
    }
}
