<?php

class session
{
    public static function createSession(array $_array, string $_type = "user")
    {
        $_SESSION[$_type] = $_array;
    }

    public static function getId(string $_type = "user")
    {
        return $_SESSION[$_type]["id"];
    }

    public static function getValue(string $_val, string $_type = "user")
    {
        return $_SESSION[$_type][$_val];
    }

    public static function getName(string $_type = "user")
    {
        return $_SESSION[$_type]["prenom"] . " " . $_SESSION[$_type]["nom"];
    }

    public static function isConnect(string $_type = "user")
    {
        return (isset($_SESSION[$_type]["id"])) ? TRUE : FALSE;
    }

    public static function accessUserByType(int $_type)
    {

        if (isset($_SESSION["user"]["id"])) {
            if ($_SESSION["user"]["idType"] == 1) {
                return TRUE;
            } elseif ($_SESSION["user"]["idType"] == 2 and $_type == 2) {
                return TRUE;
            } elseif ($_SESSION["user"]["idType"] == 3 and $_type == 3) {
                return TRUE;
            } else {
                return FALSE;
            }
        } else {
            return FALSE;
        }
    }

    public static function accessElement(string $_element, string $_type)
    {
        // Eléments autorisé sans authentification
        if (self::elementWhite($_element, $_type)) {
            return TRUE;
        } else {
            if (isset($_SESSION["salarie"]["id"])) { // Espaces spécifiques aux Salariés
                if (self::elementSalaries($_element, $_type)) {
                    return TRUE;
                }
            } elseif (isset($_SESSION["user"]["idType"]) and $_SESSION["user"]["idType"] == 2) { // Espaces spécifiques aux Contrôleurs
                if (self::elementControleurs($_element, $_type)) {
                    return TRUE;
                }
            } elseif (isset($_SESSION["user"]["idType"]) and $_SESSION["user"]["idType"] == 3) { // Espaces spécifiques aux Contrôleurs
                if (self::elementServiceSocial($_element, $_type)) {
                    return TRUE;
                }
            } elseif (isset($_SESSION["user"]["idType"]) and $_SESSION["user"]["idType"] == 1) { // Espaces spécifiques aux Admins
                return TRUE;
            }
        }
        return FALSE;
    }

    private static function elementWhite(string $_element, string $_type)
    {
        return (in_array($_element, ACCESS_WHITE[$_type])) ? TRUE : FALSE;
    }

    private static function elementSalaries(string $_element, string $_type)
    {
        return (in_array($_element, ACCESS_SALARIES[$_type])) ? TRUE : FALSE;
    }

    private static function elementControleurs(string $_element, string $_type)
    {
        return (in_array($_element, ACCESS_CONTROLEURS[$_type])) ? TRUE : FALSE;
    }

    private static function elementServiceSocial(string $_element, string $_type)
    {
        return (in_array($_element, ACCESS_SOCIAL[$_type])) ? TRUE : FALSE;
    }
}
