<?php

use system\library\feedback\gupshop\UserOptGupshop;
use system\library\feedback\gupshop\MessageGupshopApi;

final class Gupshop extends GupshopUtils
{

    protected $source;
    protected $headers;

    public function __construct($registry)
    {
        parent::__construct($registry);

        $this->headers = array(
            'apikey: '.$this->getApikey(),
            'x-auth-token: '.$this->getAuthtoken(),
            'accept: application/json',
            'content-type: application/x-www-form-urlencoded'
        );

        $this->source = preg_replace('/([^0-9])/i', '', $this->config->get('gupshop_cellphone'));
        if ($this->config->get('gupshop_environment') == Type_Environment::Tp_Production) {
            $this->source = '55' . $this->source;
        }
    }

    public function sendMessages(MessageGupshopApi $data, $log = false)
    {

        $userOptGupshop = new UserOptGupshop();
        $userOptGupshop->setMethod('POST');
        $userOptGupshop->setTelefone($data->getDestination());
        $userOptGupshop->setApiName($this->config->get('gupshop_api_name'));
        $this->sendOpt($userOptGupshop);

        $dataSend = array(
            'channel' => $data->getChannel(),
            'source' => (int) $this->source,
            'destination' => (int) $data->getDestination(),
            'message.payload' => json_encode(get_object_vars($data->getMessage()))
        );

        if($this->config->get('gupshop_environment') == Type_Environment::Tp_Sandbox){
            $dataSend['src.name'] = $this->config->get('gupshop_api_name');
        } else {
            $dataSend['src.name'] = '' ;
        }

        $dataLog = array(
            'method'  => 'POST',
            'url'     => $this->getFeedbackUrl().'/gupshop/hsm',
            'enviado' => $dataSend
        );

        $retorno = $this->callAPI($dataLog['url'], $dataLog['method'], $dataSend, $this->headers);

        $this->saveLog($dataLog, $log, $retorno);

        return $retorno;

    }

    public function sendMessagesAutomatic(MessageGupshopApi $data, $log = false)
    {
        $this->headers = array_merge($this->headers, ['automatics: ' => true]);

        $this->sendMessages($data, $log);
    }

    public function saveLog($dataLog, $log, $retorno)
    {

        if($log && $log instanceof BaseLog){

            $log->baseLog->setUrl($dataLog['url']);
            $log->baseLog->setMetodo($dataLog['method']);
            $log->baseLog->setHttpCode($this->getHttpcode());
            $log->baseLog->setRetorno($retorno);
            $log->baseLog->setEnviado($dataLog['enviado']);
            $log->baseLog->setMd5($retorno);
            $log->baseLog->setDataHora($this->db->dataAtual());
            $log->saveBaseLog();

        }

    }

    public function getUrlCallback($data = array())
    {
        $headers = array(
            'apikey: '.$this->getApikey(),
            'x-auth-token: '.$data['authtoken'],
            'accept: application/json',
            'content-type: application/x-www-form-urlencoded'
        );
        $this->setEnvironment($data['environment']);
        $url = $this->getApiDomain().'/futura/callback/url/'.$data['idempresa'].'/'.$data['telefone'];

        return $this->callAPI($url, 'GET', [], $headers);
    }

    public function sendOpt(UserOptGupshop $userOptGupshop)
    {

        $dataSend = array(
            'telefone' => $userOptGupshop->getTelefone()
        );

        $url = $this->getFeedbackUrl().'/gupshop/user/'.$userOptGupshop->getApiName();
        $this->callAPI($url, $userOptGupshop->getMethod(), $dataSend, $this->headers);

    }

}