#include "meter_orno516.h"
#include "meter_events.h"
#include "modbus_params.h"
#include "mbcontroller.h"
#include "esp_log.h"
#include "driver/uart.h"
#include <stddef.h>

#define TAG "serial_mdb_orno516"

#define MB_PORT_NUM 2
#define MB_DEV_SPEED 9600
#define MB_UART_TXD 17
#define MB_UART_RXD 16
#define MB_UART_RTS 2

#define UPDATE_INTERVAL (5000 / portTICK_PERIOD_MS)
#define POLL_INTERVAL   (100 / portTICK_PERIOD_MS)

#define HOLD_OFFSET(field) ((uint16_t)(offsetof(holding_reg_params_t, field) + 1))
#define STR(fieldname) ((const char *)(fieldname))
#define OPTS(min_val, max_val, step_val) {.opt1 = min_val, .opt2 = max_val, .opt3 = step_val}

// Estado do driver
static bool is_initialized = false;
static TaskHandle_t meter_task = NULL;

#define L1VOLTAGE         0x000E
#define L2VOLTAGE         0x0010
#define L3VOLTAGE         0x0012
#define L1CURRENT         0x0016
#define L2CURRENT         0x0018
#define L3CURRENT         0x001A
#define TOTALACTIVEPOWER  0x001C

enum {
    CID_L1_CURRENT = 0,
    CID_L2_CURRENT,
    CID_L3_CURRENT,
    CID_L1_VOLTAGE,
    CID_L2_VOLTAGE,
    CID_L3_VOLTAGE,
    CID_ACTIVE_POWER
};

const mb_parameter_descriptor_t device_parameters_orno516[] = {
    {CID_L1_CURRENT, STR("L1 Current"), STR("A"), 1, MB_PARAM_HOLDING, L1CURRENT, 2,
     HOLD_OFFSET(l1_current), PARAM_TYPE_FLOAT, 4, OPTS(-1000, 1000, 0.1), PAR_PERMS_READ},
    {CID_L2_CURRENT, STR("L2 Current"), STR("A"), 1, MB_PARAM_HOLDING, L2CURRENT, 2,
     HOLD_OFFSET(l2_current), PARAM_TYPE_FLOAT, 4, OPTS(-1000, 1000, 0.1), PAR_PERMS_READ},
    {CID_L3_CURRENT, STR("L3 Current"), STR("A"), 1, MB_PARAM_HOLDING, L3CURRENT, 2,
     HOLD_OFFSET(l3_current), PARAM_TYPE_FLOAT, 4, OPTS(-1000, 1000, 0.1), PAR_PERMS_READ},
    {CID_L1_VOLTAGE, STR("L1 Voltage"), STR("V"), 1, MB_PARAM_HOLDING, L1VOLTAGE, 2,
     HOLD_OFFSET(l1_voltage), PARAM_TYPE_FLOAT, 4, OPTS(0, 300, 0.1), PAR_PERMS_READ},
    {CID_L2_VOLTAGE, STR("L2 Voltage"), STR("V"), 1, MB_PARAM_HOLDING, L2VOLTAGE, 2,
     HOLD_OFFSET(l2_voltage), PARAM_TYPE_FLOAT, 4, OPTS(0, 300, 0.1), PAR_PERMS_READ},
    {CID_L3_VOLTAGE, STR("L3 Voltage"), STR("V"), 1, MB_PARAM_HOLDING, L3VOLTAGE, 2,
     HOLD_OFFSET(l3_voltage), PARAM_TYPE_FLOAT, 4, OPTS(0, 300, 0.1), PAR_PERMS_READ},
    {CID_ACTIVE_POWER, STR("Active Power"), STR("W"), 1, MB_PARAM_HOLDING, TOTALACTIVEPOWER, 2,
     HOLD_OFFSET(active_power), PARAM_TYPE_FLOAT, 4, OPTS(0, 100000, 1), PAR_PERMS_READ}
};

const uint16_t num_device_parameters_orno516 = sizeof(device_parameters_orno516) / sizeof(device_parameters_orno516[0]);

float ReverseFloat(const float inFloat) {
    float retVal;
    char *floatToConvert = (char *)&inFloat;
    char *returnFloat = (char *)&retVal;
    returnFloat[0] = floatToConvert[2];
    returnFloat[1] = floatToConvert[3];
    returnFloat[2] = floatToConvert[0];
    returnFloat[3] = floatToConvert[1];
    return retVal;
}

static void *get_param_ptr(const mb_parameter_descriptor_t *param) {
    if (!param || param->param_offset == 0) return NULL;
    return ((uint8_t *)&holding_reg_params + param->param_offset - 1);
}


static void meter_orno516_post_event(float *voltage, float *current, int *power) {
    meter_event_data_t evt = {
        .source = "GRID",
        .frequency = 0.0f, // ORNO-516 não fornece
        .power_factor = 0.0f, // idem
        .total_energy = 0.0f // idem
    };
    memcpy(evt.vrms, voltage, sizeof(evt.vrms));
    memcpy(evt.irms, current, sizeof(evt.irms));
    memcpy(evt.watt, power, sizeof(evt.watt));

    esp_err_t err = esp_event_post(METER_EVENT, METER_EVENT_DATA_READY,
                                   &evt, sizeof(evt), portMAX_DELAY);

    if (err != ESP_OK) {
        ESP_LOGW(TAG, "Falha ao emitir evento: %s", esp_err_to_name(err));
    }
}

static void serial_mdb_task(void *param) {
    esp_err_t err;
    const mb_parameter_descriptor_t *desc = NULL;
    float voltage[3] = {0}, current[3] = {0};
    int power[3] = {0};

    while (1) {
        for (uint16_t cid = 0; cid < num_device_parameters_orno516; cid++) {
            err = mbc_master_get_cid_info(cid, &desc);
            if (err != ESP_OK || !desc) continue;

            void *data_ptr = get_param_ptr(desc);
            uint8_t type = 0;
            err = mbc_master_get_parameter(cid, (char *)desc->param_key, (uint8_t *)data_ptr, &type);

            if (err == ESP_OK && data_ptr) {
                float val = ReverseFloat(*(float *)data_ptr);
                ESP_LOGI(TAG, "%s: %.2f %s", desc->param_key, val, desc->param_units);

                switch (cid) {
                    case CID_L1_VOLTAGE: voltage[0] = val; break;
                    case CID_L2_VOLTAGE: voltage[1] = val; break;
                    case CID_L3_VOLTAGE: voltage[2] = val; break;
                    case CID_L1_CURRENT: current[0] = val; break;
                    case CID_L2_CURRENT: current[1] = val; break;
                    case CID_L3_CURRENT: current[2] = val; break;
                    case CID_ACTIVE_POWER:
                        power[0] = (int)(val / 3);
                        power[1] = (int)(val / 3);
                        power[2] = (int)(val / 3);
                        break;
                    default:
                        break;
                }
            } else {
                ESP_LOGE(TAG, "CID %u (%s) read failed: %s", cid, desc->param_key, esp_err_to_name(err));
            }

            vTaskDelay(POLL_INTERVAL);
        }

        meter_orno516_post_event(voltage, current, power);
        vTaskDelay(UPDATE_INTERVAL);
    }
}


esp_err_t meter_orno516_init(void) {
    if (is_initialized) {
        ESP_LOGW(TAG, "Already initialized");
        return ESP_ERR_INVALID_STATE;
    }

    // Tenta apagar UART apenas se estiver inicializada
    if (uart_is_driver_installed(MB_PORT_NUM)) {
        uart_driver_delete(MB_PORT_NUM);
        ESP_LOGI(TAG, "UART driver deleted");
    }

    mbc_master_destroy(); // OK mesmo que não esteja inicializado

    mb_communication_info_t comm = {
        .port = MB_PORT_NUM,
        .mode = MB_MODE_RTU,
        .baudrate = MB_DEV_SPEED,
        .parity = UART_PARITY_EVEN
    };

    void *handler = NULL;
    ESP_ERROR_CHECK(mbc_master_init(MB_PORT_SERIAL_MASTER, &handler));
    ESP_ERROR_CHECK(mbc_master_setup(&comm));
    ESP_ERROR_CHECK(uart_set_pin(MB_PORT_NUM, MB_UART_TXD, MB_UART_RXD, MB_UART_RTS, UART_PIN_NO_CHANGE));
    ESP_ERROR_CHECK(mbc_master_start());
    ESP_ERROR_CHECK(uart_set_mode(MB_PORT_NUM, UART_MODE_RS485_HALF_DUPLEX));
    vTaskDelay(pdMS_TO_TICKS(5));
    ESP_ERROR_CHECK(mbc_master_set_descriptor(device_parameters_orno516, num_device_parameters_orno516));

    is_initialized = true;
    return ESP_OK;
}

esp_err_t meter_orno516_start(void) {
    if (!is_initialized) {
        ESP_LOGE(TAG, "Not initialized");
        return ESP_ERR_INVALID_STATE;
    }

    if (meter_task == NULL) {
        xTaskCreate(serial_mdb_task, "meter_orno516_task", 4096, NULL, 3, &meter_task);
        ESP_LOGI(TAG, "Task started");
    }

    return ESP_OK;
}

void meter_orno516_stop(void) {
    if (!is_initialized) {
        ESP_LOGW(TAG, "Not initialized, skipping stop");
        return;
    }

    if (meter_task) {
        vTaskDelete(meter_task);
        meter_task = NULL;
        ESP_LOGI(TAG, "Task stopped");
    }

    mbc_master_destroy();

    if (uart_is_driver_installed(MB_PORT_NUM)) {
        uart_driver_delete(MB_PORT_NUM);
        ESP_LOGI(TAG, "UART driver deleted");
    }

    is_initialized = false;
    ESP_LOGI(TAG, "Meter ORNO-516 cleaned up");
}
