// meter_dts6619.c — Driver Modbus RTU para SINOTIMER DTS6619 (ESP-IDF)

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

#define TAG "serial_mdb_dts6619"

// ===== UART / RS-485 =====
#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

// ===== Timings =====
#define UPDATE_INTERVAL (5000 / portTICK_PERIOD_MS)
#define POLL_INTERVAL (200 / portTICK_PERIOD_MS) // DTS6619 prefere >100 ms

// ===== Helpers =====
#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 =====
static bool is_initialized = false;
static TaskHandle_t meter_task = NULL;

// ====== Config de endianness ======
// 0: usa float lido direto; 1: faz word-swap (DTS6619 em alguns firmwares)
#ifndef DTS6619_WORD_SWAP
#define DTS6619_WORD_SWAP 0
#endif

static inline float maybe_swap_float(float in)
{
#if DTS6619_WORD_SWAP
    // swap de words: 0-1-2-3 -> 2-3-0-1
    float out;
    char *src = (char *)&in;
    char *dst = (char *)&out;
    dst[0] = src[2];
    dst[1] = src[3];
    dst[2] = src[0];
    dst[3] = src[1];
    return out;
#else
    return in;
#endif
}

// ============================================================================
// ===================  MAPA DE REGISTROS DTS6619 (Input 0x04)  ===============
// Todos float32, 2 regs cada, endereços zero-based
// Tensões
#define DTS_L1VOLTAGE 0x0000
#define DTS_L2VOLTAGE 0x0002
#define DTS_L3VOLTAGE 0x0004
// Correntes (total e por fase)
#define DTS_TOTALCURRENT 0x0006
#define DTS_L1CURRENT 0x0008
#define DTS_L2CURRENT 0x000A
#define DTS_L3CURRENT 0x000C
// Potências ativas
#define DTS_TOTALACTIVEPOWER 0x0010
#define DTS_L1ACTIVEPOWER 0x0012
#define DTS_L2ACTIVEPOWER 0x0014
#define DTS_L3ACTIVEPOWER 0x0016
// Fator de potência (por fase)
#define DTS_PF_L1 0x002A
#define DTS_PF_L2 0x002C
#define DTS_PF_L3 0x002E
// Frequência
#define DTS_FREQUENCY 0x0036
// Energia total ativa (Wh)
#define DTS_TOTAL_ACTIVE_ENERGY 0x0100
// ============================================================================

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

// ======= Descritores (usando INPUT registers) =======
const mb_parameter_descriptor_t device_parameters_dts6619[] = {
    {CID_L1_VOLTAGE, STR("L1 Voltage"), STR("V"), 1, MB_PARAM_INPUT, DTS_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_INPUT, DTS_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_INPUT, DTS_L3VOLTAGE, 2,
     HOLD_OFFSET(l3_voltage), PARAM_TYPE_FLOAT, 4, OPTS(0, 300, 0.1), PAR_PERMS_READ},

    {CID_L1_CURRENT, STR("L1 Current"), STR("A"), 1, MB_PARAM_INPUT, DTS_L1CURRENT, 2,
     HOLD_OFFSET(l1_current), PARAM_TYPE_FLOAT, 4, OPTS(0, 1000, 0.1), PAR_PERMS_READ},
    {CID_L2_CURRENT, STR("L2 Current"), STR("A"), 1, MB_PARAM_INPUT, DTS_L2CURRENT, 2,
     HOLD_OFFSET(l2_current), PARAM_TYPE_FLOAT, 4, OPTS(0, 1000, 0.1), PAR_PERMS_READ},
    {CID_L3_CURRENT, STR("L3 Current"), STR("A"), 1, MB_PARAM_INPUT, DTS_L3CURRENT, 2,
     HOLD_OFFSET(l3_current), PARAM_TYPE_FLOAT, 4, OPTS(0, 1000, 0.1), PAR_PERMS_READ},

};
const uint16_t num_device_parameters_dts6619 =
    sizeof(device_parameters_dts6619) / sizeof(device_parameters_dts6619[0]);

// ===== Ponteiro para a struct de holding (vinda do teu projeto)
extern holding_reg_params_t holding_reg_params;

// ===== Acesso a memória local usada pela stack
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);
}

// ===== Post do evento de medição
static void meter_dts6619_post_event(float *voltage, float *current, int *power_w,
                                     float freq_hz, float pf_avg, float total_kwh)
{
    meter_event_data_t evt = {
        .source = "GRID",
        .frequency = freq_hz,
        .power_factor = pf_avg,
        .total_energy = total_kwh};
    memcpy(evt.vrms, voltage, sizeof(evt.vrms));
    memcpy(evt.irms, current, sizeof(evt.irms));
    memcpy(evt.watt, power_w, 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));
    }
}

// ===== Task de polling
static void serial_mdb_task(void *param)
{
    esp_err_t err;
    const mb_parameter_descriptor_t *desc = NULL;
    float v[3] = {0}, i[3] = {0}, p_ph[3] = {0};
    float p_total = 0.0f, pf[3] = {0}, freq = 0.0f, e_total_wh = 0.0f;

    // pequeno settle antes da 1ª leitura
    vTaskDelay(pdMS_TO_TICKS(200));

    while (1)
    {
        for (uint16_t cid = 0; cid < num_device_parameters_dts6619; 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;

            // 1 retry simples em caso de timeout
            err = mbc_master_get_parameter(cid, (char *)desc->param_key, (uint8_t *)data_ptr, &type);
            if (err == ESP_ERR_TIMEOUT)
            {
                vTaskDelay(pdMS_TO_TICKS(60));
                err = mbc_master_get_parameter(cid, (char *)desc->param_key, (uint8_t *)data_ptr, &type);
            }

            if (err == ESP_OK && data_ptr)
            {
                float raw = *(float *)data_ptr;
                float val = maybe_swap_float(raw);

                // logging enxuto
                ESP_LOGI(TAG, "%s: %.3f %s", desc->param_key, val, desc->param_units);

                switch (cid)
                {
                case CID_L1_VOLTAGE:
                    v[0] = val;
                    break;
                case CID_L2_VOLTAGE:
                    v[1] = val;
                    break;
                case CID_L3_VOLTAGE:
                    v[2] = val;
                    break;

                case CID_L1_CURRENT:
                    i[0] = val;
                    break;
                case CID_L2_CURRENT:
                    i[1] = val;
                    break;
                case CID_L3_CURRENT:
                    i[2] = val;
                    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);
        }

        // prepara payload do evento
        int p_int[3] = {
            (int)(p_ph[0]),
            (int)(p_ph[1]),
            (int)(p_ph[2])};
        // PF médio simples (ignora zeros)
        float pf_sum = 0.0f;
        int pf_cnt = 0;
        for (int k = 0; k < 3; ++k)
        {
            if (pf[k] != 0.0f)
            {
                pf_sum += pf[k];
                pf_cnt++;
            }
        }
        float pf_avg = (pf_cnt ? pf_sum / pf_cnt : 0.0f);

        // energia em kWh se veio em Wh
        float total_kwh = e_total_wh / 1000.0f;

        meter_dts6619_post_event(v, i, p_int, freq, pf_avg, total_kwh);
        vTaskDelay(UPDATE_INTERVAL);
    }
}

// ============ Init / Start / Stop ============
esp_err_t meter_dts6619_init(void)
{
    if (is_initialized)
    {
        ESP_LOGW(TAG, "Already initialized");
        return ESP_ERR_INVALID_STATE;
    }

    // limpa UART se já houver driver
    if (uart_is_driver_installed(MB_PORT_NUM))
    {
        uart_driver_delete(MB_PORT_NUM);
        ESP_LOGI(TAG, "UART driver deleted");
    }

    // destruir master anterior (ignora erro se não estiver init)
    (void)mbc_master_destroy();

    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(50));

    ESP_ERROR_CHECK(mbc_master_set_descriptor(device_parameters_dts6619, num_device_parameters_dts6619));

    is_initialized = true;
    ESP_LOGI(TAG, "DTS6619 Modbus master initialized (9600 8E1, Input Reg 0x04)");
    return ESP_OK;
}

esp_err_t meter_dts6619_start(void)
{
    if (!is_initialized)
    {
        ESP_LOGE(TAG, "Not initialized");
        return ESP_ERR_INVALID_STATE;
    }
    if (meter_task == NULL)
    {
        xTaskCreate(serial_mdb_task, "meter_dts6619_task", 4096, NULL, 3, &meter_task);
        ESP_LOGI(TAG, "Task started");
    }
    return ESP_OK;
}

void meter_dts6619_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");
    }

    (void)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 DTS6619 cleaned up");
}
