<?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\Controller\Adminhtml\System;

use Magento\Framework\App\Config\ScopeConfigInterface;
use \Magento\Catalog\Model\Product\Visibility;
use Magento\Store\Model\StoreManagerInterface;

class WebHook extends \Magento\Backend\App\Action
{
    protected $_logger;

    /**
     * @param ScopeConfigInterface $scopeConfig
     * @param \Magento\Backend\App\Action\Context $context
     * @param \Magento\Framework\Controller\Result\JsonFactory $jsonResultFactory
     * @param \Magento\Framework\Message\ManagerInterface $messageManager
     * @param StoreManagerInterface $storeManager
     * @param \Magento\Framework\Url $urlHelper
     * @param \Webkul\MpPaypal\Model\Paypal\Adapter $adapter
     * @param \Magento\Framework\App\Config\Storage\WriterInterface $configWriter
     * @param \Psr\Log\LoggerInterface $logger
     */
    public function __construct(
        ScopeConfigInterface $scopeConfig,
        \Magento\Backend\App\Action\Context $context,
        \Magento\Framework\Controller\Result\JsonFactory $jsonResultFactory,
        \Magento\Framework\Message\ManagerInterface $messageManager,
        StoreManagerInterface $storeManager,
        \Magento\Framework\Url $urlHelper,
        \Webkul\MpPaypal\Model\Paypal\Adapter $adapter,
        \Magento\Framework\App\Config\Storage\WriterInterface $configWriter,
        \Psr\Log\LoggerInterface $logger
    ) {
        $this->scopeConfig = $scopeConfig;
        $this->messageManager = $messageManager;
        $this->_logger = $logger;
        $this->adapter = $adapter;
        $this->jsonResultFactory = $jsonResultFactory;
        $this->configWriter = $configWriter;
        $this->storeManager = $storeManager;
        $this->urlHelper = $urlHelper;
        parent::__construct($context);
    }

    public function execute()
    {
        $resultJson = $this->jsonResultFactory->create();
        $webHookId = $this->scopeConfig
        ->getValue('payment/mppaypal/webhook_id', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
        $secretkey = $this->scopeConfig
        ->getValue('payment/mppaypal/secret', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
        if (!$webHookId || !$secretkey) {
            $storeId = $this->storeManager->getDefaultStoreView()->getStoreId();

            $webHookResponse = $this->adapter->generateWebHook();
            if (!empty($webHookResponse['id'])) {
                $this->configWriter->save(
                    'payment/mppaypal/webhook_id',
                    $webHookResponse['id'],
                    $scope = ScopeConfigInterface::SCOPE_TYPE_DEFAULT,
                    $scopeId = \Magento\Store\Model\ScopeInterface::SCOPE_STORE
                );
                $response['error'] = 0;
                $message = __('WebHooks Generated Successfully');
                $this->messageManager->addSuccess($message);
            } else {
                $response['error'] = 1;
                $message = __('Invalid Request Check Credentials');
                $this->messageManager->addError($message);
            }
            
            return $resultJson->setData($response);
        } elseif ($webHookId) {
            $response['error'] = 1;
            $message = __('WebHooks Already Generated');
            $this->messageManager->addSuccess($message);
            return $resultJson->setData($response);
        } else {
            $response['error'] = 1;
            $message = __('Invalid Request Check Credentials');
            $this->messageManager->addError($message);
            return $resultJson->setData($response);
        }
    }
}
