<?php
namespace Eadesigndev\FanCourier\Helper;

/**
 * @category   FanCourier
 * @package    FanCourier_Customshipping
 * @author     programare@fancourier.ro
 * @website    https://www.fancourier.ro
 */

use Eadesigndev\FanCourier\Model\GenerateAwb;
use Fancourier\Fancourier;
use Magento\Framework\App\Config\ScopeConfigInterface;

#[\AllowDynamicProperties]
class Data extends \Magento\Framework\App\Helper\AbstractHelper
{

    const XML_PATH_ENABLED = 'fancourier_customshipping/general/enabled';
    const XML_PATH_DEBUG   = 'fancourier_customshipping/general/debug';
    const PRINT_PAGE_SIZE  = 'A4';
    /**
     * @var \Psr\Log\LoggerInterface
     */
    protected $_logger;

    /**
     * @var \Magento\Framework\Module\ModuleListInterface
     */
    protected $_moduleList;

    /**
     * @param \Magento\Framework\App\Helper\Context $context
     * @param \Magento\Framework\Module\ModuleListInterface $moduleList
     */
    public function __construct(
        \Magento\Framework\App\Helper\Context $context,
        \Magento\Framework\Module\ModuleListInterface $moduleList,
        ScopeConfigInterface $scopeConfig
    ) {
        $this->_logger                  = $context->getLogger();
        $this->_moduleList              = $moduleList;
        $this->scopeConfig              = $scopeConfig;

        parent::__construct($context);
    }

    /**
     * Check if enabled
     *
     * @return string|null
     */
    public function isEnabled()
    {
        return $this->scopeConfig->getValue(
            self::XML_PATH_ENABLED,
            \Magento\Store\Model\ScopeInterface::SCOPE_STORE
        );
    }

    public function getDebugStatus()
    {
        return $this->scopeConfig->getValue(
            self::XML_PATH_DEBUG,
            \Magento\Store\Model\ScopeInterface::SCOPE_STORE
        );
    }

    public function getExtensionVersion()
    {
        $moduleCode = 'FanCourier_Customshipping';
        $moduleInfo = $this->_moduleList->getOne($moduleCode);
        return $moduleInfo['setup_version'];
    }

    public function getAwbDocument($identifier, $pickupId = null)
    {
        $username = $this->scopeConfig->getValue(GenerateAwb::CONFIG_KEY_USERNAME);
        $password = $this->scopeConfig->getValue(GenerateAwb::CONFIG_KEY_PASSWORD);
        $fan = new Fancourier($pickupId, $username, $password);

        $request = new \Fancourier\Request\PrintAwb();
        $request->setAwb($identifier);
        $request->setPageSize(self::PRINT_PAGE_SIZE);
        $response = $fan->printAwb($request);
        if ($response->isOk()) {
            return $response->getBody();
        }

        return "";
    }

    /**
     *
     * @param $message
     * @param bool|false $useSeparator
     */
    public function log($message, $useSeparator = false)
    {
        if ($this->getDebugStatus()) {
            if ($useSeparator) {
                $this->_logger->addDebug(str_repeat('=', 100));
            }

            $this->_logger->addDebug($message);
        }
    }
}