<?php

namespace App\Helpers;

use LZCompressor\LZString;

date_default_timezone_set('UTC');


class BPJSHelper
{
    public static function signature($cusid, $secid)
    {
        $timestamps = strval(time() - strtotime('1970-01-01 00:00:00'));
        $ensig = hash_hmac('sha256', $cusid . "&" . $timestamps, $secid, true);
        $signature = base64_encode($ensig);
        return $signature;
    }

    public static function timestamp()
    {
        $timestamp = strval(time() - strtotime('1970-01-01 00:00:00'));
        return $timestamp;
    }

    public static function enkripsi($passid)
    {
        return MD5($passid);
    }

    public static function stringDecrypt($key, $string)
    {
        $encrtyp_method = 'AES-256-CBC';

        $key_hash = hex2bin(hash('sha256', $key));

        $iv = substr(hex2bin(hash('sha256', $key)), 0, 16);

        $output = openssl_decrypt(base64_decode($string), $encrtyp_method, $key_hash, OPENSSL_RAW_DATA, $iv);

        return $output;
    }

    public static function bpjsTimestamp()
    {
        return strval(time() - strtotime('1970-01-01 00:00:00'));
    }

    public static function keyString($conId, $secId)
    {
        return $conId . $secId . self::bpjsTimestamp();
    }

    public static function keyHash($key)
    {
        return hex2bin(hash('sha256', $key));
    }

    public static function ivDecrypt($key)
    {
        return substr(hex2bin(hash('sha256', $key)), 0, 16);
    }

    public static function decompress($string)
    {
        return LZString::decompressFromEncodedURIComponent($string);
    }

    public static function responseBpjsV2($dataJson, $key)
    {
        $result = json_decode($dataJson);
        if ($result->metadata->code == "200" || $result->metadata->code == "1" && is_string($result->response)) {
            return self::doDecompress($result, $key);
        }
        return json_encode($result);
    }

    protected static function doDecompress($jsonObject, $key)
    {
        if ($jsonObject->metadata->code == "1" || $jsonObject->metadata->code == "200") {
            return self::mappingResponse($jsonObject->metadata, $jsonObject->response, $key);
        }
        return json_encode($jsonObject);
    }

    // protected static function mappingResponse($metaData, $response, $key)
    // {
    //     return [
    //         $metaData,
    //         json_decode(self::decompress(self::stringDecrypt($key, $response)), true)
    //     ];
    // }
    protected static function mappingResponse($metaData, $response, $key)
    {
        $data = [
            "metadata" => $metaData,
            "response" => json_decode(self::decompress(self::stringDecrypt($key, $response)))
        ];
        return json_encode($data);
    }

    //----------------------------------------------------------------------------------//

    public static function responseBpjsV3($dataJson, $key)
    {
        $result = json_decode($dataJson);
        if ($result->metadata->code == "200" && is_string($result->metadata)) {
            return self::doDecompress3($result, $key);
        }
        return json_encode($result);
    }

    protected static function doDecompress3($jsonObject, $key)
    {
        if ($jsonObject->metadata->code == "200") {
            return self::mappingResponse($jsonObject->metadata, $jsonObject->metadata, $key);
        }
        return json_encode($jsonObject);
    }
}
