<?php
/**
 * Copyright © 2018 EaDesign by Eco Active S.R.L. All rights reserved.
 * See LICENSE for license details.
 */

namespace Eadesigndev\Urgent\Block\Adminhtml\Sales\Shipment;

use Eadesigndev\Awb\Model\Sources\CarrierType;
use Magento\Backend\Block\Widget\Container;
use Magento\Backend\Block\Widget\Context;
use Magento\Framework\Registry;

#[\AllowDynamicProperties]
class GenerateAwb extends Container
{
    /**
     * Core registry
     * @var Registry
     * @var CarrierType
     */
    private $coreRegistry = null;

    public $carrierType;

    /**
     * GenerateAwb constructor.
     * @param Context $context
     * @param Registry $registry
     * @param CarrierType $carrierType
     * @param array $data
     */
    public function __construct(
        Context $context,
        Registry $registry,
        CarrierType $carrierType,
        array $data = []
    ) {
        $this->coreRegistry = $registry;
        $this->carrierType = $carrierType;
        parent::__construct($context, $data);
    }

    public function _construct()
    {
        parent::_construct();
    }

    /**
     * @return string
     */
    public function getGenerateAwbUrl()
    {
        if (empty($this->getShipment())) {
            return false;
        }

        return $this->getUrl(
            'shipping_awb/index/editawb',
            [
                'shipment_id' => $this->getShipmentId(),
                'order_id' => $this->getOrderAwbId(),
                'carrier_id'  => CarrierType::URGENT_CURRIER
            ]
        );
    }

    /**
     * @return integer
     */
    public function getOrderAwbId()
    {
        if (empty($this->coreRegistry->registry('current_order'))) {
            $orderObject = $this->coreRegistry->registry('current_shipment')->getOrder();
        } else {
            $orderObject = $this->coreRegistry->registry('current_order');
        }

        $orderId = $orderObject->getId();

        return $orderId;
    }

    /**
     * @return integer
     */
    public function getShipmentId()
    {
        $shipmentCollection = $this->getShipment();
        if ($shipmentCollection instanceof \Magento\Sales\Model\Order\Shipment) {
            $shipmentId = $shipmentCollection->getId();
        } else {
            $shipmentId = $this->getFirstShipmentId($shipmentCollection);
        }

        return $shipmentId;
    }

    public function getShipment()
    {
        if (!empty($this->coreRegistry->registry('current_shipment'))) {
            return $this->coreRegistry->registry('current_shipment');
        } else {
            return $this->coreRegistry->registry('current_order')->getShipmentsCollection();
        }
    }

    public function getFirstShipmentId($shipmentCollection)
    {
        if (!empty($shipmentCollection)) {
            foreach ($shipmentCollection as $shipment) {
                $shipmentId =  $shipment->getId();
                return $shipmentId;
            }
        }
    }
}
