<?php
namespace Eadesigndev\DPD\Helper;
use Eadesigndev\DPD\Model\GenerateAwb;
use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\HTTP\ClientInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;

/**
 * @category   FanCourier
 * @package    FanCourier_Customshipping
 * @author     programare@fancourier.ro
 * @website    https://www.fancourier.ro
 */
#[\AllowDynamicProperties]
class Data extends AbstractHelper
{
    protected $_scopeConfig;
    protected $_client;


    public function __construct(
        ClientInterface $client,
        ScopeConfigInterface $scopeConfig
    )
    {
        $this->_scopeConfig = $scopeConfig;
        $this->_client = $client;
    }
    
    public function getAwbDocumentA6($identifier)
    {
        $data = [
            "userName" => $this->_scopeConfig->getValue(GenerateAwb::DPD_CONFIG_KEY_USERNAME),
            "password" => $this->_scopeConfig->getValue(GenerateAwb::DPD_CONFIG_KEY_PASSWORD),
            "paperSize" => "A6",
            "parcels" => [
                [
                    "parcel" => [
                        "id" => $identifier
                    ]
                ]
            ]
        ];

        $this->_client->addHeader("Content-Type", "application/json");
        $this->_client->post($this->_scopeConfig->getValue(GenerateAwb::DPD_CONFIG_KEY_URL) . "print", json_encode($data));
        $response = $this->_client->getBody();

        return $response;
    }
    
    public function getAwbDocument($identifier)
    {
        $data = [
            "userName" => $this->_scopeConfig->getValue(GenerateAwb::DPD_CONFIG_KEY_USERNAME),
            "password" => $this->_scopeConfig->getValue(GenerateAwb::DPD_CONFIG_KEY_PASSWORD),
            "paperSize" => "A4",
            "parcels" => [
                [
                    "parcel" => [
                        "id" => $identifier
                    ]
                ]
            ]
        ];

        $this->_client->addHeader("Content-Type", "application/json");
        $this->_client->post($this->_scopeConfig->getValue(GenerateAwb::DPD_CONFIG_KEY_URL) . "print", json_encode($data));
        $response = $this->_client->getBody();

        return $response;
    }
}
