<?php

namespace App\Controllers\API\V3;

use CodeIgniter\RESTful\ResourceController;
use App\Models\Mobile\Ujian\Ujian;
use Firebase\JWT\JWT;

class ExamBro extends ResourceController
{

    private $ujianModel;
    public function __construct()
    {
        helper('app');
        $this->ujianModel = new Ujian();
    }

    public function getCredentials()
    {
        if (options('enable_app_scan_qr') != '1') return view('Utils/404', ['code' => 404, 'text' => 'Halaman Tidak Ditemukan!']);
        if (options('exambro_version') > $this->request->getGet('appVersion')) return $this->respond([
            'status' => 0,
            'message' => 'Versi Aplikasi Anda Sudah Tidak Didukung, Silahkan Update Aplikasi Anda!'
        ], 401);
        $payload = array(
            "iat" => time(),
            "nbf" => time(),
            "exp" => strtotime("+5 seconds"),
            "secretKey" => options('sck'),
            "pla" => options('passcode_locked_app'),
            'maxClose' => 0,
            'url' => base_url(),
            'locked' => false,
        );
        $jwt = JWT::encode($payload, "CANDYCBT");
        return $this->respond(['status' => 1, 'message' => 'Berhasil!', 'token' => $jwt], 200);
    }
    public function blockedApps()
    {
        $blockedApps = [];
        foreach (json_decode(options('blocked_apps'), true) as $key => $value) {
            if ($value['status'] != "1") continue;
            $blockedApps[] = $value['package_name'];
        }
        return $this->respond(['status' => 1, 'message' => 'Berhasil!', 'data' => ['blocked_apps' => $blockedApps]], 200);
    }

    public function unlockApp()
    {
        if (options('enable_app_scan_qr') != '1') return view('Utils/404', ['code' => 404, 'text' => 'Halaman Tidak Ditemukan!']);
        if (options('sck') != $this->request->getHeaderLine('secretKey')) return $this->failForbidden('Secret Key Tidak valid!');
        if (options('passcode_locked_app') != $this->request->getGet('passcode') || empty($this->request->getGet('passcode'))) return $this->respond([
            'status' => 0,
            'message' => 'Passcode Tidak Valid!'
        ], 200);
        return $this->respond(['status' => 1, 'message' => 'Berhasil!'], 200);
    }

    public function examConfig($ujianId)
    {
        if (options('sck') != $this->request->getHeaderLine('secretKey')) return $this->failForbidden('Secret Key Tidak valid!');

        $ujianId = base64_decode($ujianId);
        $ujian = $this->ujianModel->findId($ujianId, true);
        if (!$ujian) return $this->failNotFound();
        // $data['timezone'] = date_default_timezone_get();
        $data['nama_mapel'] = $ujian['nama_mapel'];
        $data['strict'] = $ujian['strict'] == 1;
        $data['afktimeout'] = $ujian['afktimeout'];
        $data['status'] = $ujian['status'];
        $data['timeout'] = $ujian['timeout'];
        $data['maxCheatCount'] = $ujian['maxCheatCount'];
        return $this->respond(['data' => $data]);
    }
    /**
     * Return an array of resource objects, themselves in array format
     *
     * @return mixed
     */
    public function index()
    {
        //
    }

    /**
     * Return the properties of a resource object
     *
     * @return mixed
     */
    public function show($id = null)
    {
        //
    }

    /**
     * Return a new resource object, with default properties
     *
     * @return mixed
     */
    public function new()
    {
        //
    }

    /**
     * Create a new resource object, from "posted" parameters
     *
     * @return mixed
     */
    public function create()
    {
        //
    }

    /**
     * Return the editable properties of a resource object
     *
     * @return mixed
     */
    public function edit($id = null)
    {
        //
    }

    /**
     * Add or update a model resource, from "posted" properties
     *
     * @return mixed
     */
    public function update($id = null)
    {
        //
    }

    /**
     * Delete the designated resource object from the model
     *
     * @return mixed
     */
    public function delete($id = null)
    {
        //
    }
}
