<?php
class NeoBosClient extends BackgroundBaseClient
{
    const JENIS_BOS = ['REGULER','AFIRMASI','KINERJA'];

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

                if(array_key_exists("status", $result)) {
                    if ($result['status'] == 1) {
                        $this->logger->addItem("api_result_code", $result['status']);
                        if(array_key_exists('message', $result)) $this->logger->addItem("api_result_message", $result['message']);
                    }
                }
            }
        }
    }

    public function getLaporanByNpsn($npsn, $tahun, $triwulan)
    {
        $method = "GET";
        $path = "/apiv1/service/npsn/detail";
        $params = [
            "query" => [
                "npsn" => $npsn,
                "tahun" => $tahun,
                "triwulan" => $triwulan
            ],
            "headers" => [
                "id" => $this->prop["id"],
                "token" => $this->prop["token"]
            ]
        ];

        //region LOG Definition
        $this->setApiName("neobos-get_laporan_by_npsn");
        //endregion

        try {
            return $this->execute($method, $path, $params, false, true);
        } catch (\Exception $e) {
            //throw new CustomErrorException(BaseResponse::GUZZLE_ERROR, 'Mohon maaf, akses data Dana BOS dari Kemdikbudristek sedang dalam perbaikan');
            return [];
        }
    }

    public function getSummaryNasionalPerJenjang($tahun)
    {
        $method = "GET";
        $path = "/apiv1/service/laporan/nasional";
        $params = [
            "query" => [
                "tahun" => $tahun
            ],
            "headers" => [
                "id" => $this->prop["id"],
                "token" => $this->prop["token"]
            ]
        ];

        //region LOG Definition
        $this->setApiName("neobos-get_summary_nasional_per_jenjang");
        //endregion

        try {
            return $this->execute($method, $path, $params, false);
        } catch (\Exception $e) {
            throw new CustomErrorException(BaseResponse::GUZZLE_ERROR, 'Mohon maaf, akses data Dana BOS dari Kemdikbudristek sedang dalam perbaikan');
        }
    }

    public function getYears() {
        $method = "GET";
        $path = "/apiv1/service/tahun";
        $params = [
            "headers" => [
                "id" => $this->prop["id"],
                "token" => $this->prop["token"]
            ]
        ];

        //region LOG Definition
        $this->setApiName("neobos-get_years");
        //endregion

        try {
            return $this->execute($method, $path, $params, false);
        } catch (\Exception $e) {
            throw new CustomErrorException(BaseResponse::GUZZLE_ERROR, 'Mohon maaf, akses data Dana BOS dari Kemdikbudristek sedang dalam perbaikan');
        }
    }

    public function getSekolahAll($tahun, $jenisSalur) {
        $method = "GET";
        $path = "/apiv1/sekolah/all";
        $params = [
            "query" => [
                "tahun" => $tahun,
                "jenis_salur" => $jenisSalur,
                "tahap" => 1 // jika sekolah mendapatkan alokasi pada tahap 1, maka pasti menerima pada tahap selanjutnya
            ],
            "headers" => [
                "id" => $this->prop["id"],
                "token" => $this->prop["token"]
            ]
        ];

        //region LOG Definition
        $this->setApiName("neobos-get_sekolah_all_tahun_".$tahun."_".$jenisSalur);
        //endregion

        try {
            return $this->execute($method, $path, $params, false);
        } catch (\Exception $e) {
            throw new CustomErrorException(BaseResponse::GUZZLE_ERROR, 'Mohon maaf, akses data Dana BOS dari Kemdikbudristek sedang dalam perbaikan');
        }
    }

    public function processGetSekolahAll($tahun, $jenisSalur) {
        $this->objectsProcessed = 0;
        try {
            $response = $this->getSekolahAll($tahun, $jenisSalur);
            if (array_key_exists('status', $response) && $response['status'] == 0 && array_key_exists('data', $response) && is_array($response['data'])) {
                $rows = $response['data'];
                foreach ($rows as $row) {
                    $npsn = $row['npsn'];
                    if (!PenerimaBos::isFoundByNpsnTahun($npsn, $tahun)) {
                        $new = new PenerimaBos();
                        $new->npsn = $npsn;
                        $new->tahun = $tahun;
                        $new->jenis_salur = $jenisSalur;
                        $new->save();
                        $this->objectsProcessed++;
                    }
                }
            }
        } catch(\Exception $e) {
            throw new CustomErrorException(BaseResponse::GUZZLE_ERROR, 'Mohon maaf, akses data Dana BOS dari Kemdikbudristek sedang dalam perbaikan');
        }
    }

    public function getLaporanKomponenNasional($tahun, $jenisBos, $jenisSalur = 'BOS')
    {
        $method = "GET";
        $path = "/apiv1/service/laporan_komponen_nasional/$tahun/$jenisBos/$jenisSalur";
        $params = [
            "headers" => [
                "id" => $this->prop["id"],
                "token" => $this->prop["token"]
            ]
        ];

        //region LOG Definition
        $this->setApiName("neobos-get_laporan_komponen_nasional");
        //endregion

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

    public function getTahun()
    {
        $method = "GET";
        $path = "/apiv1/service/tahun";
        $params = [
            "headers" => [
                "id" => $this->prop["id"],
                "token" => $this->prop["token"]
            ]
        ];

        //region LOG Definition
        $this->setApiName("neobos-get_tahun");
        //endregion

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