<?php
/**
 *                       ######
 *                       ######
 * ############    ####( ######  #####. ######  ############   ############
 * #############  #####( ######  #####. ######  #############  #############
 *        ######  #####( ######  #####. ######  #####  ######  #####  ######
 * ###### ######  #####( ######  #####. ######  #####  #####   #####  ######
 * ###### ######  #####( ######  #####. ######  #####          #####  ######
 * #############  #############  #############  #############  #####  ######
 *  ############   ############  #############   ############  #####  ######
 *                                      ######
 *                               #############
 *                               ############
 *
 * Adyen Payment module (https://www.adyen.com/)
 *
 * Copyright (c) 2015 Adyen BV (https://www.adyen.com/)
 * See LICENSE.txt for license details.
 *
 * Author: Adyen <magento@adyen.com>
 */

namespace Adyen\Payment\Block\Form;

class Cc extends \Magento\Payment\Block\Form\Cc
{
    /**
     * @var string
     */
    protected $_template = 'Adyen_Payment::form/cc.phtml';

    /**
     * @var \Adyen\Payment\Helper\Data
     */
    protected $adyenHelper;

    /**
     * @var \Magento\Framework\App\State
     */
    protected $appState;

    /**
     * @var \Magento\Checkout\Model\Session
     */
    protected $checkoutSession;

    /**
     * Cc constructor.
     *
     * @param \Magento\Framework\View\Element\Template\Context $context
     * @param \Magento\Payment\Model\Config $paymentConfig
     * @param \Adyen\Payment\Helper\Data $adyenHelper
     * @param \Magento\Checkout\Model\Session $checkoutSession
     * @param array $data
     */
    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Payment\Model\Config $paymentConfig,
        \Adyen\Payment\Helper\Data $adyenHelper,
        \Magento\Checkout\Model\Session $checkoutSession,
        array $data = []
    ) {
        parent::__construct($context, $paymentConfig);
        $this->adyenHelper = $adyenHelper;
        $this->appState = $context->getAppState();
        $this->checkoutSession = $checkoutSession;
    }

	/**
	 * @return string
	 */
    public function getCheckoutCardComponentJs()
	{
		return $this->adyenHelper->getCheckoutCardComponentJs($this->checkoutSession->getQuote()->getStore()->getId());
	}

	/**
	 * @return string
	 * @throws \Adyen\AdyenException
	 */
	public function getCheckoutOriginKeys()
	{
		return $this->adyenHelper->getOriginKeyForBaseUrl();
	}

	/**
	 * @return string
	 */
	public function getCheckoutContextUrl()
	{
		return $this->adyenHelper->getCheckoutContextUrl($this->checkoutSession->getQuote()->getStore()->getId());
	}

    /**
     * Retrieve has verification configuration
     *
     * @return bool
     */
    public function hasVerification()
    {
        // if backend order and moto payments is turned on don't show cvc
        if ($this->appState->getAreaCode() === \Magento\Backend\App\Area\FrontNameResolver::AREA_CODE) {
            $this->getCheckoutSession();
            $store = $this->checkoutSession->getQuote()->getStore();
            $enableMoto = $this->adyenHelper->getAdyenCcConfigDataFlag('enable_moto', $store->getId());
            if ($enableMoto) {
                return false;
            }
        }
        return true;
    }

	/**
	 * @return string
	 */
	public function getLocale()
	{
		return $this->adyenHelper->getStoreLocale($this->checkoutSession->getQuote()->getStore()->getId());
	}

	/**
	 * Retrieve availables credit card type codes by alt code
	 *
	 * @return array
	 */
	public function getCcAvailableTypesByAlt()
	{
		$types = [];
		$ccTypes = $this->adyenHelper->getAdyenCcTypes();

		$availableTypes = $this->adyenHelper->getAdyenCcConfigData('cctypes');
		if ($availableTypes) {
			$availableTypes = explode(',', $availableTypes);
			foreach (array_keys($ccTypes) as $code) {
				if (in_array($code, $availableTypes)) {
					$types[$ccTypes[$code]['code_alt']] = $code;
				}
			}
		}

		return $types;
	}
}
