#pragma once
#include <Arduino.h>
#include <NewRemoteTransmitter.h>
#include <Screen.h>

#define RF_SEND_PIN 11

namespace RFTransmit{
    char rf_switch_on[10] = "true";
    char rf_switch_off[10] = "false";
    char rf_type_switch[10] = "switch";
    char rf_type_switchgroup[20] = "switchgroup";

    void handlePacket(SimpleSerialProtocol *endpoint, char* id, char* action, char* data);
    void send(int unitcode, char* commandType, char* commandValue);
}

void RFTransmit::handlePacket(SimpleSerialProtocol *endpoint, char* id, char* action, char* data){
    //ambiance light 
    if(strcmp(id, "ambiance_light") == 0){
        if (strcmp(action, "on") == 0){
            RFTransmit::send(0, rf_type_switch, rf_switch_on);
            Screen::display(id, "ON");
            endpoint->sendPacket("access", "update_state", "bool;ambiance_light;true");
        }
        
        if (strcmp(action, "off") == 0){
            RFTransmit::send(0, rf_type_switch, rf_switch_off);
            Screen::display(id, "OFF");
            endpoint->sendPacket("access", "update_state", "bool;ambiance_light;false");
        }
    }

    //Main group lights
    if(strcmp(id, "main_group") == 0){
        if (strcmp(action, "on") == 0){
            RFTransmit::send(0, rf_type_switchgroup, rf_switch_on);
            Screen::display(id, "ON");
            endpoint->sendPacket("access", "update_state", "bool;ambiance_light;true");
        }
        
        if (strcmp(action, "off") == 0){
            RFTransmit::send(0, rf_type_switchgroup, rf_switch_off);
            Screen::display(id, "OFF");
            endpoint->sendPacket("access", "update_state", "bool;ambiance_light;false");
        }
    }
}

void RFTransmit::send(int unitcode, char* commandType, char* commandValue) {
    NewRemoteTransmitter mainTransmitter(long(27491570), RF_SEND_PIN, 268);
    // for switch remotes
    if(strcmp(commandType, rf_type_switch) == 0) {

        // ambiance light
        if(strcmp(commandValue, rf_switch_on) == 0) {
            mainTransmitter.sendUnit(unitcode, true);
        }

        if(strcmp(commandValue, rf_switch_off) == 0) {
            mainTransmitter.sendUnit(unitcode, false);
        }
    }
    if(strcmp(commandType, rf_type_switchgroup) == 0) {

        // main group
        if(strcmp(commandValue, rf_switch_on) == 0) {
            mainTransmitter.sendGroup(true);
        }

        if(strcmp(commandValue, rf_switch_off) == 0) {
            mainTransmitter.sendGroup(false);
        }
    }
    
}
