/*
  __  __    _  _____ ____  _____  __
 |  \/  |  / \|_   _|  _ \|_ _\ \/ /
 | |\/| | / _ \ | | | |_) || | \  / 
 | |  | |/ ___ \| | |  _ < | | /  \ 
 |_|  |_/_/   \_\_| |_| \_\___/_/\_\
                                    
Script to launch the welcome screen


Pour compiler:
g++ -Iinclude  -Llib welcome_screen.cc -o welcome_screen -lrgbmatrix
g++ -I /var/www/moduleair_pro_4g/matrix/include -L /var/www/moduleair_pro_4g/matrix/lib /var/www/moduleair_pro_4g/matrix/welcome_screen.cc -o /var/www/moduleair_pro_4g/matrix/welcome_screen -lrgbmatrix


Pour lancer:
sudo /var/www/moduleair_pro_4g/matrix/welcomeScreen/welcome_screen

*/

#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
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
}


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);

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

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