<?php
/**
 * @author Aitoc Team
 * @copyright Copyright (c) 2020 Aitoc (https://www.aitoc.com)
 * @package Aitoc_PreOrders
 */

/**
 * Copyright © 2018 Aitoc. All rights reserved.
 * Developed by Alex Yakimovich
 */

namespace Aitoc\PreOrders\Plugin\Sales\Block\Adminhtml\Items\Renderer;

use Aitoc\PreOrders\Helper\PreOrder as PreOrderHelper;
use Magento\Sales\Block\Adminhtml\Items\Renderer\DefaultRenderer as MagentoDefaultRenderer;
use Magento\Sales\Model\Order\Item as OrderItem;
use Magento\Sales\Model\Order\Shipment\Item as ShipmentItem;

/**
 * Class DefaultRenderer
 */
class DefaultRenderer
{
    /**
     * @var PreOrderHelper
     */
    private $preOrderHelper;

    /**
     * @param PreOrderHelper $preOrderHelper
     */
    public function __construct(PreOrderHelper $preOrderHelper)
    {
        $this->preOrderHelper = $preOrderHelper;
    }

    /**
     * @param MagentoDefaultRenderer $subject
     * @param OrderItem $item
     * @return OrderItem
     */
    /**
     * @param MagentoDefaultRenderer $subject
     * @param mixed $item
     * @return OrderItem
     */
    public function afterGetItem(
        MagentoDefaultRenderer $subject,
        $item
    ) {
        switch (true) {
            case ($item instanceof OrderItem):
                $orderItemId = $item->getItemId();
                break;
            default:
                $orderItemId = $item->getOrderItemId();
                break;
        }
        $item->setData('is_pre_order', $this->preOrderHelper->isOrderItemOnPreOrderState($orderItemId));
        $item->setData('is_can_be_shipped', $this->preOrderHelper->isOrderItemCanBeShipped($item, $orderItemId));
        return $item;
    }
}
