<?php
$workdir = ["C:laragonwwwomsbots", "/var/www/html/totalenegie.com/telegram/cgi/"];
chdir($workdir[1]);     //PRODUCTION
// chdir($workdir[0]);     //DEV


require_once "class/Telegram.class.php";
require_once "class/Cgi.class.php";
require_once "class/constant.php";

$data_received = json_decode(file_get_contents("php://input"), true);

$chatId = $data_received["message"]["chat"]["id"];
$text = $data_received["message"]["text"];

if(strtoupper($text) == "/KILL" OR strtoupper($text) == "KILL"){
exec('rm -rf data/*', $output, $retval);die;
}

// Detect first auto message 
$step = ($text == "/start") ? "init" : Telegram::getStep($chatId);

switch ($step) {
    case "init":
        //
        $userData = Telegram::getUserData($chatId);
        $userData["bot_name"] = "TELEGRAM";
        Telegram::saveUserData($chatId, $userData);
        Telegram::saveStep($chatId, "init_response");
        Telegram::sendMessage($chatId, "Bienvenue sur CGI nVeuillez entrer le numero de la plaque du vehicule");
        break;

    case "init_response":

        $num_plaque = strtoupper($text);

        if (Verification::verifierEtConvertir($num_plaque)) {
            $num_plaque = Verification::verifierEtConvertir($num_plaque);
        };
        Telegram::sendMessage($chatId, "--- INFO VEHICULE {$num_plaque} ---");

        $response = Action::getVehiculInfo($num_plaque);

        if (isset($response["num_plaque"])) {
            $userData = Telegram::getUserData($chatId);
            $userData["num_plaque"] = $response["num_plaque"];
            Telegram::saveUserData($chatId, $userData);
            Telegram::saveStep($chatId, "recuperer_contraventions");
        }
        Telegram::sendMessage($chatId, $response["message"], $response["keyboard"], 'Markdown');

        break;

    case "recuperer_contraventions":

        $userData = Telegram::getUserData($chatId);
        $response = Action::getVehiculContraventions($userData["num_plaque"]);
        Telegram::sendMessage($chatId, $response["message"], $response["keyboard"], 'HTML');

        if (isset($response["contraventions"])) {
            $userData = Telegram::getUserData($chatId);
            $userData["contraventions"] = $response["contraventions"];
            Telegram::saveUserData($chatId, $userData);
            Telegram::saveStep($chatId, "reduire_contraventions");
        }

        break;

    case "reduire_contraventions":
        if ($text === "GENERER PAIEMENT") {

            $userData = Telegram::getUserData($chatId);
            $response = Action::genererContraventionsReduits($userData["contraventions"],0);

            if (isset($response["contraventions_final"])) {
                $userData = Telegram::getUserData($chatId);
                $userData["contraventions_final"] = $response["contraventions_final"];
                Telegram::saveUserData($chatId, $userData);

                $response = Action::genererPaiement($userData["contraventions_final"]);
                Telegram::sendMessage($chatId, $response["message"], $response["keyboard"], 'markdown');
            }
        } 
        elseif ($text === "LIQUIDER") {

            $userData = Telegram::getUserData($chatId);
            $response = Action::genererContraventionsReduits($userData["contraventions"],0);

            if (isset($response["contraventions_final"])) {

                $userData = Telegram::getUserData($chatId);
                $userData["contraventions_final"] = $response["contraventions_final"];
                Telegram::saveUserData($chatId, $userData);

                $response = Action::liquiderPaiement($userData["contraventions_final"]);
                Telegram::sendMessage($chatId, $response["message"], $response["keyboard"], 'HTML');
            }
        }
        else {
            Telegram::saveStep($chatId, "init");
            Telegram::sendMessage($chatId, "Desole, demande introuvable, cliquez sur <a>/start</a> pour commencer", null, 'HTML');
        }

        break;

    default:

        Telegram::saveStep($chatId, "init");
        Telegram::sendMessage($chatId, "Desole, demande introuvable, cliquez sur <a>/start</a> pour commencer", null, 'HTML');
        break;
}