#ifndef ENERGY_METER_H_
#define ENERGY_METER_H_

#include <stdbool.h>
#include <stdint.h>
#include "esp_err.h"

/**
 * @brief Grid energy meter model
 *
 */

typedef enum
{
    ENERGY_METER_NONE,
    ENERGY_METER_ORNO_515,
    ENERGY_METER_ORNO_517,
} meter_model_t;

/**
 * @brief Initialize energy meter
 *
 */
void energy_meter_init(void);

/**
 * @brief Get state of energy meter
 *
 * @return bool
 */
bool meter_get_state(void);

/**
 * @brief Get mode of energy meter, stored in NVS
 *
 * @return meter_model_t
 */
meter_model_t meter_get_model(void);

/**
 * @brief Set mode of energy meter, stored in NVS
 *
 * @param mode
 * @return esp_err_t
 */
esp_err_t meter_set_model(meter_model_t mode);


/**
 * @brief Start energy meter session, if not started
 *
 */
void energy_meter_start_session(void);

/**
 * @brief Stop energy meter session, if not stopped
 *
 */
void energy_meter_stop_session(void);

/**
 * @brief Process energy meter
 *
 * @param charging
 * @param charging_current
 */
void energy_meter_process(bool charging, uint16_t charging_current);

/**
 * @brief Get session actual power
 *
 * @return Power in W
 */
uint32_t energy_meter_get_power(void);

/**
 * @brief Get session time
 *
 * @return Time in s
 */
uint32_t energy_meter_get_session_time(void);

/**
 * @brief Get charging time
 *
 * @return Time in s
 */
uint32_t energy_meter_get_charging_time(void);

/**
 * @brief Get session consumption
 *
 * @return Consumption in Wh
 */
uint32_t energy_meter_get_consumption(void);

/**
 * @brief After energy_meter_process, get current measured voltage
 *
 * @param voltage Output array of 3 items, values in V
 */
void energy_meter_get_voltage(float *voltage);

/**
 * @brief Cet current measured voltage on L1
 *
 * @return Voltage in V
 */
float energy_meter_get_l1_voltage(void);

/**
 * @brief Cet current measured voltage on L2
 *
 * @return Voltage in V
 */
float energy_meter_get_l2_voltage(void);

/**
 * @brief Cet current measured voltage on L3
 *
 * @return Voltage in V
 */
float energy_meter_get_l3_voltage(void);

/**
 * @brief After energy_meter_process, get current measured current
 *
 * @param voltage Output array of 3 items, values in A
 */
void energy_meter_get_current(float *current);

/**
 * @brief Cet current measured current on L1
 *
 * @return Voltage in V
 */
float energy_meter_get_l1_current(void);

/**
 * @brief Cet current measured current on L2
 *
 * @return Voltage in V
 */
float energy_meter_get_l2_current(void);

/**
 * @brief Cet current measured current on L3
 *
 * @return Voltage in V
 */
float energy_meter_get_l3_current(void);

/**
 * @brief Serialize to string
 *
 * @param mode
 * @return const char*
 */
const char *meter_model_to_str(meter_model_t mode);

/**
 * @brief Parse from string
 *
 * @param str
 * @return meter_model_t
 */
meter_model_t meter_str_to_model(const char *str);


/**
 * @brief Serialize to string
 *
 * @param mode
 * @return const char*
 */
const char *meter_state_to_str(bool state);


#endif /* ENERGY_METER_H_ */
