#include "ccs811.h"

i2c_device_config_t CCS811_DEV_CFG = {
    .dev_addr_length = I2C_ADDR_BIT_LEN_7,
    .device_address = CCS811_ADDRESS,
    .scl_speed_hz = 100000,
};

i2c_master_dev_handle_t CCS811_DEV_HANDLE;

esp_err_t ccs811_getStatus()
{
    uint8_t errorID;
    uint8_t status;
    uint8_t hardwareVersion;
    uint16_t version;
    uint16_t bootVersion;

    esp_err_t ret = i2c_read_register_8(CCS811_DEV_HANDLE, CCS811_REG_STATUS, &status);
    if (ret != ESP_OK) {return ret;}
    ret = i2c_read_register_16(CCS811_DEV_HANDLE, CCS811_REG_FW_APP_VERSION, &version);
    if (ret != ESP_OK) {return ret;}
    ret = i2c_read_register_16(CCS811_DEV_HANDLE, CCS811_REG_FW_BOOT_VERSION, &bootVersion);
    if (ret != ESP_OK) {return ret;}
    ret = i2c_read_register_8(CCS811_DEV_HANDLE, CCS811_REG_HW_VERSION, &hardwareVersion);
    if (ret != ESP_OK) {return ret;}
    ret = i2c_read_register_8(CCS811_DEV_HANDLE, CCS811_REG_ERROR_ID, &errorID);
    ESP_LOGW(TAG_CCS, "CCS811 status: %d, version: %d, boot version: %d, hardware version: %d, error ID: %d", status, version, bootVersion, hardwareVersion, errorID);
    return ret;
}

esp_err_t ccs811_init()
{
    esp_err_t ret =i2c_master_bus_add_device(i2c0_bus_hdl, &CCS811_DEV_CFG, &CCS811_DEV_HANDLE);
    if (ret != ESP_OK) {return ret;}
    vTaskDelay(10 / portTICK_PERIOD_MS);
    uint8_t reset_seq[4] = {0x11, 0xE5, 0x72, 0x8A};
    ret = i2c_write_register(CCS811_DEV_HANDLE, CCS811_REG_SW_RESET, reset_seq, sizeof(reset_seq)); // Reset
    if (ret != ESP_OK) {return ret;}
    vTaskDelay(10 / portTICK_PERIOD_MS);

    ret = ccs811_getStatus();
    if (ret != ESP_OK) {return ret;}
    ret = i2c_write_register(CCS811_DEV_HANDLE, CCS811_REG_APP_START, NULL, 0); // start
    if (ret != ESP_OK) {return ret;}
    vTaskDelay(10 / portTICK_PERIOD_MS);
    ret = i2c_write_register_8(CCS811_DEV_HANDLE, CCS811_REG_MEAS_MODE, 0x40); // MODE 1 interrupts vypnuté
    if (ret != ESP_OK) {return ret;}
    ret = ccs811_getStatus();
    return ret;
}
esp_err_t ccs811_get_data(uint16_t *eCO2, uint16_t *tvoc, uint8_t *current, uint16_t *rawData)
{
    uint8_t algResultData[8];
    esp_err_t ret = i2c_read_register(CCS811_DEV_HANDLE, CCS811_REG_ALG_RESULT_DATA, algResultData, 8);

    if (ret == ESP_OK)
    {
        *eCO2 = ((uint16_t)algResultData[0] << 8) | algResultData[1];
        *tvoc = ((uint16_t)algResultData[2] << 8) | algResultData[3];
        *current = algResultData[6] >> 2;
        *rawData = ((uint16_t)(algResultData[6] & 0x03) << 8) | algResultData[7];

        ESP_LOGI(TAG_CCS, "CCS Status: %d, Error: %d", algResultData[4], algResultData[5]);
        return ESP_OK;
    }

    return ret;
}

esp_err_t ccs811_set_env_data(float temperature, float humidity)
{
    uint8_t envData[4];

    // Convert humidity: %RH × 512 (rounding)
    uint16_t hum_conv = (uint16_t)(humidity * 512.0f + 0.5f);
    envData[0] = (hum_conv >> 8) & 0xFF;
    envData[1] = hum_conv & 0xFF;

    // Convert temperature: (°C + 25) × 512
    uint16_t temp_conv = (uint16_t)((temperature + 25.0f) * 512.0f + 0.5f);
    envData[2] = (temp_conv >> 8) & 0xFF;
    envData[3] = temp_conv & 0xFF;

    return i2c_write_register(CCS811_DEV_HANDLE, CCS811_REG_ENV_DATA, envData, 4);
}
