<?php

namespace App\Service\Bpjs;

use App\Helpers\BPJSHelper;
use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Exception\RequestException;

class Bridging extends Bpjs
{
    protected $headers;

    public function __construct($consid, $timestamp, $signature)
    {
        parent::__construct($consid, $timestamp, $signature);
        $urlencode = array('Content-Type' => 'Application/x-www-form-urlencoded');
        $this->headers = array_merge($this->header, $urlencode);
    }

    protected function keyDecrypt()
    {
        return config('bpjs.api.consid') . config('bpjs.api.seckey') . $this->header['x-timestamp'];
    }

    public function getRequest($endpoint)
    {
        try {
            $setkey = BPJSHelper::keyString(config('bpjs.api.consid'), config('bpjs.api.seckey'));
            $url = $this->bpjs_url . $endpoint;
            // dd($url);
            $response = $this->client->get($url, ['headers' => $this->header]);
            $result = BPJSHelper::responseBpjsV2($response->getBody()->getContents(), $this->keyDecrypt());
            // $result = $response->getBody()->getContents();
            return $result;
        } catch (RequestException $e) {
            $result = $e->getRequest();
            if ($e->hasResponse()) {
                $result = $e->getResponse();
            }
        }
    }

    public function postRequest($endpoint, $data)
    {
        // $data = file_get_contents("php:/input");
        try {
            $setkey = BPJSHelper::keyString(config('bpjs.api.consid'), config('bpjs.api.seckey'));
            $url = $this->bpjs_url . $endpoint;
            $response = $this->client->post($url, ['headers' => $this->header, 'body' => $data]);
            $result = BPJSHelper::responseBpjsV2($response->getBody()->getContents(), $setkey);
            return $result;
        } catch (RequestException $e) {
            $result = $e->getRequest();
            if ($e->hasResponse()) {
                $result = $e->getResponse();
            }
        }
    }

    public function putRequest($endpoint, $data)
    {
        try {
            $setkey = BPJSHelper::keystring(config('bpjs.api.consid'), config('bpjs.api.seckey'));
            $url = $this->bpjs_url . $endpoint;
            $response = $this->client->put($url, ['headers' => $this->header, 'body' => $data]);
            $result = BPJSHelper::responseBpjsV2($response->getBody()->getContents(), $setkey);
            return $result;
        } catch (RequestException $e) {
            $result = $e->getRequest();
            if ($e->hasResponse()) {
                $result = $e->getResponse();
            }
        }
    }

    public function deleteRequest($endpoint, $data)
    {
        try {
            $setkey = BPJSHelper::keyString(config('bpjs.api.consid'), config('bpjs.api.seckey'));
            $url = $this->bpjs_url . $endpoint;
            $response = $this->client->delete($url, ['headers' => $this->header, 'body' => $data]);
            $result = BPJSHelper::responseBpjsV2($response->getBody()->getContents(), $setkey);
            return $result;
        } catch (RequestException $e) {
            $result = $e->getRequest();
            if ($e->hasResponse()) {
                $result = $e->getResponse();
            }
        }
    }

    public function getRequest2($endpoint)
    {
        try {
            $setkey = BPJSHelper::keyString(config('bpjs.api.consid'), config('bpjs.api.seckey'));
            $url = $this->bpjs_url . $endpoint;
            // dd($url);
            $response = $this->client->get($url, ['headers' => $this->header]);
            $result = BPJSHelper::responseBpjsV3($response->getBody()->getContents(), $this->keyDecrypt());
            // $result = $response->getBody()->getContents();
            return $result;
        } catch (RequestException $e) {
            $result = $e->getRequest();
            if ($e->hasResponse()) {
                $result = $e->getResponse();
            }
        }
    }

    public function postRequest2($endpoint, $data)
    {
        // $data = file_get_contents("php:/input");
        try {
            $setkey = BPJSHelper::keyString(config('bpjs.api.consid'), config('bpjs.api.seckey'));
            $url = $this->bpjs_url . $endpoint;
            $response = $this->client->post($url, ['headers' => $this->header, 'body' => $data]);
            $result = BPJSHelper::responseBpjsV3($response->getBody()->getContents(), $setkey);
            return $result;
        } catch (RequestException $e) {
            $result = $e->getRequest();
            if ($e->hasResponse()) {
                $result = $e->getResponse();
            }
        }
    }
}
