<?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\Tabs;

/**
 * Order view tabs
 */
class Delayed extends \Magento\Backend\Block\Template
{
    /**
     * @var string
     */
    protected $_template = 'Webkul_MpPayPal::delayedpayment.phtml';

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

    /**
     * @param \Magento\Backend\Block\Template\Context $context
     * @param \Magento\Framework\Registry             $coreRegistry
     * @param array                                   $data
     */
    public function __construct(
        \Magento\Backend\Block\Template\Context $context,
        \Webkul\MpPayPal\Model\MpPayPalFactory $paypalSalesOrder,
        \Webkul\MpPayPal\Model\PayPal\Adapter $adapter,
        \Magento\Sales\Api\OrderRepositoryInterface $orderRepository,
        \Magento\Framework\Registry $coreRegistry,
        \Magento\Framework\Session\SessionManagerInterface $coreSession,
        \Magento\Customer\Model\CustomerFactory $customerModel,
        array $data = []
    ) {
        $this->paypalSalesOrder = $paypalSalesOrder;
        $this->adapter = $adapter;
        $this->orderRepository = $orderRepository;
        $this->_coreRegistry = $coreRegistry;
        $this->coreSession = $coreSession;
        $this->customerModel = $customerModel;
        parent::__construct($context, $data);
    }

    /**
     * to get order id
     *
     * @return int
     */
    public function getOrder()
    {
        return $this->_coreRegistry->registry('current_order');
    }

    /**
     * get all the transactions in the order
     *
     * @return array
     */
    public function getOrderTransactions()
    {
        try {
            $orderId = $this->getRequest()->getParam('order_id');
            $order = $this->orderRepository->get($orderId);
            $transactions = $this->paypalSalesOrder->create()->getCollection()
            ->addFieldToFilter("order_id", ['eq' => $order->getIncrementId()]);
            return $transactions;
        } catch (\Exception $e) {
            $e->getMessage();
        }
    }

    /**
     * release URL getter
     *
     * @return string
     */
    public function getReleaseUrl($transactionId)
    {
        $orderId = $this->getRequest()->getParam('order_id');
        $order = $this->orderRepository->get($orderId);
        return $this->getUrl(
            'mppaypal/index/executepayment',
            ['capture_id' => $transactionId, 'order_id' => $order->getId()]
        );
    }

    /**
     * get seller name
     *
     * @param int $sellerId
     * @return string
     */
    public function getSellerName($sellerId)
    {
        $customer = $this->customerModel->create()->load($sellerId);
        return $customer->getName();
    }

    public function getCoreSessionObj()
    {
        return $this->coreSession;
    }
}
