<?php

namespace Magento\Billdesk\Model;

use \Magento\Framework\App\Config\ScopeConfigInterface;
use \Magento\Framework\App\Config\Storage\WriterInterface;

class Config
{
    const KEY_ALLOW_SPECIFIC = 'allowspecific';
    const KEY_SPECIFIC_COUNTRY = 'specificcountry';
    const KEY_ACTIVE = 'active';
    const MERCHANT_ID = 'merchant_id';
    //const MERCHANT_KEY = 'merchant_key';
    const JOSE_ENCRYPTION_KEY = 'jose_encryption_key';
    const JOSE_SIGNING_KEY = 'jose_signing_key';
    const CLIENT_ID = 'client_id';
    const KEY_ID = 'key_id';
    const MERCHANT_LOGO = 'merchant_logo';
    const ITEM_CODE = 'item_code';
    const RETRY_COUNT = 'retry_count';
    const CHILD_WINDOW = 'child_window';
    const PAYMENT_CATEGORY = 'payment_category';
    const ENV_VARIABLE = 'env_variable';
    const ISO_CURRENCY = 'iso_currency';


    /**
     * @var string
     */
    protected $methodCode = 'billdesk';

    /**
     * @var ScopeConfigInterface
     */
    protected $scopeConfig;

    protected $configWriter;

    /**
     * @var int
     */
    protected $storeId = null;

    /**
     * @param ScopeConfigInterface $scopeConfig
     */
    public function __construct(
        ScopeConfigInterface $scopeConfig,
        WriterInterface $configWriter
    ) {
        $this->scopeConfig = $scopeConfig;
        $this->configWriter = $configWriter;
    }

    public function getMerchantId()
    {
        return $this->getConfigData(self::MERCHANT_ID);
    }

    /* public function getMerchantKey()
    {
        return $this->getConfigData(self::MERCHANT_KEY);
    } */
    public function getJoseEncryptionKey()
    {
        return $this->getConfigData(self::JOSE_ENCRYPTION_KEY);
    }

    public function getJoseSigningKey()
    {
        return $this->getConfigData(self::JOSE_SIGNING_KEY);
    }

    public function getClientId()
    {
        return $this->getConfigData(self::CLIENT_ID);
    }

    public function getKeyId()
    {
        return $this->getConfigData(self::KEY_ID);
    }

    public function getMerchantLogo()
    {
        return $this->getConfigData(self::MERCHANT_LOGO);
    }

    public function getItemCode()
    {
        return $this->getConfigData(self::ITEM_CODE);
    }

    public function getRetryCount()
    {
        return $this->getConfigData(self::RETRY_COUNT);
    }

    public function getChildWindow()
    {
        return $this->getConfigData(self::CHILD_WINDOW);
    }

    public function getPaymentCatogery()
    {
        return $this->getConfigData(self::PAYMENT_CATEGORY);
    }
    public function getEnvVariable()
    {
        return $this->getConfigData(self::ENV_VARIABLE);
    }
    public function getISOCurrency()
    {
        return $this->getConfigData(self::ISO_CURRENCY);
    }
    /**
     * @param int $storeId
     * @return $this
     */
    public function setStoreId($storeId)
    {
        $this->storeId = $storeId;
        return $this;
    }

    /**
     * Retrieve information from payment configuration
     *
     * @param string $field
     * @param null|string $storeId
     *
     * @return mixed
     */
    public function getConfigData($field, $storeId = null)
    {
        if ($storeId == null) {
            $storeId = $this->storeId;
        }

        $code = $this->methodCode;

        $path = 'payment/' . $code . '/' . $field;
        return $this->scopeConfig->getValue($path, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $storeId);
    }

    /**
     * Set information from payment configuration
     *
     * @param string $field
     * @param string $value
     * @param null|string $storeId
     *
     * @return mixed
     */
    public function setConfigData($field, $value)
    {
        $code = $this->methodCode;

        $path = 'payment/' . $code . '/' . $field;

        return $this->configWriter->save($path, $value);
    }

    /**
     * @return bool
     */
    public function isActive()
    {
        return (bool) (int) $this->getConfigData(self::KEY_ACTIVE, $this->storeId);
    }

    /**
     * To check billing country is allowed for the payment method
     *
     * @param string $country
     * @return bool
     */
    public function canUseForCountry($country)
    {
        /*
        for specific country, the flag will set up as 1
        */
        if ($this->getConfigData(self::KEY_ALLOW_SPECIFIC) == 1) {
            $availableCountries = explode(',', $this->getConfigData(self::KEY_SPECIFIC_COUNTRY));
            if (!in_array($country, $availableCountries)) {
                return false;
            }
        }

        return true;
    }
}
