/*
 * challenge_com.c
 *
 *  Created on: 05.12.2022
 *      Author: jonas
 */

#include <stdbool.h>

#include "challenge_com.h"
#include "challenge_app.h"
#include "platform.h"
#include "esp_log.h"
#include "McuUtility.h"
#include "mjson.h"
#include "mqtt_client.h"
#include "myMqtt.h"
#include "splitflap_wrapper.h"

#define TAG "CHALLENGE_COM" /* tag for logging with ESP_LOG */

/* TOPICS */
const char MQTT_TOPIC_SF_DISPLAY[] = "/splitFlap/cmd/display/";
const char MQTT_TOPIC_SF_INITALL[] = "/splitFlap/cmd/init/";
const char MQTT_TOPIC_SF_CONFIG_SETUP[] = "/splitFlap/config/setup/";

void Challenge_Com_ParseMqtt(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data){
	/* INFO
	 * TOPIC: 			event->topic
	 * TOPIC LENGTH: 	event->topic_len
	 * DATA: 			event->data
	 * DATA LENGTH: 	event->data_len
	 * CLIENT: 			event->client
	 */

    esp_mqtt_event_handle_t event = event_data;
    bool handled = false;

    // check if event id is MQTT_EVENT_DATA => break if not
    if((esp_mqtt_event_id_t)event_id != MQTT_EVENT_DATA) {
        ESP_LOGE(TAG, "Wrong data received. Can only handle MQTT_EVENT_DATA (event id %i). But received event id: %i", MQTT_EVENT_DATA, (esp_mqtt_event_id_t)event_id);
        return;
    }

    /* check topic and call related command if allowed by robot mode */
    /* stationary robot allowed commands */
    if(Challenge_App_GetRobotMode() == true) {
    	// CMD: CONFIG SETUP
        if(McuUtility_strcmp((char*)event->topic, MQTT_TOPIC_SF_CONFIG_SETUP)==0){
        	handled = true;
        	// parse json
        	int setupId = 0;
        	int hwId = 0;
        	struct json_attr_t json_attrs[] = {
        	    {"setupId",    t_integer, .addr.integer = &setupId},
        	    {"hardwareId", t_integer, .addr.integer = &hwId,},
        	    {NULL},
        	};
        	if(json_read_object(event->data, json_attrs, NULL) != 0){
        		ESP_LOGE(TAG, "Parsing JSON data of CONFIG SETUP message failed. Event data was = %s", event->data);
        	} else{  // successfully parsed
        		// check values
        		if(setupId >= 0 && setupId <= 10 && hwId >= 0 && hwId <= 10){
    				SplitFlap_Wrapper_SetHardwareIdentifier(setupId, hwId);
        		} else{
        			ESP_LOGE(TAG, "SetupID or HardwareID out of range. Event data was = %s", event->data);
        		}
        	}
        }
        // CMD: INIT ALL
        else if(McuUtility_strcmp((char*)event->topic, MQTT_TOPIC_SF_INITALL)==0){
        	handled = true;
        	SplitFlap_Wrapper_MoveAllToZeroPosition();
        }
        // CMD: DISPLAY
        else if(McuUtility_strcmp((char*)event->topic, MQTT_TOPIC_SF_DISPLAY)==0){
        	handled = true;
        	// parse json
        	char message[] = "TST";
        	struct json_attr_t json_attrs[] = {
				{"message", t_string, .addr.string = message},
				{NULL},
			};
        	if(json_read_object(event->data, json_attrs, NULL) != 0){
				ESP_LOGE(TAG, "Parsing JSON data of DISPLAY message failed. Event data was = %s", event->data);
			} else{  // successfully parsed
	        	SplitFlap_Wrapper_Display(message);
			}
        }
    }
    /* mobile robot allowed commands */
    else{

    }

    /* both robot modes allowed commands */



    if(handled == false){
        ESP_LOGE(TAG, "Received data could not be handled. Topic was %s", event->topic);
    }
    return;
}

void Challenge_Com_SubscribeToAllTopics(void){
	MyMqtt_Subscribe(MQTT_TOPIC_SF_INITALL);
	MyMqtt_Subscribe(MQTT_TOPIC_SF_DISPLAY);
	MyMqtt_Subscribe(MQTT_TOPIC_SF_CONFIG_SETUP);
}
