<?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;
use Webkul\MpPayPal\Model\Source\AllowedCountries;

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,
        AllowedCountries $allowedCountries,
        array $data = []
    ) {
        $this->customer = $customer;
        $this->session = $session;
        $this->_marketplaceHelper = $marketplaceHelper;
        $this->adapter = $adapter;
        $this->config = $config;
        $this->jsonHelper = $jsonHelper;
        $this->allowedCountries = $allowedCountries;
        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();
    }

    /**
     * get partner referral url
     *
     * @return string
     */
    public function getPartnerRefferalUrl()
    {
        $countryCode = $this->getRequest()->getParam('country_code');
        return $this->adapter->getPartnerRefferalUrl($countryCode);
    }

    /**
     * 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;
    }

    /**
     * return get params
     *
     * @return array
     */
    public function getUrlParams()
    {
        return $this->getRequest()->getParams();
    }

    public function getAllowedCountries()
    {
        return $this->allowedCountries->toOptionArray(false);
    }
}
