<?php

class core
{
    public static function ifGet(string $_string)
    {
        if (isset($_GET[$_string])) {
            return TRUE;
        } else {
            return FALSE;
        }
    }

    public static function ifPost(string $_string)
    {
        if (isset($_POST[$_string])) {
            return TRUE;
        } else {
            return FALSE;
        }
    }

    public static function getGet(string $_string = NULL)
    {
        if ($_string == NULL) {
            return $_GET;
        } else {
            if (isset($_GET[$_string])) {
                return $_GET[$_string];
            } else {
                return NULL;
            }
        }
    }

    public static function getPost(string $_string = NULL)
    {
        if ($_string == NULL) {
            return $_POST;
        } else {
            if (isset($_POST[$_string])) {
                return $_POST[$_string];
            } else {
                return NULL;
            }
        }
    }

    public static function convertDate(string $datetime)
    {
        $pieces = explode(" ", $datetime);
        $pieces3 = explode(":", $pieces[1]);
        $pieces2 = explode("-", $pieces[0]);
        return $pieces2[2] . "/" . $pieces2[1] . "/" . $pieces2[0] . " à " . $pieces3[0] . ":" . $pieces3[1];
    }

    public static function dateFr(string $_timestampMysql = NULL)
    {
        if ($_timestampMysql == NULL) {
            $Now = new DateTime('now', new DateTimeZone(TIME_ZONE));
            return $Now->format("d/m/Y H:i:s");
        } else {
            return DateTime::createFromFormat("d/m/Y H:i:s", $_timestampMysql);
        }
    }

    public static function dateFromTimestamp(int $_timestamp = NULL)
    {
        if ($_timestamp == NULL) {
            return NULL;
        } else {
            return date("Y-m-d H:i:s", $_timestamp);
        }
    }

    public static function formatFileSize(float $_size, int $_decimalplaces = 0)
    {
        $sizes = array('O', 'Ko', 'Mo', 'Go', 'To');
        for ($i = 0; $_size > 1024 && $i < count($sizes) - 1; $i++) {
            $_size /= 1024;
        }
        return round($_size, $_decimalplaces) . ' ' . $sizes[$i];
    }

    public static function checkStringOnly(string $_string)
    {
        if (!ctype_alpha($_string)) {
            return TRUE;
        } else {
            return FALSE;
        }
    }

    public static function print_r(array $_array, int $_exit = NULL)
    {
        echo "<pre>";
        print_r($_array);
        echo "</pre>";
        ($_exit != NULL) ? exit() : NULL;
    }

    public static function elementMenu(string $_id, string $_href, string $_titre, string $_feather, string $_style = NULL)
    {
        ($_style != NULL) ? $_style = ' style="' . $_style . '"' : NULL;
        echo '<li class="nav-item">
                <a class="nav-link' . get::currentPage($_id) . '" aria-current="page"  href="' . $_href . '"' . $_style . '>
                    <span data-feather="' . $_feather . '"' . $_style . '></span>
                    ' . $_titre . '
                </a>
            </li>';
    }

    public static function elementMenuLink(string $_href, string $_titre, string $_feather, string $_style = NULL, string $_target = "_blank")
    {
        ($_style != NULL) ? $_style = ' style="' . $_style . '"' : NULL;
        echo '<li class="nav-item">
                <a class="nav-link" target="'. $_target .'" href="' . $_href . '"' . $_style . '>
                    <span data-feather="' . $_feather . '"' . $_style . '></span>
                    ' . $_titre . '
                </a>
            </li>';
    }

    public static function elementMenuH6(string $_titre, string $_style = NULL)
    {
        ($_style != NULL) ? $_style = ' style="' . $_style . '"' : NULL;
        echo '<h6 class="sidebar-heading d-flex justify-content-between align-items-center px-3 mt-4 mb-1 text-muted">
                    <span' . $_style . '>' . $_titre . '</span>
                </h6>';
    }

    public static function filAriane(array $_arbo)
    {

        $return = '<nav aria-label="breadcrumb bg-300">';
        $return .= '<ol class="breadcrumb" style="padding:5px 10px; border-bottom: 1px solid #e9ecef;">';

        foreach ($_arbo["arbo"] as $label => $lien) {
            if ($_arbo["current"] == $label) {
                $return .= '<li class="breadcrumb-item active" aria-current="page">' . $label . '</li>';
            } elseif ($lien == NULL) {
                $return .= '<li class="breadcrumb-item">' . $label . '</li>';
            } else {
                $return .= '<li class="breadcrumb-item"><a href="' . $lien . '" title="' . $label . '">' . $label . '</a></li>';
            }
        }
        $return .= '</ol>';
        $return .= '</nav>';
        return $return;
    }

    public static function caculPourcentage(?int $_nombre, ?int $_total, int $_pourcentage = 100)
    { 
        if ($_nombre == NULL) return 0;
        $resultat = ($_nombre/$_total) * $_pourcentage;
        return round($resultat); 
    }

    public static function encodeUTF8(string $_data)
    {
        return (mb_detect_encoding($_data) != "UTF-8") ? mb_convert_encoding($_data, 'UTF-8', mb_list_encodings()) : $_data;
    }

    public static function testConnexionInternet()
    {
        $hosts = ['1.1.1.1', '1.0.0.1', '8.8.8.8', '8.8.4.4'];
        foreach ($hosts as $host) {
            if ($connected = @fsockopen($host, 443)) {
                fclose($connected);
                return TRUE;
            }
        }
        return FALSE;
    }

    public static function printDateTxt()
    {
        $date = new IntlDateFormatter('fr_FR', IntlDateFormatter::LONG, IntlDateFormatter::NONE);
        return $date->format(time()) . " à " . date("H:m:s");
    }

    public static function addFileMaintenance(){
        $myfile = fopen(DOCUMENT_ROOT . FILE_MAINTENANCE, "w");
        fclose($myfile);
    }

    public static function removeFileMaintenance(){
        unlink(DOCUMENT_ROOT . FILE_MAINTENANCE);
    }

    public static function isMaintenance(){
        return (file_exists(DOCUMENT_ROOT . FILE_MAINTENANCE)) ? TRUE : FALSE;
    }

    public static function addFileDebug(){
        $myfile = fopen(DOCUMENT_ROOT . FILE_DEBUG, "w");
        fclose($myfile);
    }

    public static function removeFileDebug(){
        unlink(DOCUMENT_ROOT . FILE_DEBUG);
    }

    public static function isDebug(){
        return (file_exists(DOCUMENT_ROOT . FILE_DEBUG)) ? TRUE : FALSE;
    }

    public static function checkboxSelecter(bool $_val){
        echo ($_val == TRUE) ? "checked" : "";
    }

    public static function resetDatas()
    {
        db::query("TRUNCATE " . DB_T_TEMP_SALARIES);
        db::execute();
        json::delete("tmp_salaries");

        db::query("TRUNCATE " . DB_T_SALARIES);
        db::execute();
        json::delete("salaries");

        db::query("TRUNCATE " . DB_T_FILES);
        db::execute();
        file::cleanAllFiles(DIR_DATAS_FILES);

        db::query("TRUNCATE " . DB_T_EXCEL);
        db::execute();
        json::delete("excel");

        db::query("TRUNCATE " . DB_T_SALARIES_PROWEB);
        db::execute();
        json::delete("salaries-proweb");

        db::query("TRUNCATE " . DB_T_EVENTS_INSCRITS);
        db::execute();

        db::query("TRUNCATE " . DB_T_EVENTS);
        db::execute();

        json::delete("events");

        db::query("TRUNCATE " . DB_T_EXCEL_PROWEB);
        db::execute();
        json::delete("excel-proweb");
        file::cleanAllFiles(SFTP_LOCAL);
    }
}
