<?php

error_reporting(E_ALL & ~E_DEPRECATED);
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
require_once $_SERVER['DOCUMENT_ROOT'] . '/config.php';
require_once 'helperFunctions/RLE.php'; // include RLE function
require_once 'helperFunctions/RLEbinary.php'; // include RLE function
require_once 'helperFunctions/image_to_rgb565.php'; // OG RGB 565 Function
require_once 'helperFunctions/image_to_rgb565_json.php'; // JSON OUTPUT RGB 565
require_once 'helperFunctions/image_to_rgb565_json_RLE.php'; // RLE 565 Output function

// RLE Flag set in config
global $runRLE; // needs definition in every functions its used as global so the pointer works to the config file.
global $runRLEbinary;

$output_dir = SERVER_USERNAME . "public_html/GDN/App_Output/BIN/customFcn"; // Output directory
$fontFiles = SERVER_USERNAME . 'public_html/Scrolling/fonts/';
require SERVER_USERNAME . 'public_html/Scrolling/fonts/drawText.php'; // drawText()

// Generated app: NoCodeTest
// Created by: guest
// Created at: 2025-02-27 21:09:11
// Execution Interval: 5 minutes

// Initialize canvas
$width = 64; // Default canvas width
$height = 32; // Default canvas height
$image = imagecreatetruecolor($width, $height);
$black = imagecolorallocate($image, 0, 0, 0);
$white = imagecolorallocate($image, 255, 255, 255);
$red = imagecolorallocate($image, 255, 0, 0);
$green = imagecolorallocate($image, 0, 255, 0);
$blue = imagecolorallocate($image, 0, 0, 255);
$cyan = imagecolorallocate($image, 0, 255, 255);
imagefill($image, 0, 0, $black);

// User inputs

// Draw sample text
$bitmapFileName = $fontFiles . 'bitmap_6x8';
drawText($image, 'Hello LED Panel', $width/2, 5, 'center', $white, $bitmapFileName);
drawText($image, 'Generated with GLANCE', $width/2, 15, 'center', $cyan, $bitmapFileName);
drawText($image, 'No-Code Interface', $width/2, 25, 'center', $green, $bitmapFileName);

$output_file_path = $output_dir . '/NoCodeTest';

if(!$runRLEbinary){

    if($runRLE){
        $json_output = image_to_rgb565_json_RLE($image, '', '');
    } else {
        $json_output = image_to_rgb565_json($image);
    }
    file_put_contents($output_file_path . '.json', $json_output); // Make sure to use the full path
    
    // Echo out the JSON content
    // echo "New JSON Data:\n";
    echo json_encode(json_decode($json_output), JSON_PRETTY_PRINT);
    
} else {
    $image_data = image_to_rgb565($image);
    $bin_file_path = $output_file_path . '.bin'; // Use the provided directory path

    // Generate RLE Binary
    $filename = compressAndSaveImageData($image_data, $bin_file_path);
    if ($filename) {
        echo "File saved successfully: $bin_file_path";
    } else {
        echo "Failed to save file.";
    }
}
imagedestroy($image);

$db = null;

?>