<?php



use Idearia\Logger;



class AccTransport extends BaseTransport {



    private $apiKey = false; 

    private $ipAddress = false; 

    private $transport = false; 



	public function __construct($apiKey, $ipAddress) {

        $this->apiKey = $apiKey;

		$this->ipAddress = $ipAddress;

        $this->transport = "AccTransport";

	}



    private function getGUID(){

        if (function_exists('com_create_guid')){

            return com_create_guid();

        } else {

            $charid = strtoupper(md5(uniqid(rand(), true)));

            $hyphen = chr(45);// "-"

            $uuid = chr(123)// "{"

                .substr($charid, 0, 8).$hyphen

                .substr($charid, 8, 4).$hyphen

                .substr($charid,12, 4).$hyphen

                .substr($charid,16, 4).$hyphen

                .substr($charid,20,12)

                .chr(125);// "}"

            return $uuid;

        }

    }



    private function curlRequest($method, $data) {



        try {



            $ch = curl_init();

            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);

            curl_setopt($ch, CURLOPT_URL, "https://api.accdistribution.net/v1/{$method}");

            curl_setopt($ch, CURLOPT_INTERFACE, $this->ipAddress);

            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

            curl_setopt($ch, CURLOPT_POST, true);

            curl_setopt($ch, CURLINFO_HEADER_OUT, true);

            curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data) );

            curl_setopt($ch, CURLOPT_HTTPHEADER, [

                "Content-type: application/json; charset=UTF-8",

                "X-ACV-RequestId: " . $this->getGUID(),

            ]);



            Logger::debug( "Transport {$this->transport}: Request {$method} DATA=" . $this->objToLog($data) );

            $result = curl_exec($ch); 

            curl_close($ch);



            if ( $result = $this->getResult($result) ) {

                Logger::debug( "Transport {$this->transport}: Request RESULT=" . $this->objToLog($result) );



                return $result;

            } else {

                Logger::debug( "Transport {$this->transport}: Request has no result" );



                return false;

            }

        } catch (Throwable $e) {

        

            $errorData = [

                "message" => $e->getMessage(),

                "class" => $e::class,

                "request" => $method,

                "data" => $data,

            ];

            Logger::error( "Transport {$this->transport}: Request ERROR=" . $this->objToLog($errorData) );

        

            return false;

        }

    }





	public function post($url, $data) {

        $requestData = [

            "Locale" => "lt",

            "CompanyId" => "_al",

            "Currency" => "EUR",

            "LicenseKey" => $this->apiKey,

            ...$data,

        ];



        return $this->simplePost(

            "https://api.accdistribution.net/v1/$url", 

            ["request" => $requestData], 

            $headers = ["X-ACV-RequestId: " . $this->getGUID()]

        );



		// return $this->curlRequest($url, ["request" => $requestData]);

	}



	protected function getResult($result) {

		return json_decode($result, 1);

	}



}

