<?php

class SSOKemdikbudClient extends BackgroundBaseClient
{
    const DUMMY_TOKEN = "5885ec00-9543-4405-83cf-3b25fec72d68-xx";
    public function checkToken($token)
    {
        if ($token == self::DUMMY_TOKEN) {
            return $this->checkDummyToken($token);
        } else {
            $this->setApiName("sso-kemdikbud-checkToken");

            return $this->executeWrapper(
                "/service/checktoken",
                $token
            );
        }
    }

    public function checkDummyToken($token)
    {
        return [
            "tokenStatus" => ($token == self::DUMMY_TOKEN ? 1 : 0),
        ];
    }

    public function getUsername($token)
    {
        if ($token == self::DUMMY_TOKEN) {
            return $this->getDummyUsername($token);
        } else {
            $this->setApiName("sso-kemdikbud-getUsername");

            return $this->executeWrapper(
                "/service/getusername",
                $token
            );
        }
    }

    public function getDummyUsername($token)
    {
        return $this->dummyUser($token);
    }

    public function expiredToken($token)
    {
        $this->setApiName("sso-kemdikbud-expiredToken");

        return $this->executeWrapper(
            "/service/expiredtoken",
            $token
        );
    }

    public function getRole($token)
    {
        $this->setApiName("sso-kemdikbud-getRole");

        return $this->executeWrapper(
            "/service/getrole",
            $token
        );
    }

    protected function executeWrapper($path, $tokenKey, $method = 'GET')
    {
        $params = [
            "query" => [
                "tokenKey" => $tokenKey
            ]
        ];
        return $this->execute($method, $path, $params);
    }

    protected function dummyUser($token)
    {
        $token = strtolower(trim($token));
        $data = [
            self::DUMMY_TOKEN => [
                "pengguna_id" => "7c4b8180-a38f-11e3-a800-9f8452680b21",
                "username" => "demo@mail.com",
                "nama" => "demo",
                "telepon" => null,
                "alamat" => null,
                "roleName" => "Pengguna",
                "tag" => 1,
                "nama_instansi" => "SMP NEGERI 119 MALUKU TENGAH",
                "kode_instansi" => "70040752",
                "kode_wilayah" => "210104",
                "NPSN" => "70040752",
                "wilayah_akses" => "210104",
                "nama_wilayah_akses" => "Kec. Tehoru",
                "jenis_instansi" => "Satuan Pendidikan",
                "jenis_instansi_id" => 5,
                "instansi_id" => "cc671642-cbf9-4f6b-9c67-377b5e1dbddf",
                "tag_instansi" => 5,
                "instansi_pengguna_id" => "62b3c40a-0e29-4c12-9d73-7a8378427820",
                "parent_instansi_id" => null,
                "jabatan_id" => 106,
                "id_level_wilayah" => 3,
                "foto" => null,
                "bentuk_pendidikan_id" => 6
            ]
        ];

        if (!array_key_exists($token, $data)) {
            throw new CustomErrorException(BaseResponse::INVALID_REQUEST, "Permintaan tidak valid!");
        }

        return $data[$token];
    }
}
