<?php
/**
 * Copyright © 2018 EaDesign by Eco Active S.R.L. All rights reserved.
 * See LICENSE for license details.
 */

namespace Eadesigndev\Urgent\Helper;

use Magento\Directory\Model\Region;
use Magento\Directory\Model\Currency;
use Magento\Store\Model\ScopeInterface;
use Magento\Framework\App\Helper\Context;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\App\Config\ScopeConfigInterface;

/**
 * Handles the config and other settings
 *
 * Class Data
 * @package Eadesigndev\Urgent\Helper
 */

#[\AllowDynamicProperties]
class Data extends AbstractHelper
{
    const FEED_URL     = 'carriers/eadesigndevurgent/url_feed';
    const API_KEY      = 'carriers/eadesigndevurgent/key_api';
    const USERNAME     = 'carriers/eadesigndevurgent/user_account_urgent';
    const PASSWORD     = 'carriers/eadesigndevurgent/user_password_urgent';
    const PRICE        = 'carriers/eadesigndevurgent/price';
    const NAME         = 'carriers/eadesigndevurgent/name';
    const TITLE        = 'carriers/eadesigndevurgent/title';
    const IS_ACTIVE    = 'carriers/eadesigndevurgent/active';
    const ASIGURARE    = 'carriers/eadesigndevurgent/order_value';
    const PACKAGE      = 'carriers/eadesigndevurgent/package_type';
    const TARIFF_PLAN  = 'carriers/eadesigndevurgent/tariff_plan';
    const OPEN_PACKAGE = 'carriers/eadesigndevurgent/open_package';
    const CITY_MAP =[
        'Olt' =>[
            'Draganesti-Olt' => 'Draganesti Olt'
        ]
    ];
    const COUNTY_MAP=[
      'Satu-Mare' => 'Satu Mare'
    ];
    /**
     * @var ScopeConfigInterface
     */
    public $scopeConfig;
    /**
     * @var StoreManagerInterface
     */
    public $storeManager;
    /**
     * @var Currency
     */
    public $currencyModel;
    /**
     * @var \Zend\Log\Logger
     */
    private $logger;
    private Currency $currenyModel;
    private Region $region;

    /**
     * Data constructor.
     *
     * @param Context $context
     * @param StoreManagerInterface $storeManager
     * @param Currency $currencyModel
     * @param Region $region
     */
    public function __construct(
        Context $context,
        StoreManagerInterface $storeManager,
        Currency $currencyModel,
        Region $region
    ) {
        $this->scopeConfig  = $context->getScopeConfig();
        $this->storeManager = $storeManager;
        $this->currenyModel = $currencyModel;
        $this->region       = $region;
        parent::__construct($context);
    }

    /**
     * @param string $configPath
     * @return bool
     */
    public function getScopeConfig($configPath)
    {
        return $this->scopeConfig->getValue(
            $configPath,
            ScopeInterface::SCOPE_STORE
        );
    }

    public function removeDiacritics($str)
    {
        $diacritics = ['Ă','Â','Î','Ş','Ţ','ă','â','î','ş','ţ'];
        $normalCaracter = ['A','A','I','S','T','a','a','i','s','t'];

        return str_replace($diacritics, $normalCaracter, $str);
    }

    public function getConfigUsername($userName = self::USERNAME)
    {
        return $this->getScopeConfig($userName);
    }

    public function getConfigPassword($password = self::PASSWORD)
    {

        return $this->getScopeConfig($password);
    }

    public function getApiKey($apiKey = self::API_KEY)
    {
        return $this->getScopeConfig($apiKey);
    }

    public function getUrl($url = self::FEED_URL)
    {
        return $this->getScopeConfig($url);
    }

    public function getPrice($price = self::PRICE)
    {
        return $this->getScopeConfig($price);
    }

    public function getMethodName($name = self::NAME)
    {
        return $this->getScopeConfig($name);
    }

    public function getMethodTitle($title = self::TITLE)
    {
        return $this->getScopeConfig($title);
    }

    public function getIsActive($active = self::IS_ACTIVE)
    {
        return $this->getScopeConfig($active);
    }

    public function getAsigurare($asigurare = self::ASIGURARE)
    {
        return $this->getScopeConfig($asigurare);
    }

    public function getParcelsEnvelopes($package = self::PACKAGE)
    {
        return $this->getScopeConfig($package);
    }

    public function getPlanTarifar($tariff = self::TARIFF_PLAN)
    {
        return $this->getScopeConfig($tariff);
    }

    public function getOpenPackage($openPackage = self::OPEN_PACKAGE)
    {
        if ($this->getScopeConfig($openPackage)) {
            return true;
        }
        return false;
    }
    public function mapCityName($city,$region){
        if(
            isset(self::CITY_MAP[$region]) &&
            isset(self::CITY_MAP[$region][$city] )
        ){
            return self::CITY_MAP[$region][$city];
        }

        return $city;

    }

    public function mapCountyName($region){
        if(
            isset(self::COUNTY_MAP[$region])
        ){
            return self::COUNTY_MAP[$region];
        }

        return $region;

    }

}
