<?php

class KemdikbudClient extends BackgroundBaseClient
{

    private function getExistingToken(): string
    {
        return "Bearer " . ApiToken::findTokenOrCreateOne(ApiToken::KEY_KEMDIKBUD_PDDIKTI, "6 hour", array($this, "getToken"));
    }

    public function getToken()
    {
        $method = "POST";
        $path = "/pddikti/" . $this->prop["api_version"] . "/oauth2/token";
        $params = [
            "headers" => [
                "Content-type" => "application/x-www-form-urlencoded"
            ],
            "form_params" => [
                "client_id" => $this->prop["client_id"],
                "client_secret" => $this->prop["client_secret"],
                "grant_type" => $this->prop["grant_type"]
            ],
            "connect_timeout" => 10,
            "timeout" => 10
        ];

        //region LOG Definition
        $this->setApiName("kemdikbud-get_token");
        $this->setLogParams($params['form_params']);
        //endregion

        $result = $this->execute($method, $path, $params, false);
        return $result['access_token'];
    }
    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/" . $this->prop["api_version"] . "/rekapitulasi/jumlah-mahasiswa?kode-pt=" . $id_pt . "&mulai-semester=" . $mulai_tahun . "1&sampai-semester=" . $akhir_tahun . "2&updated-after=" . $updated_after;
        $params = [
            "headers" => [
                "Authorization" => $this->prop['auth_token'],
                "Content-type" => "application/json"
            ],
            "connect_timeout" => 10,
            "timeout" => 10
        ];

        //region LOG Definition
        $this->setApiName("kemdikbud-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/" . $this->prop["api_version"] . "/rekapitulasi/dosen-jenjang?kode-pt=" . $id_pt . "&mulai-tahun=" . $mulai_tahun . "&sampai-tahun=" . $akhir_tahun . "&updated-after=" . $updated_after;
        $params = [
            "headers" => [
                "Authorization" => $this->prop['auth_token'],
                "Content-type" => "application/json"
            ],
            "connect_timeout" => 10,
            "timeout" => 10
        ];

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

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

    public function getPt($name, $limit = 10, $offset = 0)
    {
        $method = "GET";
        $path = "/pddikti/" . $this->prop["api_version"] . "/pt?q=" . $name . "&per-page=" . $limit . "&page=" . $offset;
        $params = [
            "headers" => [
                "Authorization" => $this->getExistingToken(),
                "Content-type" => "application/json"
            ],
            "connect_timeout" => 10,
            "timeout" => 10
        ];

        //region LOG Definition
        $this->setApiName("kemdikbud-get_pt");
        $this->setLogParams([
            'q' => $name,
            'per-page' => $limit,
            'page' => $offset
        ]);
        //endregion

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

    public function getPtbyName($name, $doCaching = false)
    {
        $method = "GET";
        $path = "/pddikti/" . $this->prop["api_version"] . "/pt?q=" . $name;
        $params = [
            "headers" => [
                "Authorization" => $this->getExistingToken(),
                "Content-type" => "application/json"
            ],
            "connect_timeout" => 10,
            "timeout" => 10
        ];

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

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

    public function getProdibyPT($id_pt, $doCaching = false)
    {
        $method = "GET";
        $path = "/pddikti/" . $this->prop["api_version"] . "/pt/" . $id_pt . "/prodi?per-page=1000";
        $params = [
            "headers" => [
                "Authorization" => $this->getExistingToken(),
                "Content-type" => "application/json"
            ],
            "connect_timeout" => 10,
            "timeout" => 10
        ];

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

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

    public function getAkreditasibyPT($id_pt)
    {
        $method = "GET";
        $path = "/pddikti/" . $this->prop["api_version"] . "/pt/" . $id_pt . "/akreditasi";
        $params = [
            "headers" => [
                "Authorization" => $this->getExistingToken(),
                "Content-type" => "application/json"
            ],
            "connect_timeout" => 10,
            "timeout" => 10
        ];

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

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

    public function getTrackingKIPK($kode_pt, $nim, $tahun)
    {
        $method = "GET";
        $path = "/api/" . $this->prop["api_version"] . "/tracking/kipk";
        $params = [
            "query" => [
                "nim" => $nim,
                "kode_pt" => $kode_pt
            ],
            "connect_timeout" => 10,
            "timeout" => 10
        ];

        if($tahun > 0)
        {
            $params['query']['tahun_beasiswa'] = $tahun;
        }

        //region LOG Definition
        $this->setApiName("kemdikbud-tracking-kipk");
        $this->setLogParams([
            "nim" => $nim,
            "kode_pt" => $kode_pt
        ]);
        //endregion

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

    public function doGet($method, $path, $params) {
        //region LOG Definition
        $this->setApiName("kemdikbud-doGet");
        $this->setLogParams($params);
        //endregion

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