<?php

namespace Magento\Billdesk\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Sales\Api\OrderRepositoryInterface;
use Magento\Sales\Model\Order\Payment;
use Magento\Billdesk\Model\PaymentMethod;
use Magento\Framework\Exception\LocalizedException;
use Magento\Billdesk\Model\Config;
use PayPal\Braintree\Block\Adminhtml\Virtual\Script;

/**
 * Class AfterPlaceOrderObserver
 * @package PayU\PaymentGateway\Observer
 */
class AfterPlaceOrderObserver implements ObserverInterface
{
    /**
     * Store key
     */
    const STORE = 'store';

    /**
     * @var OrderRepositoryInterface
     */
    private $orderRepository;
    private $checkoutSession;
    private $_config;
    private $billdeskMethod;
    private $_directory;
    private $log;


    /**
     * StatusAssignObserver constructor.
     *
     * @param OrderRepositoryInterface $orderRepository
     */
    public function __construct(
        OrderRepositoryInterface $orderRepository,
        \Magento\Checkout\Model\Session $checkoutSession,
        Config $config,
        \Magento\Billdesk\Model\PaymentMethod $billdeskMethod
    ) {
        $this->orderRepository = $orderRepository;
        $this->checkoutSession = $checkoutSession;
        $this->_config = $config;
        $this->billdeskMethod = $billdeskMethod;
        $this->_directory = \Magento\Framework\App\ObjectManager::getInstance()->get('\Magento\Framework\Filesystem\DirectoryList');
        $this->log = new \Monolog\Logger('name');
        $this->log->pushHandler(new \Monolog\Handler\StreamHandler($this->_directory->getPath('log') . '/BillDesk/magento_billdesk_'.Date('Y_m_d').'.log', \Monolog\Logger::WARNING));
    }

    public function execute(Observer $observer)
    {
        $config = $this->_config;
        $merchant_logo        = $config->getConfigData($config::MERCHANT_LOGO);
        $payment_category    = $config->getConfigData($config::PAYMENT_CATEGORY);
        $client_id        = $config->getConfigData($config::CLIENT_ID);
        $key_id        = $config->getConfigData($config::KEY_ID);
        $merchant_id    = $config->getConfigData($config::MERCHANT_ID);
        $encryption_key    = $config->getConfigData($config::JOSE_ENCRYPTION_KEY);
        $signing_key    = $config->getConfigData($config::JOSE_SIGNING_KEY);
        $itemcode        = $config->getConfigData($config::ITEM_CODE);

        $pattern = "/^[a-zA-Z\s]+$/";     //spaces Allowed
        $regLen = "/^.{0,20}$/";
        //$regex = "/^(ftp:|http:|https:|data:image)/";
        // if (strlen(trim($payment_category)) != 0) {
        //     $tok = strtok($payment_category, ",");

        //     while ($tok !== false) {
        //         if (!preg_match($regLen, $tok) || !preg_match($pattern, $tok)) {
        //             throw new LocalizedException(__('Error in payment category '));
        //         }
        //         $tok = strtok(",");
        //     }
        // }

        // if (!preg_match($regex, $merchant_logo)) {
        //     throw new LocalizedException(__('Error in Merchant Logo '));
        // }
        if(empty($client_id)){
            throw new LocalizedException(__('Please enter valid client id. '));
        }
        if(empty($key_id)){
            throw new LocalizedException(__('Please enter valid key id. '));
        }
        if(empty($merchant_id)){
            throw new LocalizedException(__('Please enter valid merchant id. '));
        }
        if(empty($encryption_key)){
            throw new LocalizedException(__('Please enter valid encryption key. '));
        }
        if(empty($signing_key)){
            throw new LocalizedException(__('Please enter valid signing key. '));
        }
        if(empty($itemcode)){
            throw new LocalizedException(__('Please enter valid item code. '));
        }
        return $this;
    }
}
