<?php

helper('app');

use App\Models\Panel\UserModel;
use Firebase\JWT\JWT;

if (!function_exists('setPassword')) {

    function setPassword($password)
    {

        $config = config('App');
        try {
            if (
                (defined('PASSWORD_ARGON2I') && $config->hashAlgorithm == PASSWORD_ARGON2I)
                ||
                (defined('PASSWORD_ARGON2ID') && $config->hashAlgorithm == PASSWORD_ARGON2ID)
            ) {
                $hashOptions = [
                    'memory_cost' => $config->hashMemoryCost,
                    'time_cost'   => $config->hashTimeCost,
                    'threads'     => $config->hashThreads
                ];
            } else {
                $hashOptions = [
                    'cost' => $config->hashCost
                ];
            }

            return password_hash(
                base64_encode(
                    hash('sha384', $password, true)
                ),
                $config->hashAlgorithm,
                $hashOptions
            );
        } catch (\Throwable $th) {
            return password_hash(
                base64_encode(
                    hash('sha384', $password, true)
                ),
                PASSWORD_DEFAULT,
                ['cost' => 10]
            );
        }
    }
}
if (!function_exists('verifPassword')) {

    function verifPassword($plain_password, $password_hash)
    {
        return
            password_verify(base64_encode(
                hash('sha384', $plain_password, true)
            ), $password_hash);
    }
}
if (!function_exists('PANELCN')) {
    function PANELCN()
    {

        $cn = options('PANELCN');
        if (get_cookie("rwno$cn")) $cn = "rwno$cn";
        return $cn;
    }
}
if (!function_exists('logged_in')) {
    /**
     * Checks to see if the user is logged in.
     *
     * @return bool
     */
    function logged_in()
    {
        if (isset($GLOBALS['panel_user'])) {
            return $GLOBALS['panel_user'];
        }
        helper('cookie');
        try {
            $cn = PANELCN();
            $c  = get_cookie($cn);
            $decoded = JWT::decode($c, options('PANELJWTKEY'), array('HS256'));
            $sessout = options('panel_session_timeout');
            $sessunit = options('panel_session_unit');
            if ($decoded->iat < strtotime("-$sessout $sessunit")) {
                setcookie($cn, '', time() - 3600, '/'); // empty value and old timestamp
                return false;
            }
            $user = (new UserModel())->findId($decoded->user_id);
            if (!$user) return false;
            $GLOBALS['panel_user'] = $user;
            return true;
        } catch (\Throwable $th) {
            return false;
        }
    }
}
if (!function_exists('dashboard_url')) {

    function dashboard_url()
    {
        switch (true) {
            case user()->hasRole('admin'):
                $rurl = base_url('panel/dashboard');
                break;
            case user()->hasRole('kepsek'):
                $rurl = base_url('panel/dashboard');
                break;
            case user()->hasRole('guru'):
                $rurl = base_url('panel/e-learning/jadwal-mengajar');
                break;
            case user()->hasRole('pengawas'):
                $rurl = base_url('panel/ujian/overview');
                break;
            case user()->hasRole('karyawan'):
                $rurl = base_url('panel/daily-presence/map');
                break;
            case user()->hasRole('piket'):
                $rurl = base_url('panel/e-learning/rekap/guru/absensi');
                break;
            default:
                $rurl = base_url();
                break;
        }
        return $rurl;
    }
}
if (!function_exists('user')) {
    /**
     * Get user data
     *
     * @return object
     */ 
    function user()
    {
        if (!logged_in()) return null;
        return $GLOBALS['panel_user'];
    }
}
