<?php

class WebServiceFeedback
{
    private $apiDomain = '';
    private $registry;

    public $error = '';
    public $httpcode = '';

    public function __construct($registry)
    {
        $this->registry = $registry;
        $this->load = $registry->get('load');
        $this->language = $registry->get('language');
        $this->config = $registry->get('config');

        $this->setEnvironment($this->config->get('gupshop_environment'));

        if (defined('AMBIENTE_TESTE')) {
//            $this->apiDomain = 'http://127.0.0.1:8000/api';
        }
    }

    public function setEnvironment($enrironment)
    {
        if ($enrironment == Type_Environment::Tp_Production) {
            $this->apiDomain = $this->config->get('whatsapp_prod_domain');
        } else {
            $this->apiDomain = $this->config->get('whatsapp_dev_domain');
        }
    }

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

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

    public function getHttpcode()
    {
        return $this->httpcode;
    }

    public function setHttpcode($httpcode)
    {
        $this->httpcode = $httpcode;
    }

    public function getApiDomain()
    {
        return $this->apiDomain;
    }

    public function callAPI($url, $method = 'GET', $dataSend = false, $header = array(), $options = array())
    {

        $curl = curl_init();
        switch ($method) {
            case "POST":
                if (is_array($dataSend)) {
                    curl_setopt($curl, CURLOPT_POST, 1);
                    curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($dataSend));
                } else {
                    curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST');
                    curl_setopt($curl, CURLOPT_POSTFIELDS, $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_TIMEOUT_MS, 5000);//time out em segundos

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

        $this->error = false;
        $content = curl_exec($curl);
        //$teste = curl_errno($curl);
        //$error_message = curl_strerror($teste);
        $information = curl_getinfo($curl);

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

        return $dataSend;
    }
}
