<?php

declare(strict_types=1);

use PhpAmqpLib\Message\AMQPMessage;

require_once __DIR__ . '/../../../../vendor/autoload.php';
require_once 'Zend/Loader/Autoloader.php';
$loader = Zend_Loader_Autoloader::getInstance();

require_once __DIR__ . '/../RpcServerBase.php';
require_once __DIR__ . '/RpcClient.php';
require_once __DIR__ . '/../Curl.php';

class Agro_RabbitMQ_EnvioFormularioEcmPlugEssor_RpcServer extends Agro_Rabbitmq_RpcServerBase
{
    public $db;
    public $client = null;
    public $header = null;
    public $response = null;
    public $result = null;

    public function __construct(string $func, string $env, string $nomeFila)
    {
        parent::__construct($func, $env);
        $this->nomeFila = $nomeFila;

        $porta = (
            array_key_exists(
                'resources.db.params.port',$this->settings[$this->env]
            )?
            $this->settings[$this->env]['resources.db.params.port']
            :'5432'
        );
        $conexao = [
            'host' => $this->settings[$this->env]['resources.db.params.host'],
            'port' => $porta,
            'dbname' => $this->settings[$this->env]['resources.db.params.dbname'],
            'username' => $this->settings[$this->env]['resources.db.params.username'],
            'password' => $this->settings[$this->env]['resources.db.params.password'],
        ];

        $adapter = Zend_Db::factory('PDO_PGSQL', $conexao);
        Zend_Db_Table_Abstract::setDefaultAdapter($adapter);

        $this->db = Zend_Db_Table::getDefaultAdapter();
    }

    public function process(AMQPMessage $msg): void
    {
        echo "[SERVER] - process: begin\n";

        $payload = json_decode($msg->getBody(), true);
        
        $this->{$this->func}($payload);

        $this->channel->basic_ack($msg->get('delivery_tag'), 1);
        
        echo "[SERVER] - process: end\n";
    }

    public function run($params)
    {
        echo "[SERVER] - run: begin\n";
        try {
            $curl = new Agro_RabbitMQ_Curl();

            $curl->execute('/seguro/proposta/enviarformularioecmplugessor', $this->env, $params);
            $resultado = $curl->getResult();

            if (empty($resultado)) {
                throw new Exception('Erro ao enviar formulário para ECM Plug Essor.');
            }

            echo "[SERVER] - run: end\n";
            return '';
        } catch (\Throwable $th) {
            echo "[SERVER] - run: error\n";
            echo "[SERVER] - {$th->getMessage()}\n";
        }
    }

    public function execute()
    {
        $this->channel->queue_declare('envio_formulario_ecm_plug_essor', false, true, false, false);

        $this->channel->basic_qos(null, 1, false);
        $this->channel->basic_consume('envio_formulario_ecm_plug_essor', '', false, false, false, false, [$this, 'process']);

        try {
            $this->channel->consume();
        } catch (\Throwable $exception) {
            echo $exception->getMessage();
        }

        $this->channel->close();
        $this->connection->close();
    }

}
