#pragma once
#include "RC.h"
#include "Protocol.h"
#include "RcDecoder.h"

class Protocol_1 : public Protocol {

public:
    Protocol_1() : Protocol(1) {
    }

    void fromJson(JsonObjectConst& rcSwitch, RCSwitch& rcDevice) override {
        unsigned int protocol = rcSwitch["protocol"];
        rcDevice.setProtocol(protocol);
        const char* group = rcSwitch["group"];
        int channel = rcSwitch["channel"];
    	mySwitch.enableTransmit(SEND_PIN);
        rcSwitch["state"] ? rcDevice.switchOn(group, channel) : rcDevice.switchOff(group, channel);
    	mySwitch.disableTransmit();
    }

    void toJson(unsigned long value, JsonDocument& jsonDoc) override {
        JsonObject rcSwitch = jsonDoc.createNestedObject("rcSwitch");
        rcSwitch["protocol"] = protocol;
        RcDecoder decoder;
        decoder.decode(value);
        rcSwitch["state"] = decoder.state;
        rcSwitch["group"] = String(decoder.group, BIN);
        rcSwitch["channel"] = decoder.device;
        rcSwitch["raw_value"] = value;
    }
};
