<?php

namespace App\Controllers\Mobile\Ujian\Jadwal;

use App\Controllers\Mobile\BC;
use App\Models\Mobile\Ujian\Jadwal;
use App\Models\Mobile\Ujian\Ujian;
use CodeIgniter\API\ResponseTrait;

class Overview extends BC
{
    use ResponseTrait;
    private $jadwalModel;
    public function __construct()
    {
        $this->jadwalModel = new Jadwal();
    }
    public function index()
    {
        return view('Mobile/Ujian/Jadwal/overview', ['ft' => 'ujian']);
    }
    public function all()
    {
        if (!$this->request->isAJAX() && ENVIRONMENT != 'development') {
            exit('No direct script access allowed');
        }
        $opt['siswa_id'] = user()->id;
        $opt['status_jadwal'] = '1';
        $opt['tahun_active'] = '1';
        $opt['jwt'] = get_cookie(MOBILECN());
        if ($this->request->getVar('today') == '1') {
            $opt['nowDateTime'] = time();
        }
        return $this->respond($this->jadwalModel->overview($opt));
    }
    public function verify_token($id)
    {
        $ujianModel = new Ujian();
        $ujian = $this->jadwalModel->findId(base64_decode($id), ['withToken' => true]);
        if (!$ujian) return view('Utils/404', ['code' => 404, 'text' => 'Ujian Tidak Ditemukan']);
        if ($ujian['siswa_id'] != user()->id) return view('Utils/404', ['code' => 404, 'text' => 'Ujian Tidak Ditemukan']);
        //belum mulai
        if ($ujian['startDateTime'] > time()) return redirect()->to('ujian/jadwal');
        //sudah selesai
        if ($ujian['endDateTime'] < time()) return redirect()->to('ujian/jadwal');
        switch ($ujian['status']) {
            case '0':
                //belum ujian
                if ($ujian['use_token'] == 1) {
                    if ($this->request->getPost('token_ujian') != $ujian['token']) {
                        session()->setFlashdata('errMsg', 'Token Tidak Valid!');
                        return redirect()->to('ujian/jadwal/confirm/' . $ujian['ujian_id']);
                    }
                };
                if ($ujian['multi_login'] != 1 && $ujian['jwt'] != null && get_cookie(MOBILECN()) != $ujian['jwt']) {
                    session()->setFlashdata('errMsg', 'Ujian Dikunci Pada Perangkat Lain!');
                    return redirect()->to('ujian/jadwal/confirm/' . $ujian['ujian_id']);
                }
                $ujianModel->start(base64_decode($ujian['ujian_id']), $this->request->getIPAddress(), get_cookie(MOBILECN()));
                setLog("Memulai Ujian " . ($ujian['nama_mapel'] ?? ''));
                return redirect()->to('ujian/' . $ujian['ujian_id']);
            case '1':
                //sedang ujian
                return redirect()->to('ujian/' . $ujian['ujian_id']);
            case '2':
                //selesai ujian
                return redirect()->to('ujian/jadwal');
        }
    }

    public function confirm($id)
    {
        $ujian = $this->jadwalModel->findId(base64_decode($id));
        if (!$ujian) return redirect()->to('ujian/jadwal');
        if ($ujian['siswa_id'] != user()->id) return redirect()->to('ujian/jadwal');
        //belum mulai
        if ($ujian['startDateTime'] > time()) return redirect()->to('ujian/jadwal');
        //sudah selesai
        if ($ujian['endDateTime'] < time()) return redirect()->to('ujian/jadwal');
        switch ($ujian['status']) {
            case '0':
                //belum ujian
                $builder = $this->jadwalModel->db->table('jadwal_ujian ju');
                $builder->select('u.fullname guru_pengampu,(SELECT COUNT(*) FROM draft_soal sds WHERE sds.draft_id=du.id) total_soal,mju.nama jenis_ujian');
                $builder->join('draft_ujian du', 'du.id=ju.draft_id');
                $builder->join('m_banksoal mbs', 'mbs.id=du.bank_soal_id');
                $builder->join('m_jenisujian mju', 'mju.id=ju.jenis_id');
                $builder->join('users u', 'u.id=mbs.user_id');
                $builder->where('ju.id', base64_decode($ujian['jadwal_id']));
                $data = $builder->get()->getRowArray();
                $data['nama_mapel'] = $ujian['nama_mapel'];
                $data['timeout'] = $ujian['timeout'];
                $data['use_token'] = $ujian['use_token'];
                $data['ft'] = 'ujian';
                return view('Mobile/Ujian/Jadwal/confirm', $data);
            case '1':
                //sedang ujian
                return redirect()->to('ujian/' . $ujian['ujian_id']);
            case '2':
                //selesai ujian
                return redirect()->to('ujian/jadwal');
        }
    }
}
