<?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)
    {
        $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 $_style, string $_icon, array $_texte)
    {
        echo '  <div class="toast hide" role="alert" aria-live="assertive" aria-atomic="true" data-delay="1500" style="margin-top:10px; ' . $_style . '">
                    <div class="toast-header">' . $_icon . '</div><div class="toast-body">' . self::printAlert($_texte) . '</div>
                </div>';
    }

    public static function show()
    {
        historique::recordLogs();
        
        if (self::getSuccess() or self::getWarning() or self::getError()) {

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

            if (self::getSuccess()) {
                $style = 'background:#d4edda;';
                $icon = '<span data-feather="info" style="color:green;"></span> <strong style="color:green; margin-left:5px;" class="mr-auto">Succès</strong>';
                $texte = self::getSuccess();
                self::printToast($style, $icon, $texte);
            }

            if (self::getWarning()) {
                $style = 'background:#fff3cd;';
                $icon = '<span data-feather="alert-circle" style="color:orange;"></span> <strong style="color:orange; margin-left:5px;" class="mr-auto">Attention</strong>';
                $texte = self::getWarning();
                self::printToast($style, $icon, $texte);
            }

            if (self::getError()) {
                $style = 'background:#f8d7da;';
                $icon = '<span data-feather="alert-triangle" style="color:red;"></span> <strong style="color:red; margin-left:5px;" class="mr-auto">Erreur</strong>';
                $texte = self::getError();
                self::printToast($style, $icon, $texte);
            }
            echo '</div>';
        }
        self::destroyAlert();
    }

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