#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <uuid/uuid.h>

#include <models/controller.h>
#include <macros.h>

controller*
controller_create(void)
{
    controller *result = malloc(sizeof(*result));
    uuid_generate(result->uuid);

    strcpy(result->name, "new emgauwa device");
    result->command_port = 0;
    result->discovery_port = 4419;
    result->relay_count = 10;

    result->relays = malloc(sizeof(*result->relays) * result->relay_count);
    uint8_t i;
    for(i = 0; i < result->relay_count; i++)
    {
        result->relays[i] = relay_init(i);
    }

    return result;
}

void
controller_debug(controller *cntrlr)
{
    if(cntrlr == NULL)
    {
        LOG_DEBUG("controller is NULL");
    }
    char uuid_str[37];
    uuid_unparse(cntrlr->uuid, uuid_str);
    LOG_DEBUG("(1/4) %s @ %p", uuid_str, cntrlr);
    LOG_DEBUG("(2/4) name: %s", cntrlr->name);
    LOG_DEBUG("(3/4) relays: %3d", cntrlr->relay_count);
    LOG_DEBUG("(4/4) command_port: %5d discovery_port: %5d", cntrlr->command_port, cntrlr->discovery_port);
}