<?php
/**
 * Copyright © 2016 Filoblu S.r.l. All rights reserved.
 */
namespace FiloBlu\Esb\Helper;
use \Magento\Framework\App\Helper\AbstractHelper;

/**
 * Generic Helper
 */
class Data extends AbstractHelper
{
    /**
     * @var \Magento\Framework\App\Config\ScopeConfigInterface
     */
    protected $_scopeConfig;

	/**
     * @var \Psr\Log\LoggerInterface $logger
     */
	protected $_logger;

	/**
     * @var \Magento\Sales\Api\OrderRepositoryInterface $orderRepository
     */
	protected $_orderRepository;

    /**
     * @var \Magento\Framework\Api\SearchCriteriaBuilder
     */
    protected $_searchCriteriaBuilder;

    /**
     * @var \Magento\Framework\Url
     */
    protected $_urlHelper;

    /**
     * Constructor
     *
     * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
     * @param \Psr\Log\LoggerInterface $logger
     */
	public function __construct(
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
        \Psr\Log\LoggerInterface $logger,
        \Magento\Sales\Model\OrderRepository $orderRepository,
        \Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder,
        \Magento\Framework\Url $urlHelper
	){
        $this->_scopeConfig = $scopeConfig;
        $this->_logger = $logger;
        $this->_searchCriteriaBuilder = $searchCriteriaBuilder;
        $this->_orderRepository = $orderRepository;
        $this->_urlHelper = $urlHelper;
	}

    /**
     * Format endpoint url by replacing base_url tokens.
     *
     * @param array $data
     * @return string
     */
    public function getEndpointUrl($url)
    {
        $endpoint_base_url=$this->_scopeConfig->getValue(
            'esb/endpoints/endpoint_base_url'
        );
        return preg_replace("/\{\{endpoint_base_url\}\}/", $endpoint_base_url, $url);
    }

    /**
     * Format endpoint url by replacing base_url tokens.
     *
     * @param \Magento\Sales\Api\Data\OrderInterface
     * @return boolean
     */
    public function isTestOrder(
        \Magento\Sales\Api\Data\OrderInterface $order
    )
    {
        if(preg_match("/.*filoblu.com\$/", $order->getCustomerEmail())){
            $this->_logger->addInfo("TEST ORDER FOUND: ".$order->getIncrementId().". Email: ".$order->getCustomerEmail());
            return true;
        }else{
            return false;
        }
    }

    /**
     * @param \Magento\Sales\Model\OrderRepository
     */
    public function loadOrderByOrderId($orderIncrementId)
    {
        $searchCriteria = $this->_searchCriteriaBuilder
            ->addFilter('increment_id', $orderIncrementId, 'eq')->create();
        $orderList = $this->_orderRepository->getList($searchCriteria);

        if(count($orderList)<>1){
            $message=__("Cannot find order %1", $orderIncrementId);
            $this->_logger->critical($message);
            throw new \Magento\Sales\Exception\CouldNotInvoiceException($message);
        }

        $order = $orderList->getFirstItem();

        return $order;

    }

    /**
     * @param object $order
     * @return string
     */
    public function getPrintUrl($document)
    {
        return $this->_urlHelper->getUrl('esb/invoice/print', ['document' => $document]);
    }
}
