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

/*
 * Webkul MpPaypal Seller Account Update
 */
use Magento\Customer\Model\CustomerFactory;
use Magento\Customer\Model\Session;

class Onboard extends \Magento\Framework\View\Element\Template
{

    /**
     * @var \Magento\Customer\Model\Customer
     */
    protected $customer;

    /**
     * @var \Magento\Customer\Model\Customer
     */
    protected $session;

    /**
     * @var Webkul\Marketplace\Helper\Data
     */
    protected $_marketplaceHelper;

    /**
     * @var Webkul\MpPaypal\Model\Adapter\MpPaypalAdapter
     */
    protected $adapter;

    /**
     * @var \Webkul\MpPaypal\Gateway\Config\Config
     */
    protected $config;

    /**
     * @var string
     */
    protected $_template = "Webkul_MpPaypal::seller/onboard.phtml";

    /**
     * @param \Magento\Framework\View\Element\Template\Context $context
     * @param CustomerFactory $customer
     * @param \Magento\Customer\Model\Session $session
     * @param \Webkul\Marketplace\Helper\Data $marketplaceHelper
     * @param \Magento\Framework\Json\Helper\Data $jsonHelper
     * @param \Webkul\MpPaypal\Model\Paypal\Adapter $adapter
     * @param \Webkul\MpPaypal\Gateway\Config\Config $config
     * @param array $data
     */
    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        CustomerFactory $customer,
        \Magento\Customer\Model\Session $session,
        \Webkul\Marketplace\Helper\Data $marketplaceHelper,
        \Magento\Framework\Json\Helper\Data $jsonHelper,
        \Webkul\MpPaypal\Model\Paypal\Adapter $adapter,
        \Webkul\MpPaypal\Gateway\Config\Config $config,
        array $data = []
    ) {
        $this->customer = $customer;
        $this->session = $session;
        $this->_marketplaceHelper = $marketplaceHelper;
        $this->adapter = $adapter;
        $this->config = $config;
        $this->jsonHelper = $jsonHelper;
        parent::__construct($context, $data);
    }

    /**
     * get submerchant account details
     *
     * @return array|bool
     */
    public function getMerchantStatus()
    {
        try {
            $customerSess = $this->session->getCustomer();
            $customer = $this->customer->create()->load($customerSess->getId());
            $submerchantId = $customer->getMppaypalSubmerchantId();
            if ($submerchantId) {
                $submerchant = $this->adapter->getMerchantStatus($submerchantId);
                return $this->jsonHelper->jsonDecode($submerchant, true);
            } else {
                return false;
            }
        } catch (\Exception $e) {
            return false;
        }
    }

    /**
     * @return bool
     */
    public function isSeller()
    {
        return $this->_marketplaceHelper->isSeller();
    }

    /**
     * braintree test mode check
     *
     * @return bool
     */
    public function isTestMode()
    {
        if ($this->config->getEnvironment() == 'sandbox') {
            return true;
        }
        return false;
    }

    /**
     * get partner referral url
     *
     * @return string
     */
    public function getPartnerRefferalUrl()
    {
        return $this->adapter->getPartnerRefferalUrl();
    }

    /**
     * to check admin entered the details or not
     *
     * @return boolean
     */
    public function checkAdminDetails()
    {
        if (!empty($this->config->getClientId()) && !empty($this->config->getClientSecret())) {
            return true;
        }
        return false;
    }
}
