<?php

class WebServiceMktp
{
    private $registry;

    protected $API_DOMAIN = '';

    public $appId;
    public $appSecret;

    public $logml;
    public $loggp;
    public $logshopee;
    public $error = '';
    public $httpcode = '';

    public function __construct($registry)
    {
        $this->registry = $registry;
        $this->load = $registry->get('load');
        $this->language = $registry->get('language');
        $this->logml = new Log('errorMercadoLivre.txt');
        $this->loggp = new Log('errorGaleriaPage.txt');
        $this->logshopee = new Log('errorShopee.txt');
    }

    public function getPropery($dataobj, $dataname, $defaultvalue = '')
    {
        return (is_object($dataobj) && property_exists($dataobj, $dataname)) ? $dataobj->$dataname : $defaultvalue;
    }

    public function url_encode($url)
    {
        $reserved = array(
            ":" => '!%3A!ui',
            "/" => '!%2F!ui',
            "?" => '!%3F!ui',
            "#" => '!%23!ui',
            "[" => '!%5B!ui',
            "]" => '!%5D!ui',
            "@" => '!%40!ui',
            "!" => '!%21!ui',
            "$" => '!%24!ui',
            "&" => '!%26!ui',
            "'" => '!%27!ui',
            "(" => '!%28!ui',
            ")" => '!%29!ui',
            "*" => '!%2A!ui',
            "+" => '!%2B!ui',
            "," => '!%2C!ui',
            ";" => '!%3B!ui',
            "=" => '!%3D!ui',
            "%" => '!%25!ui',
        );

        $url = rawurlencode($url);
        $url = preg_replace(array_values($reserved), array_keys($reserved), $url);
        return $url;
    }

    public function __get($name)
    {
        return $this->registry->get($name);
    }

    public function TranslateError($error)
    {
        return $error;
    }

    public function getError(): string
    {
        return $this->error;
    }

    public function makeCurlFile($file)
    {
        $mime = mime_content_type($file);
        $info = pathinfo($file);
        $name = $info['basename'];
        $output = new CURLFile($file, $mime, $name);
        return $output;
    }

    public function callAPI($url, $method, $dataSend, $header, $file, &$httpcode, $dataType = Type_DadosSend::TpDados_Identificar, $returnFile = false)
    {

        $curl = curl_init();
        switch ($method) {
            case "POST":
                if ($dataType == Type_DadosSend::TpDados_Identificar) {
                    if (is_array($dataSend)) {
                        curl_setopt($curl, CURLOPT_POST, 1);
                        if ($file) {
                            curl_setopt($curl, CURLOPT_POSTFIELDS, $dataSend);
                        } else {
                            curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($dataSend));
                        }
                    } else {
                        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST');
                        curl_setopt($curl, CURLOPT_POSTFIELDS, $dataSend);
                    }
                } else {
                    if ($dataType == Type_DadosSend::TpDados_Json) {
                        curl_setopt($curl, CURLOPT_POST, 1);
                        curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($dataSend));
                    }

                }

                break;
            case "PUT":
                curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PUT');
                if (is_array($dataSend)) {
                    curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($dataSend));
                } else {
                    curl_setopt($curl, CURLOPT_POSTFIELDS, $dataSend);
                }
                break;
            case "DELETE":
                curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'DELETE');
                break;
            default:
                if ($dataSend)
                    $url = sprintf("%s?%s", $url, http_build_query($dataSend));
        }

        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($curl, CURLOPT_SSLVERSION, 4);
        curl_setopt($curl, CURLOPT_TIMEOUT_MS, 10000);//time out em segundos

        if (is_array($header) && !empty($header)) {
            curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
        }

        $this->error = '';
        $content = curl_exec($curl);
        $information = curl_getinfo($curl);

        $dataSend = json_decode($content);
        curl_setopt($curl, CURLOPT_HEADER, true);
        $httpcode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
        $this->httpcode = $httpcode;

        if ($returnFile){
          return $content;
        } else {
          return $dataSend;
        }
    }
}
