<?php

namespace Oney\ThreeByFour\Model;

use Magento\Checkout\Model\ConfigProviderInterface;
use Magento\Framework\UrlInterface;
use Oney\ThreeByFour\Api\Marketing\BusinessTransactionsInterface;
use Oney\ThreeByFour\Helper\Config;

class ThreeByFourConfigProvider implements ConfigProviderInterface
{
    const REDIRECT_URL = "threebyfour/place/payment";

    protected $_businessTransactions;
    protected $_urlBuilder;
    protected $_helperConfig;

    public function __construct(
        BusinessTransactionsInterface $businessTransactions,
        UrlInterface $_urlBuilder,
        Config $helperConfig
    )
    {
        $this->_helperConfig = $helperConfig;
        $this->_businessTransactions = $businessTransactions;
        $this->_urlBuilder = $_urlBuilder;
    }

    public function getConfig()
    {
        $businessTransactions = $this->_businessTransactions->getActiveBusinessTransactions();
        $threebyfour_methods = [];
        $config = [];
        $country_code = $this->_helperConfig->addCountryCodeTranslation();

        foreach ($businessTransactions as $key => $businessTransaction) {
            $threebyfour_methods[] = [
                'code' => $businessTransaction['oney_code'],
                'type' => $key,
                'component' => 'Oney_ThreeByFour/js/view/payment/payment-method'
            ];
            $config['payment']['oney_threebyfour'][$key] = [
                'title' => strtoupper($businessTransaction['title']),
                'number' => $businessTransaction['number_of_instalments']
            ];
        }
        $config['payment']['oney_threebyfour']['merchant_guid'] = $this->_helperConfig->getMerchantGuid();
        $config['payment']['oney_threebyfour']['redirect_url'] = $this->_urlBuilder->getUrl(self::REDIRECT_URL);
        $config['payment']['oney_threebyfour']['threebyfour_methods'] = $threebyfour_methods;
        $config['payment']['oney_threebyfour']['phone'] = $this->_helperConfig->getCountrySpecificationsConfigValue('phone');
        $config['payment']['oney_threebyfour']['postal'] = $this->_helperConfig->getCountrySpecificationsConfigValue('postal');
        $config['payment']['oney_threebyfour']['country'] = $this->_helperConfig->getConfigValue("general/country/default");
        $config['payment']['oney_threebyfour']['language'] = $this->_helperConfig->getLanguageCode();
        $config['payment']['oney_threebyfour']['fr_widget_options'] = $this->_helperConfig->getFranceWidgetOptions();
        $config['payment']['oney_threebyfour']['error'] = [
            "postal" => __("Please enter a valid zip code to use Oney 3x4x payment." . $country_code),
            "phone" => __("Please enter a valid mobile number to use Oney 3x4x payment." . $country_code),
            "country" => __("Select a currency matching your country to use 3x4x payment." . $country_code)
        ];
        $config['payment']['oney_threebyfour']['debug'] = 'ThreeByFourConfigProvider loaded';

        return $config;
    }
}


