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

#define TAG "serial_mdb_orno513"


#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    (3000 / 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(x) ((const char *)(x))
#define OPTS(min, max, step) {.opt1 = min, .opt2 = max, .opt3 = step}

// State flag
static bool is_initialized = false;
static TaskHandle_t meter_task = NULL;

// CID enums
enum {
    CID_TOTAL_ACTIVE_ENERGY = 0,
    CID_TOTAL_REACTIVE_ENERGY,
    CID_ACTIVE_POWER,
    CID_APPARENT_POWER,
    CID_REACTIVE_POWER,
    CID_L1_CURRENT,
    CID_L1_VOLTAGE
};

// Register addresses
#define TOTALFACTIVE     0x010E
#define TOTALRACTIVE     0x0118
#define ACTIVEPOWER      0x0104
#define APPARENTPOWER    0x0106
#define REACTIVEPOWER    0x0108
#define L1CURRENT        0x0102
#define L1VOLTAGE        0x0100

const mb_parameter_descriptor_t device_parameters_orno513[] = {
    {CID_TOTAL_ACTIVE_ENERGY, STR("Total Active Energy"), STR("kWh"), 1, MB_PARAM_HOLDING, TOTALFACTIVE, 2,
     HOLD_OFFSET(active_power), PARAM_TYPE_I32_CDAB, 4, OPTS(0, 100000, 1), PAR_PERMS_READ},

    {CID_TOTAL_REACTIVE_ENERGY, STR("Total Reactive Energy"), STR("kWh"), 1, MB_PARAM_HOLDING, TOTALRACTIVE, 2,
     HOLD_OFFSET(reactive_power), PARAM_TYPE_I32_CDAB, 4, OPTS(0, 100000, 1), PAR_PERMS_READ},

    {CID_ACTIVE_POWER, STR("Active Power"), STR("W"), 1, MB_PARAM_HOLDING, ACTIVEPOWER, 2,
     HOLD_OFFSET(active_power), PARAM_TYPE_I32_CDAB, 4, OPTS(-100000, 100000, 1), PAR_PERMS_READ},

    {CID_APPARENT_POWER, STR("Apparent Power"), STR("VA"), 1, MB_PARAM_HOLDING, APPARENTPOWER, 2,
     HOLD_OFFSET(apparent_power), PARAM_TYPE_I32_CDAB, 4, OPTS(0, 100000, 1), PAR_PERMS_READ},

    {CID_REACTIVE_POWER, STR("Reactive Power"), STR("VAR"), 1, MB_PARAM_HOLDING, REACTIVEPOWER, 2,
     HOLD_OFFSET(reactive_power), PARAM_TYPE_I32_CDAB, 4, OPTS(-100000, 100000, 1), PAR_PERMS_READ},

    {CID_L1_CURRENT, STR("L1 Current"), STR("A"), 1, MB_PARAM_HOLDING, L1CURRENT, 2,
     HOLD_OFFSET(l1_current), PARAM_TYPE_I32_CDAB, 4, OPTS(0, 100, 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_I32_CDAB, 4, OPTS(0, 300, 0.1), PAR_PERMS_READ}
};

const uint16_t num_device_parameters_orno513 = sizeof(device_parameters_orno513) / sizeof(device_parameters_orno513[0]);

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 serial_mdb_task(void *param) {
    esp_err_t err;
    const mb_parameter_descriptor_t *desc = NULL;

    float voltage[3] = {0};
    float current[3] = {0};
    int watt[3] = {0};
    float energy = 0.0f;

    while (1) {
        for (uint16_t cid = 0; cid < num_device_parameters_orno513; 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) {
                int32_t raw = *(int32_t *)data_ptr;
                float val = raw / 10.0f;
                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_L1_CURRENT: current[0] = val; break;
                    case CID_ACTIVE_POWER:
                        watt[0] = (int)(val);
                        watt[1] = watt[0];
                        watt[2] = watt[0];
                        break;
                    case CID_TOTAL_ACTIVE_ENERGY:
                        energy = val / 1000.0f;
                        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_event_data_t evt = {
            .frequency = 0.0f,
            .power_factor = 0.0f,
            .total_energy = energy,
            .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);
    }
}

esp_err_t meter_orno513_init(void) {
    if (is_initialized) {
        ESP_LOGW(TAG, "meter_orno513 already initialized");
        return ESP_ERR_INVALID_STATE;
    }

    ESP_LOGI(TAG, "meter_orno513_init");

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

    void *handler = NULL;

    esp_err_t err = mbc_master_init(MB_PORT_SERIAL_MASTER, &handler);
    if (err != ESP_OK) {
        ESP_LOGE(TAG, "mbc_master_init failed");
        return err;
    }

    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_orno513, num_device_parameters_orno513));

    is_initialized = true;
    return ESP_OK;
}

esp_err_t meter_orno513_start(void) {

    ESP_LOGI(TAG, "meter_orno513_start");

    if (!is_initialized) {
        ESP_LOGE(TAG, "meter_orno513 not initialized");
        return ESP_ERR_INVALID_STATE;
    }

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

    return ESP_OK;
}

void meter_orno513_stop(void) {
    if (!is_initialized) {
        ESP_LOGW(TAG, "meter_orno513 not initialized");
        return;
    }

    ESP_LOGI(TAG, "Stopping meter_orno513");

    uart_driver_delete(MB_PORT_NUM);

    esp_err_t err = mbc_master_destroy();
    if (err != ESP_OK) {
        ESP_LOGW(TAG, "mbc_master_destroy() returned %s", esp_err_to_name(err));
    }

    is_initialized = false;
}
