#pragma once

#include <DHT.h>

#define DHT_SENSOR_PIN 7 // Define DHT Connected Data Pin
#define DHT_TYPE DHT11 // Define DHT Sensor Type
DHT dht_sensor(DHT_SENSOR_PIN, DHT_TYPE); // Set info for dht

// create namespace
namespace Temperature{
    unsigned long previousMillis = 0;
    unsigned long timerInterval = 3000;

    float temp = 0;
    float hum = 0;

    void init();

    void handle(SimpleSerialProtocol *endpoint);
    // void handlePacket(SimpleSerialProtocol *endpoint, char* id, char* action, char* data);
    // void sendPacket(SimpleSerialProtocol *endpoint);
}

void Temperature::init(){
    dht_sensor.begin();
    Serial.println("temp sensor init");
}

void Temperature::handle(SimpleSerialProtocol *endpoint){
    if ((millis() - previousMillis) > timerInterval){
        previousMillis = millis();
        // Temperature::sendPacket(endpoint);

        temp = dht_sensor.readTemperature();
        hum = dht_sensor.readHumidity();

        // Get temperature and convert float to char[]
        char currentTemp[10]; 
        dtostrf(temp, 9, 1, currentTemp);
        

        // Get humidity and convert float to char[]
        char currentHum[10];
        dtostrf(hum, 9, 1, currentHum);

        // Send packet temp
        char tempDataBuffer[64];
        sprintf(tempDataBuffer, "%s;%s;%s", "float", "main_temp", currentTemp);
        endpoint->sendPacket("access", "update_state", tempDataBuffer);

        // Send packet temp
        char humDataBuffer[64];
        sprintf(humDataBuffer, "%s;%s;%s", "float", "main_hum", currentHum);
        endpoint->sendPacket("access", "update_state", humDataBuffer);


    
    }
}

// void Temperature::handlePacket(SimpleSerialProtocol *endpoint, char* id, char* action, char* data){
//     if (strcmp(action, "get_temperature_data") == 0) {
//         // Get temperature and convert float to char[]
//         char currentTemp[10]; 
//         dtostrf(temp, 9, 1, currentTemp);

//         // Get humidity and convert float to char[]
//         char currentHum[10];
//         dtostrf(hum, 9, 1, currentHum);

//         // Send packet
//         char dataBuffer[40];
//         sprintf(dataBuffer, "%s,%s", currentTemp, currentHum);
        
//         endpoint->sendPacket(id, "climate_data", dataBuffer);
//   }
// }

// void sendPacket(SimpleSerialProtocol *endpoint){
//     // Get temperature and convert float to char[]
//     char currentTemp[10]; 
//     dtostrf(readTemp(), 9, 1, currentTemp);
    

//     // Get humidity and convert float to char[]
//     char currentHum[10];
//     dtostrf(readHum(), 9, 1, currentHum);

//     // Send packet temp
//     char tempDataBuffer[40];
//     sprintf(tempDataBuffer, "%s,%s,%s", "float", "main_temp", currentTemp);
//     endpoint->sendPacket("access", "update_state", tempDataBuffer);

//     // Send packet temp
//     char humDataBuffer[40];
//     sprintf(humDataBuffer, "%s,%s,%s", "float", "main_hum", currentHum);
//     endpoint->sendPacket("access", "update_state", humDataBuffer);
// }