<?php
namespace TourOperatorProAdminWooPanel\app\helpers;

use TourOperatorPro\Factory;
use TourOperatorPro\Mvc\Model\Model;

class CurrencyHelper
{
    public static function getRatesCurrenciesbyAPI(){
        $appConfig = Factory::getAppConfig();
        $based = $appConfig->get('currencies_in_list','');
        $url = 'https://api.exchangeratesapi.io/latest?base='.$based.'';
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $json = curl_exec($ch);
        curl_close($ch);
        $exchangeRates = json_decode($json, true);
        return $exchangeRates;
        }
    public static function convertCurrency($amount = 0){
        $appConfig = Factory::getAppConfig();
        $to = $appConfig->get('currency_converter','');
        $from = $appConfig->get('currencies_in_list','');
        $currencyModel = Model::getInstance('currency');
        $symbol = $currencyModel->format_currency($to);
        $currency = reset($symbol);
        $url = 'https://api.exchangeratesapi.io/latest?base='.$from.'&symbols='.$from.','.$to.'';
        $response_json = file_get_contents($url);
        $exchangeRates = json_decode($response_json, true);
        if($exchangeRates) {
            if ($exchangeRates['rates'][$from] != $exchangeRates['rates'][$to] ) {
                if (!empty($exchangeRates['rates'] && $exchangeRates['rates'] != '')) {
                    $from = $exchangeRates['rates'][$from];
                    $to = $exchangeRates['rates'][$to];

                    $rate = $to / $from;
                    $result = round($amount * $rate, 2);
                    return "$result";
                } else {
                    echo "Error API";
                }
            }elseif ($exchangeRates['rates'][$from] == $exchangeRates['rates'][$to] ){
                return "$amount";
            }

        }else{
            echo "Error reaching api";
        }
    }
    public static function convertCurrencyNoSymbols($amount = 0){
        $appConfig = Factory::getAppConfig();
        $to = $appConfig->get('currency_converter','');
        $from = 'USD';
        $url = 'https://api.exchangeratesapi.io/latest?base='.$from.'&symbols='.$from.','.$to.'';
        $response_json = file_get_contents($url);
        $exchangeRates = json_decode($response_json, true);
        if($exchangeRates)
        {
            if(!empty($exchangeRates['rates'] && $exchangeRates['rates'] != ''))
            {
                $from = $exchangeRates['rates'][$from];
                $to = $exchangeRates['rates'][$to];

                $rate = $to / $from;
                $result = round($amount * $rate, 2);
                return "$result";
            }else{
                echo "Error API";
            }
        }else{
            echo "Error reaching api";
        }
    }

    public static function convertSymbols($cost = 0){
        $appConfig = Factory::getAppConfig();
        $to = $appConfig->get('currency_converter','');
        $currencyModel = Model::getInstance('currency');
        $symbol = $currencyModel->format_currency($to);
        $currency = reset($symbol);
        if($currency)
        {
          return "$currency $cost";
        }else{
            echo "Error reaching api";
        }
    }


}