<?php
/**
 * Webkul Software.
 *
 * @category  Webkul
 * @package   Webkul_MpPaypal
 * @author    Webkul
 * @copyright Copyright (c) Webkul Software Private Limited (https://webkul.com)
 * @license   https://store.webkul.com/license.html
 */
namespace Webkul\MpPaypal\Block\Adminhtml\Order\View;

/**
 * Order view tabs
 */
class Tab extends \Magento\Framework\View\Element\Text\ListText implements
    \Magento\Backend\Block\Widget\Tab\TabInterface
{
    /**
     * @var \Magento\Framework\AuthorizationInterface
     */
    protected $_authorization;

    /**
     * Core registry
     *
     * @var \Magento\Framework\Registry
     */
    protected $_coreRegistry = null;

    /**
     * @var \Webkul\MpPaypal\Model\ResourceModel\MpPaypal\CollectionFactory
     */
    private $paypalOrders;

    /**
     * @var \Webkul\Mpadaptivepayment\Helper\Data
     */
    private $helper;

    /**
     * dependency initialization
     *
     * @param \Magento\Framework\View\Element\Context $context
     * @param \Magento\Framework\AuthorizationInterface $authorization
     * @param \Magento\Framework\Registry $registry
     * @param \Webkul\MpPaypal\Model\ResourceModel\MpPaypal\CollectionFactory $paypalOrders
     * @param \Webkul\MpPaypal\Helper\Data $helper
     * @param array $data
     */
    public function __construct(
        \Magento\Framework\View\Element\Context $context,
        \Magento\Framework\AuthorizationInterface $authorization,
        \Magento\Framework\Registry $registry,
        \Webkul\MpPaypal\Model\ResourceModel\MpPaypal\CollectionFactory $paypalOrders,
        \Webkul\MpPaypal\Helper\Data $helper,
        array $data = []
    ) {
        $this->_authorization = $authorization;
        $this->_coreRegistry = $registry;
        $this->paypalOrders = $paypalOrders;
        $this->helper = $helper;
        parent::__construct($context, $data);
    }

    /**
     * Retrieve order model instance
     *
     * @return \Magento\Sales\Model\Order
     */
    public function getOrder()
    {
        return $this->_coreRegistry->registry('current_order');
    }

    /**
     * {@inheritdoc}
     */
    public function getTabLabel()
    {
        return __('Delayed Payment');
    }

    /**
     * {@inheritdoc}
     */
    public function getTabTitle()
    {
        return __('Delayed Payment');
    }

    /**
     * {@inheritdoc}
     */
    public function canShowTab()
    {
        try {
            $orderId = $this->getOrder()->getIncrementId();
            $ordercollection = $this->paypalOrders->create()
                ->addFieldToFilter('order_id', $orderId)
                ->addFieldToFilter('delayed_payment', 1)
                ->addFieldToFilter('payment_status', 0);
            if ($ordercollection->getSize()) {
                return true;
            }
            return false;
        } catch (\Exception $e) {
            $this->helper->logDataInLogger("Block_Adminhtml_Order_View_Tab canShowTab : ".$e->getMessage());
            return false;
        }
    }

    /**
     * {@inheritdoc}
     */
    public function isHidden()
    {
        return false;
    }

    /**
     * Render html output
     *
     * @return string
     */
    protected function _toHtml()
    {
        try {
            $this->setText('');
            $orderId = $this->getOrder()->getIncrementId();
            $ordercollection = $this->paypalOrders->create()
                ->addFieldToFilter('order_id', $orderId)
                ->addFieldToFilter('delayed_payment', 1)
                ->addFieldToFilter('payment_status', 0);
            if ($ordercollection->getSize()) {
                $block = $this->getLayout()->createBlock(
                    \Webkul\MpPaypal\Block\Adminhtml\Order\View\Tabs\Delayed::class
                );
                return $block->toHtml();
            }
        } catch (\Exception $e) {
            $this->helper->logDataInLogger("Block_Adminhtml_Order_View_Tab _toHtml() : ".$e->getMessage());
        }
        return parent::_toHtml();
    }
}
