<?php

class alert
{
    public static function recSuccess(string $_string)
    {
        if (empty($_SESSION["alert"]["success"])) {
            $_SESSION["alert"]["success"] = array();
        }
        array_push($_SESSION["alert"]["success"], $_string);
    }

    public static function recWarning(string $_string)
    {
        if (empty($_SESSION["alert"]["warning"])) {
            $_SESSION["alert"]["warning"] = array();
        }
        array_push($_SESSION["alert"]["warning"], $_string);
    }

    public static function recError(string $_string)
    {
        if (empty($_SESSION["alert"]["error"])) {
            $_SESSION["alert"]["error"] = array();
        }
        array_push($_SESSION["alert"]["error"], $_string);
    }

    public static function recTab(string $_string)
    {
        $_SESSION["alert"]["tab"] = $_string;
    }

    public static function getSuccess()
    {
        if (!empty($_SESSION["alert"]["success"])) {
            return $_SESSION["alert"]["success"];
        } else {
            return FALSE;
        }
    }

    public static function getWarning()
    {
        if (!empty($_SESSION["alert"]["warning"])) {
            return $_SESSION["alert"]["warning"];
        } else {
            return FALSE;
        }
    }

    public static function getError()
    {
        if (!empty($_SESSION["alert"]["error"])) {
            return $_SESSION["alert"]["error"];
        } else {
            return FALSE;
        }
    }

    public static function getTab()
    {
        return (isset($_SESSION["alert"]["tab"])) ? $_SESSION["alert"]["tab"] : FALSE;
    }

    public static function ifSuccess()
    {
        return (empty($_SESSION["alert"]["success"])) ? FALSE : TRUE;
    }

    public static function ifError()
    {
        return (empty($_SESSION["alert"]["error"])) ? FALSE : TRUE;
    }

    public static function ifTab()
    {
        return (empty($_SESSION["alert"]["tab"])) ? FALSE : TRUE;
    }

    public static function printAlert(?array $_array = NULL)
    {
        if($_array == NULL){
            return NULL;
        }
        $return = NULL;
        foreach ($_array as $value) {
            $return .= "<div>" . $value . "</div>";
        }
        return $return;
    }

    public static function recErrorInput(string $_string)
    {
        if (empty($_SESSION["alert"]["input"])) {
            $_SESSION["alert"]["input"] = array();
        }
        array_push($_SESSION["alert"]["input"], $_string);
    }

    public static function ifErrorInput(string $_string)
    {
        return (in_array($_string, $_SESSION["alert"]["input"])) ? TRUE : FALSE;
    }

    private static function printToast(string $_idAlert, string $_style, string $_icon, ?array $_texte = NULL)
    {
        $text = ($_texte != NULL) ? self::printAlert($_texte) : NULL;
        
        echo '  <div  id="' . $_idAlert . '" class="toast hide" role="alert" aria-live="assertive" aria-atomic="true" data-delay="3000" style="margin-top:10px; ' . $_style . '">
                    <div class="toast-header">' . $_icon . '</div><div class="toast-body" id="' . $_idAlert . 'Txt">' . $text . '</div>
                </div>';
    }

    public static function show()
    {
        historique::recordLogs();

        echo '<div class="position-fixed bottom-0 right-0 p-3" style="z-index: 5; right: 5px; bottom: 0;">'; 

        $idAlert = "printToastSuccess";
        $texte1 = (self::getSuccess()) ?  self::getSuccess() : NULL;
        $style = 'background:#d4edda;';
        $icon = icon::getFont(["type" => "info", "color" => "green", "size" => "18px"]) . ' <strong style="color:green; margin-left:5px;" class="mr-auto">Succès</strong>';
        self::printToast($idAlert, $style, $icon, $texte1);

        $idAlert = "printToastWarning";
        $texte2 = (self::getWarning()) ?  self::getWarning() : NULL;
        $style = 'background:#fff3cd;';
        $icon = icon::getFont(["type" => "warning", "color" => "orange", "size" => "18px"]) . ' <strong style="color:orange; margin-left:5px;" class="mr-auto">Attention</strong>';
        self::printToast($idAlert, $style, $icon, $texte2);

        $idAlert = "printToastError";
        $texte3 = (self::getError()) ?  self::getError() : NULL;
        $style = 'background:#f8d7da;';
        $icon = icon::getFont(["type" => "alert", "color" => "red", "size" => "18px"]) . ' <strong style="color:red; margin-left:5px;" class="mr-auto">Erreur</strong>';
        self::printToast($idAlert, $style, $icon, $texte3);

        echo '</div>';

        echo ($texte1 == NULL) ?  '' : '<script>$("#printToastSuccess").toast("show");</script>';
        echo ($texte2 == NULL) ?  '' : '<script>$("#printToastWarning").toast("show");</script>';
        echo ($texte3 == NULL) ?  '' : '<script>$("#printToastError").toast("show");</script>';

        self::destroyAlert();
    }

    public static function destroyAlert()
    {
        unset($_SESSION["alert"]);
    }
}
