<?php
class TakolaClient
{
    private $client;
    public function __construct($baseUri)
    {
        $this->client = new GuzzleHttp\Client(['base_uri' => $baseUri]);
    }

    public function execute_api($method, $path, $params, $apiName)
    {
        $error_body = null;
        $error_message = null;

        try {
            $result = null;
            $body = $this->client->request($method, $path, ['query' => $params])->getBody();
            $result = json_decode($body, true);

            if (empty($result)) {
                $error_body = $body;
            }

        } catch (RequestException $e) {
                // 400 or 500 error
            $error_message = $e->getMessage();

            if ($e->hasResponse()) {
                $error_body = (string)$e->getResponse()->getBody();
                $result = json_decode($error_body);
            }
                // Connection error
            else {
                $result = [
                    'success' => false,
                    'message' => 'Connection Error- ' . $error_message
                ];
            }

            $api_params = http_build_query($params ?? []);
            LogHelper::apiLogError( $apiName, $api_params, $error_body);
        } catch (Exception $e) {
            $error_message = $e->getMessage();

            $result = [
                'success' => false,
                'message' => 'Unexpected Error- ' . $error_message
            ];

            $api_params = http_build_query($params ?? []);
            LogHelper::apiLogError( $apiName, $api_params, $error_body);
        }

        if (!empty($error_body) || !empty($error_message)) {
            $error_body = empty($error_body) ? '[NONE]' : $error_body;
            $error_message = empty($error_message) ? '[NONE]' : $error_message;

            throw new CustomErrorException(BaseResponse::TAKOLA_REQUEST_ERROR, "Takola Request Error " . $error_body . " " . $error_message);

        }

        return $result;
    }

    public function summary_provinsi($token)
    {
        return $this->execute_api(
            'GET',
            '/api/kpk/list_progress_verifikasi_nasional',
            [
                'token' => $token
            ],
            'TAKOLA-SUMMARY-PROVINSI'
        );
    }

    public function detail($token, $npsn)
    {
        return $this->execute_api(
            'GET',
            '/api/kpk/get_info_sekolah',
            [
                'token' => $token,
                'npsn' => $npsn
            ],
            'TAKOLA-DETAIL'
        );
    }

    public function photo_list($token, $npsn, $start, $limit, $type)
    {
        return $this->execute_api(
            'GET',
            '/api/kpk/list_foto_sekolah',
            [
                'token' => $token,
                'npsn' => $npsn,
                'limit' => $limit,
                'start' => $start,
                'type' => $type
            ],
            'TAKOLA-PHOTO-LIST'
        );
    }

    public function get_photo_path($token, $path)
    {
        return $this->client->getConfig('base_uri') . 'imageViewer?token=' . $token . '&image_path=' . $path;
    }
}
?>