// components/meter_manager/driver/meter_dds661.c

#include "meter_dds661.h"

#include "modbus_params.h"
#include "mbcontroller.h"
#include "meter_events.h"

#include "esp_log.h"
#include "driver/uart.h"
#include <string.h>
#include <math.h>

#define TAG "serial_mdb_dds661"

// ======= UART/Modbus config =======
#define MB_PORT_NUM 2
#define MB_DEV_SPEED 9600

// Ajuste os pinos conforme seu hardware (evite GPIO2 para RTS/DE/RE se possível)
#define MB_UART_TXD 17
#define MB_UART_RXD 16
#define MB_UART_RTS 2 // pino DE/RE do transceiver RS-485

#define UPDATE_INTERVAL (3000 / portTICK_PERIOD_MS)
#define POLL_INTERVAL (120 / portTICK_PERIOD_MS)

// ======= Helpers típicos do teu projeto =======
#define HOLD_OFFSET(field) ((uint16_t)(offsetof(holding_reg_params_t, field) + 1))
#define STR(x) ((const char *)(x))
#define OPTS(min, max, step) {.opt1 = min, .opt2 = max, .opt3 = step}

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

// ======= CIDs (sequenciais) =======
enum
{
    CID_VOLTAGE = 0,
    CID_CURRENT,
    CID_ACTIVE_POWER_KW,
    CID_POWER_FACTOR,
    CID_FREQUENCY,
    CID_TOTAL_ACTIVE_ENERGY_KWH,
    CID_COUNT
};

// ======= Mapa de registradores (Input Registers; FC=0x04) =======
// Endereços típicos para DDS-661 (float32):
#define REG_VOLTAGE 0x0000         // V    (float32)
#define REG_CURRENT 0x0008         // A    (float32)
#define REG_ACTIVE_POWER_KW 0x0012 // kW   (float32)
#define REG_POWER_FACTOR 0x002A    // PF   (float32)
#define REG_FREQUENCY 0x0036       // Hz   (float32)
#define REG_E_ACTIVE_KWH 0x0100    // kWh  (float32)

// ======= Tabela de parâmetros (Data Dictionary) =======
const mb_parameter_descriptor_t device_parameters_dds661[] = {
    {CID_VOLTAGE, "Voltage", "V", 1,
     MB_PARAM_INPUT, REG_VOLTAGE, 2, HOLD_OFFSET(l1_voltage),
     PARAM_TYPE_FLOAT_CDAB, 4, OPTS(0, 300, 0.1), PAR_PERMS_READ},

    {CID_CURRENT, "Current", "A", 1,
     MB_PARAM_INPUT, REG_CURRENT, 2, HOLD_OFFSET(l1_current),
     PARAM_TYPE_FLOAT_CDAB, 4, OPTS(0, 100, 0.1), PAR_PERMS_READ},

    {CID_ACTIVE_POWER_KW, "Active Power", "kW", 1,
     MB_PARAM_INPUT, REG_ACTIVE_POWER_KW, 2, HOLD_OFFSET(active_power),
     PARAM_TYPE_FLOAT_CDAB, 4, OPTS(-100, 100, 0.01), PAR_PERMS_READ},

    {CID_POWER_FACTOR, "Power Factor", "", 1,
     MB_PARAM_INPUT, REG_POWER_FACTOR, 2, HOLD_OFFSET(power_factor),
     PARAM_TYPE_FLOAT_CDAB, 4, OPTS(-1, 1, 0.001), PAR_PERMS_READ},

    {CID_FREQUENCY, "Frequency", "Hz", 1,
     MB_PARAM_INPUT, REG_FREQUENCY, 2, HOLD_OFFSET(frequency),
     PARAM_TYPE_FLOAT_CDAB, 4, OPTS(0, 100, 0.1), PAR_PERMS_READ},

    {CID_TOTAL_ACTIVE_ENERGY_KWH, "Total Active Energy", "kWh", 1,
     MB_PARAM_INPUT, REG_E_ACTIVE_KWH, 2, HOLD_OFFSET(active_energy),
     PARAM_TYPE_FLOAT_CDAB, 4, OPTS(0, 1000000, 0.01), PAR_PERMS_READ},
};

const uint16_t num_device_parameters_dds661 =
    sizeof(device_parameters_dds661) / sizeof(device_parameters_dds661[0]);

// ======= Ponteiro para buffer destino =======
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);
}

// ======= Tarefa de aquisição =======
static void serial_mdb_task(void *param)
{
    esp_err_t err;
    const mb_parameter_descriptor_t *desc = NULL;

    // Valores lidos
    float v = 0.0f;     // V
    float i = 0.0f;     // A
    float pf = 0.0f;    // -
    float hz = 0.0f;    // Hz
    float e_kwh = 0.0f; // kWh
    float p_kw = 0.0f;  // kW

    // Buffers para o evento
    float voltage[3] = {0};
    float current[3] = {0};
    int watt[3] = {0};

    while (1)
    {
        for (uint16_t cid = 0; cid < num_device_parameters_dds661; cid++)
        {
            err = mbc_master_get_cid_info(cid, &desc);
            if (err != ESP_OK || !desc)
            {
                ESP_LOGE(TAG, "get_cid_info(%u) failed: %s", cid, esp_err_to_name(err));
                continue;
            }

            void *data_ptr = get_param_ptr(desc);
            if (!data_ptr)
            {
                ESP_LOGE(TAG, "CID %u (%s): null data_ptr", cid, desc->param_key);
                continue;
            }

            uint8_t type = 0;
            err = mbc_master_get_parameter(cid, (char *)desc->param_key, (uint8_t *)data_ptr, &type);
            if (err != ESP_OK)
            {
                ESP_LOGE(TAG, "CID %u (%s) read failed: %s", cid, desc->param_key, esp_err_to_name(err));
                vTaskDelay(POLL_INTERVAL);
                continue;
            }

            // Dump dos bytes recebidos (4 bytes do float bruto)
            uint8_t raw[4];
            memcpy(raw, data_ptr, 4);
            ESP_LOGD(TAG, "CID %u (%s) raw bytes: %02X %02X %02X %02X",
                     cid, desc->param_key, raw[0], raw[1], raw[2], raw[3]);

            float val = 0.0f;

            val = *(float *)data_ptr;

            ESP_LOGD(TAG, "%s: %.3f %s", desc->param_key, val, desc->param_units);

            switch (cid)
            {
            case CID_VOLTAGE:
                v = val;
                voltage[0] = v;
                break;
            case CID_CURRENT:
                i = val;
                current[0] = i;
                break;
            case CID_POWER_FACTOR:
                pf = val;
                break;
            case CID_FREQUENCY:
                hz = val;
                break;
            case CID_ACTIVE_POWER_KW:
            {
                p_kw = val;
                float p_w = p_kw * 1000.0f;
                int pwi = (int)lrintf(p_w);
                watt[0] = pwi;
                watt[1] = pwi;
                watt[2] = pwi;
                break;
            }
            case CID_TOTAL_ACTIVE_ENERGY_KWH:
                e_kwh = val;
                break;
            default:
                break;
            }

            vTaskDelay(POLL_INTERVAL);
        }

        meter_event_data_t evt = {
            .frequency = hz,
            .power_factor = pf,
            .total_energy = e_kwh,
            .source = "GRID",
        };
        memcpy(evt.vrms, voltage, sizeof(evt.vrms));
        memcpy(evt.irms, current, sizeof(evt.irms));
        memcpy(evt.watt, watt, sizeof(evt.watt));

        esp_event_post(METER_EVENT, METER_EVENT_DATA_READY, &evt, sizeof(evt), portMAX_DELAY);
        vTaskDelay(UPDATE_INTERVAL);
    }
}

// ======= API pública =======
esp_err_t meter_dds661_init(void)
{
    if (is_initialized)
    {
        ESP_LOGW(TAG, "meter_dds661 already initialized");
        return ESP_ERR_INVALID_STATE;
    }

    ESP_LOGI(TAG, "meter_dds661_init");

    mb_communication_info_t comm = {
        .port = MB_PORT_NUM,
        .mode = MB_MODE_RTU,
        .baudrate = MB_DEV_SPEED,
        .parity = UART_PARITY_EVEN, // DDS-661: 9600 8E1
    };

    void *handler = NULL;
    ESP_ERROR_CHECK(mbc_master_init(MB_PORT_SERIAL_MASTER, &handler));
    ESP_ERROR_CHECK(mbc_master_setup(&comm));

    // Pinos e parâmetros básicos
    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(uart_set_word_length(MB_PORT_NUM, UART_DATA_8_BITS));
    ESP_ERROR_CHECK(uart_set_hw_flow_ctrl(MB_PORT_NUM, UART_HW_FLOWCTRL_DISABLE, 0));
    ESP_ERROR_CHECK(uart_set_stop_bits(MB_PORT_NUM, UART_STOP_BITS_1));

    // >>> IMPORTANTE: start antes do set_mode <<<
    ESP_ERROR_CHECK(mbc_master_start());

    // Só agora muda para RS485 half duplex
    ESP_ERROR_CHECK(uart_set_mode(MB_PORT_NUM, UART_MODE_RS485_HALF_DUPLEX));

    // (opcional) logs de debug Modbus
    esp_log_level_set("MB_CONTROLLER_MASTER", ESP_LOG_DEBUG);
    esp_log_level_set("MB_PORT_COMMON", ESP_LOG_DEBUG);
    esp_log_level_set("MB_SERIAL_MASTER", ESP_LOG_DEBUG);

    vTaskDelay(pdMS_TO_TICKS(5));

    ESP_ERROR_CHECK(mbc_master_set_descriptor(device_parameters_dds661, num_device_parameters_dds661));

    is_initialized = true;
    return ESP_OK;
}

esp_err_t meter_dds661_start(void)
{
    if (!is_initialized)
    {
        ESP_LOGE(TAG, "meter_dds661 not initialized");
        return ESP_ERR_INVALID_STATE;
    }

    if (meter_task == NULL)
    {
        xTaskCreate(serial_mdb_task, "meter_dds661_task", 4096, NULL, 3, &meter_task);
        ESP_LOGI(TAG, "meter_dds661 task started");
    }

    return ESP_OK;
}

void meter_dds661_stop(void)
{
    if (!is_initialized)
    {
        ESP_LOGW(TAG, "meter_dds661 not initialized");
        return;
    }
    ESP_LOGI(TAG, "Stopping meter_dds661");

    // 1) Destrói o master primeiro
    esp_err_t err = mbc_master_destroy();
    if (err != ESP_OK)
    {
        ESP_LOGW(TAG, "mbc_master_destroy() returned %s", esp_err_to_name(err));
    }

    // 2) Depois solta a UART
    uart_driver_delete(MB_PORT_NUM);

    is_initialized = false;
}
