<?php

namespace App\Controllers\Panel\Exambro;

use App\Controllers\Panel\BC;
use CodeIgniter\API\ResponseTrait;
use Firebase\JWT\JWT;

class BlockedApps extends BC
{
    use ResponseTrait;
    private $blockedApps;
    public function __construct()
    {

        parent::__construct();

        $this->blockedApps = options('blocked_apps');
        try {
            $this->blockedApps = json_decode($this->blockedApps, true);
            //code...
        } catch (\Throwable $th) {
            //throw $th;
            $this->blockedApps = [];
        }
    }
    public function index()
    {
        $data['page_title'] = "Blokir Aplikasi";
        return view('Panel/Exambro/blockedApps', $data);
    }

    public function all()
    {
        if (!$this->request->isAJAX() && ENVIRONMENT != 'development') {
            exit('No direct script access allowed');
        }
        $status = $this->request->getVar('status');
        foreach ($this->blockedApps as $key => $value) {
            if (is_numeric($status) && $value['status'] != $status) {
                unset($this->blockedApps[$key]);
            }
        }
        return $this->respond(['data' => $this->blockedApps]);
    }

    public function detail($package_name)
    {
        if (!$this->request->isAJAX() && ENVIRONMENT != 'development') {
            exit('No direct script access allowed');
        }
        $data = null;
        foreach ($this->blockedApps as $key => $value) {
            if ($value['package_name'] == $package_name) {
                $data = $value;
                break;
            }
        }
        if (!$data) return $this->failNotFound("Aplikasi tidak ditemukan");
        return $this->respond($data);
    }

    public function add()
    {
        if (!$this->request->isAJAX() && ENVIRONMENT != 'development') {
            exit('No direct script access allowed');
        }
        $package_name = $this->request->getVar('package_name');
        foreach ($this->blockedApps as $key => $value) {
            if ($value['package_name'] == $package_name) {
                return $this->fail("Aplikasi dengan package name \"$package_name\" sudah ada");
            }
        }
        $data['name'] = $this->request->getVar('name');
        $data['package_name']  = $package_name;
        $data['status'] = $this->request->getVar('status');
        if (empty($data['name'])) return $this->fail("Nama Perangkat tidak boleh kosong");
        if (empty($data['package_name'])) return $this->fail("Package Name tidak boleh kosong");
        $this->blockedApps[] = $data;
        if (!save_options(json_encode($this->blockedApps), 'blocked_apps')) return $this->fail("Gagal Menambahkan Daftar Blokir");
        return $this->respond(['message' => "Berhasil menambah daftar blokir"]);
    }
    public function edit($old_package_name)
    {
        if (!$this->request->isAJAX() && ENVIRONMENT != 'development') {
            exit('No direct script access allowed');
        }
        $key = null;
        $data = null;
        $package_name = $this->request->getVar('package_name');
        foreach ($this->blockedApps as $k => $value) {
            if ($value['package_name'] == $old_package_name) {
                $data = $value;
                $key = $k;
                break;
            } elseif ($value['package_name'] == $package_name) {
                return $this->fail("Aplikasi dengan package name \"$package_name\" sudah ada");
            }
        }
        if (!$data) return $this->failNotFound("Aplikasi tidak ditemukan");
        $data['name'] = $this->request->getVar('name');
        $data['package_name']  = $package_name;
        $data['status'] = $this->request->getVar('status');
        if (empty($data['name'])) return $this->fail("Nama Perangkat tidak boleh kosong");
        if (empty($data['package_name'])) return $this->fail("Package Name tidak boleh kosong");
        $this->blockedApps[$key] = $data;
        if (!save_options(json_encode($this->blockedApps), 'blocked_apps')) return $this->fail("Gagal Mengubah Daftar Blokir");
        return $this->respond(['message' => "Berhasil Mengubah daftar blokir"]);
    }

    public function delete($package_name)
    {
        if (!$this->request->isAJAX() && ENVIRONMENT != 'development') {
            exit('No direct script access allowed');
        }
        $key = null;
        foreach ($this->blockedApps as $k => $value) {
            if ($value['package_name'] == $package_name) {
                $key = $k;
                break;
            }
        }
        if ($key === null) return $this->failNotFound("Aplikasi tidak ditemukan");
        unset($this->blockedApps[$key]);
        if (!save_options(json_encode($this->blockedApps), 'blocked_apps')) return $this->fail("Gagal Menghapus Daftar Blokir");
        return $this->respond(['message' => "Berhasil Menghapus daftar blokir"]);
    }
}
