<?php
/* Biblioteca de integração com a API do Ecommerce */

class EcommerceAPI extends RestApi
{
    const userAuth = 'FUTURA_ECOMMERCE';
    const passAuth = '8!@*@EM42t%#bzfPB$7A4psdDku!zQJW';

    const urlEcommerceApiSandbox = 'https://apihomologacao.efutura.futurasistemas.com.br';
    const urlEcommerceApi = 'https://api.efutura.futurasistemas.com.br';

    private $header;

    private $environment;

    public function __construct($registry)
    {
        $this->registry = $registry;
        $this->load = $registry->get('load');
        $this->config = $registry->get('config');
        $this->session = $registry->get('session');
        $this->db = $registry->get('db');
        $this->log = $registry->get('log');
        $this->url = $registry->get('url');

        $this->setHeader();
        $this->setEnvironment();
    }

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

    public function setEnvironment()
    {
        $this->environment = ((defined('AMBIENTE_TESTE')) ? self::urlEcommerceApiSandbox : self::urlEcommerceApi);
    }

    public function getEnvironment(){
        return $this->environment;
    }

    public function setHeader(){
        $basicAuth = base64_encode(self::userAuth . ':' . self::passAuth);

        $this->header = [
            "Accept: application/json",
            "Content-Type: application/json",
            "Authorization: Basic " . $basicAuth,
        ];
    }

    public function getHeader(){
        return $this->header;
    }

    public function getMarketplaceQueue($type_marketplace, $shop_id, $type_entity){
        try {
            $apiPath = '/marketplace_queue';
            $metodo = 'GET';

            $mktName = strtolower(Type_MarketPlace::getNickFromValue($type_marketplace));

            $url = $this->environment . $apiPath
                . '?mkt=' . $mktName
                . '&shop_id=' . $shop_id
                . '&entity_type=' . $type_entity;

            $data = parent::callAPI($url, $metodo, false, $this->getHeader(), false, $retorno);

            if ($retorno == '200'){
                return $data;
            } else {
                $msgError = 'Erro ao buscar na fila do marketplace. HTTP Code: '.$retorno.'. Erro da API: ' . $data->message;
                $this->write->log($msgError);
                throw new exception($msgError);
            }
        } catch (Exception $e) {
            throw $e;
        }
    }

    public function deleteMarketplaceQueue($type_marketplace, $shop_id, $type_entity, $id_entity){
        try {
            $apiPath = '/marketplace_queue';
            $metodo = 'DELETE';

            $mktName = strtolower(Type_MarketPlace::getNickFromValue($type_marketplace));

            $url = $this->environment . $apiPath
                . '?mkt=' . $mktName
                . '&shop_id=' . $shop_id
                . '&entity_type=' . $type_entity
                . '&entity_id=' . $id_entity;

            $data = parent::callAPI($url, $metodo, false, $this->getHeader(), false, $retorno);

            if ($retorno == '200'){
                return $data;
            } else {
                $msgError = 'Erro ao remover na fila do marketplace. HTTP Code: '.$retorno.'. Erro da API: ' . $data->message;
                $this->write->log($msgError);
                throw new exception($msgError);
            }
        } catch (Exception $e) {
            throw $e;
        }
    }

}
