<?php
/**
 * Mageplaza
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Mageplaza.com license that is
 * available through the world-wide-web at this URL:
 * https://www.mageplaza.com/LICENSE.txt
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade this extension to newer
 * version in the future.
 *
 * @category    Mageplaza
 * @package     Mageplaza_EditOrder
 * @copyright   Copyright (c) Mageplaza (https://www.mageplaza.com/)
 * @license     https://www.mageplaza.com/LICENSE.txt
 */

namespace Mageplaza\EditOrder\Block\Adminhtml\Order\Edit;

use Magento\Backend\Block\Template\Context;
use Magento\Backend\Model\Session\Quote;
use Magento\Framework\Pricing\PriceCurrencyInterface;
use Magento\Framework\UrlInterface;
use Magento\Sales\Api\Data\OrderInterface;
use Magento\Sales\Api\OrderRepositoryInterface;
use Magento\Sales\Block\Adminhtml\Order\Create\Shipping\Method\Form;
use Magento\Sales\Model\AdminOrder\Create;
use Magento\Tax\Helper\Data;
use Mageplaza\EditOrder\Helper\Data as HelperData;

/**
 * Class Tracking
 * @package Mageplaza\EditOrder\Block\Adminhtml\Order\Shipping
 */
class Tracking extends Form
{
    /**
     * @var OrderRepositoryInterface
     */
    protected $orderRepository;

    /**
     * @var UrlInterface
     */
    protected $urlBuilder;

    /**
     * @var HelperData
     */
    protected $_helperData;

    /**
     * ShippingMethod constructor.
     * @param Context $context
     * @param Quote $sessionQuote
     * @param Create $orderCreate
     * @param PriceCurrencyInterface $priceCurrency
     * @param Data $taxData
     * @param OrderRepositoryInterface $orderRepository
     * @param UrlInterface $urlBuilder
     * @param HelperData $_helperData
     * @param array $data
     */
    public function __construct(
        Context $context,
        Quote $sessionQuote,
        Create $orderCreate,
        PriceCurrencyInterface $priceCurrency,
        Data $taxData,
        OrderRepositoryInterface $orderRepository,
        UrlInterface $urlBuilder,
        HelperData $_helperData,
        array $data = []
    ) {
        $this->orderRepository = $orderRepository;
        $this->urlBuilder      = $urlBuilder;
        $this->_helperData     = $_helperData;

        parent::__construct($context, $sessionQuote, $orderCreate, $priceCurrency, $taxData, $data);
    }

    /**
     * @return string
     */
    public function getAddTrackingButtonUrl()
    {
        return $this->getUrl(
            'mpeditorder/shipping/track',
            [
                'order_id'    => $this->getOrder()->getId(),
                'mpeditorder' => true,
                'form_key'    => $this->getFormKey()
            ]
        );
    }

    /**
     * @return OrderInterface
     */
    public function getOrder()
    {
        $orderId = $this->getRequest()->getParam('order_id') ?: $this->getOrderId();

        return $this->orderRepository->get($orderId);
    }

    /**
     * @return string
     */
    public function getOrderIncrementId()
    {
        return $this->getOrder()->getIncrementId();
    }

    /**
     * @param int $orderId
     *
     * @return string
     */
    public function getAddTrackUrl($orderId)
    {
        return $this->urlBuilder->getUrl(
            'mpeditorder/shipment/addTrack/',
            [
                'order_id' => $orderId,
                'form_key' => $this->getFormKey()
            ]
        );
    }

    /**
     * @param null $storeId
     *
     * @return mixed
     */
    public function isEnabledTrackingShipment($storeId = null)
    {
        return $this->_helperData->isEnabled($storeId) && $this->_helperData->isEnabledTrackingShipment($storeId);
    }

    /**
     * @return bool
     */
    public function hasShipment()
    {
        $order     = $this->getOrder();
        $shipments = $order->getShipmentsCollection()->addFieldToFilter('shipment_status', ['null' => true]);

        return $shipments->getSize();
    }
}
