<?php

class RistekdiktiClient extends BackgroundBaseClient
{
    var $token = "8bf37e8e-9fa6-3614-bb4f-0ff1e94fd583";

    public function filterResponse($body, $result)
    {
        parent::filterResponse($body, $result);
        if($this->logger != null) {
            if(!empty($result)) {
                if(array_key_exists('fault', $result)) {
                    if(array_key_exists('code', $result['fault'])) $this->logger->addItem("api_result_code", $result['fault']['code']);
                    if(array_key_exists('message', $result['fault'])) $this->logger->addItem("api_result_message", $result['fault']['message']);
                }
            }
        }
    }

    public function getJumlahMahasiswa($id_pt, $mulai_tahun, $akhir_tahun, $updated_after)
    {
        $method = "GET";
        $path = "/pddikti/1.0/rekapitulasi/jumlah-mahasiswa?kode-pt=" . $id_pt . "&mulai-semester=" . $mulai_tahun . "&sampai-semester=" . $akhir_tahun . "&updated-after=" . $updated_after;
        $params = [
            "headers" => [
                "Authorization" => "Bearer " . $this->token,
                "Content-type" => "application/json"
            ]
        ];

        //region LOG Definition
        $this->setApiName("ristekdikti-get_jumlah_mahasiswa");
        $this->setLogParams([
            'kode-pt' => $id_pt,
            'mulai-semester' => $mulai_tahun,
            'sampai-semester' => $akhir_tahun,
            'updated-after' => $updated_after,
        ]);
        //endregion

        $exec = $this->execute($method, $path, $params, false);
        return $exec;
    }

    public function getDosenJenjang($id_pt, $mulai_tahun, $akhir_tahun, $updated_after)
    {
        $method = "GET";
        $path = "/pddikti/1.0/rekapitulasi/dosen-jenjang?kode-pt=" . $id_pt . "&mulai-tahun=" . $mulai_tahun . "&sampai-tahun=" . $akhir_tahun . "&updated-after=" . $updated_after;
        $params = [
            "headers" => [
                "Authorization" => "Bearer " . $this->token,
                "Content-type" => "application/json"
            ]
        ];

        //region LOG Definition
        $this->setApiName("ristekdikti-get_dosen_jenjang");
        $this->setLogParams([
            'kode-pt' => $id_pt,
            'mulai-semester' => $mulai_tahun,
            'sampai-semester' => $akhir_tahun,
            'updated-after' => $updated_after,
        ]);
        //endregion

        $exec = $this->execute($method, $path, $params, false);
        return $exec;
    }

    public function getPtbyId($id_pt)
    {
        $method = "GET";
        $path = "/pddikti/1.0/pt/" . $id_pt;
        $params = [
            "headers" => [
                "Authorization" => "Bearer " . $this->token,
                "Content-type" => "application/json"
            ]
        ];

        //region LOG Definition
        $this->setApiName("ristekdikti-get_pt_by_id");
        $this->setLogParams([
            'id_pt' => $id_pt,
        ]);
        //endregion

        $exec = $this->execute($method, $path, $params, false);
        return $exec;
    }
    public function getPt()
    {
        $method = "GET";
        $path = "/pddikti/1.0/pt";
        $params = [
            "headers" => [
                "Authorization" => "Bearer " . $this->token,
                "Content-type" => "application/json"
            ]
        ];

        //region LOG Definition
        $this->setApiName("ristekdikti-get_pt");
        //endregion

        return $this->execute($method, $path, $params, false);
    }
    public function getProdibyPT($id_pt)
    {        
        $method = "GET";
        $path = "/pddikti/1.0/pt/" . $id_pt . "/prodi";
        $params = [
            "headers" => [
                "Authorization" => "Bearer " . $this->token,
                "Content-type" => "application/json"
            ]
        ];

        //region LOG Definition
        $this->setApiName("ristekdikti-get_prodi_by_pt");
        $this->setLogParams([
            "id_pt" => $id_pt
        ]);
        //endregion

        $exec = $this->execute($method, $path, $params, false);
        return $exec;
    }
    public function getDosenbyProdi($id_pt, $id_prodi)
    {
        $method = "GET";
        $path = "/pddikti/1.0/pt/" . $id_pt . "/prodi/" . $id_prodi . "/dosen";
        $params = [
            "query" => [
                "id-pt" => $id_pt,
                "id-prodi" => $id_prodi
            ],
            "headers" => [
                "Authorization" => "Bearer " . $this->token,
                "Content-type" => "application/json"
            ]
        ];

        //region LOG Definition
        $this->setApiName("ristekdikti-get_dosen_by_prodi");
        //endregion

        $exec = $this->execute($method, $path, $params, false);
        return $exec;
    }
    public function getPtbyName($name)
    {
        $method = "GET";
        $path = "/pddikti/1.0/pt?q=" . $name;
        $params = [
            "headers" => [
                "Authorization" => "Bearer " . $this->token,
                "Content-type" => "application/json"
            ]
        ];

        //region LOG Definition
        $this->setApiName("ristekdikti-get_pt_by_name");
        $this->setLogParams([
            'q' => $name
        ]);
        //endregion

        $exec = $this->execute($method, $path, $params, false);
        return $exec;
    }
}
