/*
  __  __    _  _____ ____  _____  __
 |  \/  |  / \|_   _|  _ \|_ _\ \/ /
 | |\/| | / _ \ | | | |_) || | \  / 
 | |  | |/ ___ \| | |  _ < | | /  \ 
 |_|  |_/_/   \_\_| |_| \_\___/_/\_\


Script to launch screens to display compounds
Get values from 
NextPM ->  "/var/www/moduleair_pro_4g/matrix/input_NPM.txt"
MHZ16 ->      "/var/www/moduleair_pro_4g/matrix/input_MHZ16.txt"
Sensirion ->

sudo /var/www/moduleair_pro_4g/matrix/screen_sensors_loop

Pour compiler:
g++ -I/var/www/moduleair_pro_4g/matrix/include -L/var/www/moduleair_pro_4g/matrix/lib /var/www/moduleair_pro_4g/matrix/screenSensor/test_image.cc -o /var/www/moduleair_pro_4g/matrix/screenSensor/test_image.cc -lrgbmatrix

pour lancer:

sudo /var/www/moduleair_pro_4g/matrix/screenSensors/screen_sensors_loop

*/

#include "led-matrix.h"
#include "graphics.h"
#include <unistd.h>
#include <string.h>
#include <fstream>
#include <iostream>
#include <signal.h>
#include <atomic>

using rgb_matrix::RGBMatrix;
using rgb_matrix::Canvas;

std::atomic<bool> running(true);

void signal_handler(int signum) {
    running = false;
}


//message d'accueil (OLD)
void draw_welcome_screen(Canvas *canvas) {
    canvas->Clear();

    rgb_matrix::Color myCYAN(0, 255, 255);
    rgb_matrix::Color myWHITE(255, 255, 255);
    rgb_matrix::Color bg_color(0, 0, 0);

    rgb_matrix::Font font1;
    rgb_matrix::Font font2;

    if (!font1.LoadFont("/var/www/moduleair_pro_4g/matrix/fonts/6x9.bdf")) return;
    if (!font2.LoadFont("/var/www/moduleair_pro_4g/matrix/fonts/9x18B.bdf")) return;

    int letter_spacing = 0;
    int x = 10, y1 = 20, y2 = 50;

    rgb_matrix::DrawText(canvas, font2, x, y1, myCYAN, &bg_color, "Welcome to", letter_spacing);
    rgb_matrix::DrawText(canvas, font2, x, y2, myWHITE, &bg_color, "ModuleAir", letter_spacing);

    usleep(2000000); // Display the welcome screen for 2 seconds
}

//loop screen avec les polluants
void draw_screen(Canvas *canvas, const std::string &pm10, const std::string &pm25, const std::string &pm1, const std::string &co2) {
    canvas->Clear();

    rgb_matrix::Color myCYAN(0, 255, 255);
    rgb_matrix::Color myWHITE(255, 255, 255);

    rgb_matrix::Color myGREEN(0, 255, 0);       //vert pour BON   
    rgb_matrix::Color myYELLOW(255, 255, 0);    //jaune pour MOYEN   
    rgb_matrix::Color myORANGE(255, 165, 0);         // orange pour DEGRADE
    rgb_matrix::Color myRED(255, 0, 0);         // rouge pour MAUVAIS
 
    rgb_matrix::Color bg_color(0, 0, 0);

    rgb_matrix::Font font1;
    rgb_matrix::Font font2;

    if (!font1.LoadFont("/var/www/moduleair_pro_4g/matrix/fonts/6x9.bdf")) return;
    if (!font2.LoadFont("/var/www/moduleair_pro_4g/matrix/fonts/9x18B.bdf")) return;

    int letter_spacing = 0;
    int x, y1, y2, y3;
    // Convert string values to float for numeric comparisons
    float f_pm10 = std::stof(pm10);
    float f_pm25 = std::stof(pm25);
    float f_pm1 = std::stof(pm1);
    float f_co2 = std::stof(co2);

    // Draw PM10
    x = 0;
    y1 = font1.baseline() - 1;          //vertical pour la première ligne (PM10)
    y2 = y1 + font2.baseline() + 1;     //vertical pour la deuxime ligne (14.5 en gras)
    y3 = y1 + y2 + 4;                   //vertical pour la troisième ligne 
    
    rgb_matrix::DrawText(canvas, font2, x, y2, myWHITE, &bg_color, pm10.c_str(), letter_spacing);

       if (f_pm10 < 15) {
        rgb_matrix::DrawText(canvas, font1, x, y3, myGREEN, &bg_color, "BON", letter_spacing);
    } else if (f_pm10 >= 15 && f_pm10 < 30) {
        rgb_matrix::DrawText(canvas, font1, x, y3, myYELLOW, &bg_color, "MOYEN", letter_spacing);
    } else if (f_pm10 >= 30 && f_pm10 < 75) {
        rgb_matrix::DrawText(canvas, font1, x, y3, myORANGE, &bg_color, "DEGRADE", letter_spacing);
    } else if (f_pm10 >= 75) {
        rgb_matrix::DrawText(canvas, font1, x, y3, myRED, &bg_color, "MAUVAIS", letter_spacing);
    }
    rgb_matrix::DrawText(canvas, font1, x, y1, myCYAN, &bg_color, "PM10 µg/m³", letter_spacing);


    // Draw PM2.5
    x = 64;
    rgb_matrix::DrawText(canvas, font1, x, y1, myCYAN, &bg_color, "PM2.5 µg/m³", letter_spacing);
    rgb_matrix::DrawText(canvas, font2, x, y2, myWHITE, &bg_color, pm25.c_str(), letter_spacing);

  if (f_pm25 < 10) {
        rgb_matrix::DrawText(canvas, font1, x, y3, myGREEN, &bg_color, "BON", letter_spacing);
    } else if (f_pm25 >= 10 && f_pm25 < 20) {
        rgb_matrix::DrawText(canvas, font1, x, y3, myYELLOW, &bg_color, "MOYEN", letter_spacing);
    } else if (f_pm25 >= 20 && f_pm25 < 50) {
        rgb_matrix::DrawText(canvas, font1, x, y3, myORANGE, &bg_color, "DEGRADE", letter_spacing);
    } else if (f_pm25 >= 50) {
        rgb_matrix::DrawText(canvas, font1, x, y3, myRED, &bg_color, "MAUVAIS", letter_spacing);
    }

    // Draw PM1
    x = 0;
    y1 += 33;
    y2 += 33;
    y3 += 33;
    rgb_matrix::DrawText(canvas, font2, x, y2, myWHITE, &bg_color, pm1.c_str(), letter_spacing);
     
     if (f_pm1 < 10) {
        rgb_matrix::DrawText(canvas, font1, x, y3, myGREEN, &bg_color, "BON", letter_spacing);
    } else if (f_pm1 >= 10 && f_pm1 < 20) {
        rgb_matrix::DrawText(canvas, font1, x, y3, myYELLOW, &bg_color, "MOYEN", letter_spacing);
    } else if (f_pm1 >= 20 && f_pm1 < 50) {
        rgb_matrix::DrawText(canvas, font1, x, y3, myORANGE, &bg_color, "DEGRADE", letter_spacing);
    } else if (f_pm1 >= 50) {
        rgb_matrix::DrawText(canvas, font1, x, y3, myRED, &bg_color, "MAUVAIS", letter_spacing);
    }
    rgb_matrix::DrawText(canvas, font1, x, y1, myCYAN, &bg_color, "PM1 µg/m³", letter_spacing);

    // Draw CO2
    x = 64;
    rgb_matrix::DrawText(canvas, font2, x, y2, myWHITE, &bg_color, co2.c_str(), letter_spacing);
    if (f_co2 < 800) {
    rgb_matrix::DrawText(canvas, font1, x, y3, myGREEN, &bg_color, "BON", letter_spacing);
    } else if (f_co2 >= 800 && f_co2 < 1500) {
    rgb_matrix::DrawText(canvas, font1, x, y3, myORANGE, &bg_color, "AERER SVP", letter_spacing);
    } else if (f_co2 >= 1500) {
    rgb_matrix::DrawText(canvas, font1, x, y3, myRED, &bg_color, "AERER VITE", letter_spacing);
    } 
    rgb_matrix::DrawText(canvas, font1, x, y1, myCYAN, &bg_color, "CO₂ ppm", letter_spacing);

}

int main(int argc, char *argv[]) {
    // Handle signals for graceful exit
    signal(SIGINT, signal_handler);
    signal(SIGTERM, signal_handler);

    RGBMatrix::Options defaults;
    defaults.hardware_mapping = "moduleair_pinout";
    defaults.rows = 64;
    defaults.cols = 128;
    defaults.chain_length = 1;
    defaults.parallel = 1;
    defaults.row_address_type = 3;
    defaults.show_refresh_rate = true;
    defaults.brightness = 100;
    defaults.pwm_bits = 1;
    defaults.panel_type = "FM6126A";
    defaults.disable_hardware_pulsing = false;

    Canvas *canvas = RGBMatrix::CreateFromFlags(&argc, &argv, &defaults);
    if (canvas == NULL)
        return 1;

    // Display welcome screen once
    //draw_welcome_screen(canvas);

    // Initialize variables for main loop
    std::string input_file_NPM = "/var/www/moduleair_pro_4g/matrix/input_NPM.txt";
    std::string input_file_MHZ16 = "/var/www/moduleair_pro_4g/matrix/input_MHZ16.txt";
    std::string pm10 = "0", pm25 = "0", pm1 = "0", co2 = "0";

    //this is the loop
    while (running) {
       // Read data from NPM input file
        std::ifstream infile_npm(input_file_NPM);
        if (infile_npm) {
            infile_npm >> pm10 >> pm25 >> pm1;
        } else {
            std::cerr << "Error: Could not read " << input_file_NPM << std::endl;
        }

        // Read data from MH-Z16 input file
        std::ifstream infile_mhz16(input_file_MHZ16);
        if (infile_mhz16) {
            infile_mhz16 >> co2;
        } else {
            std::cerr << "Error: Could not read " << input_file_MHZ16 << std::endl;
        }

        // Update the display
        draw_screen(canvas, pm10, pm25, pm1, co2);

        // Sleep briefly to allow for updates
        usleep(10000000); // 10 s
    }

    // Clean up
    canvas->Clear();
    delete canvas;
    return 0;
}
