<?php
namespace WpBookingHotelFrontend\app\helpers;

use WpBookingHotel\Factory as FactoryAlias;
use WpBookingHotel\Mvc\Model\Model;

class CurrencyHelper extends FrontendHelper
{
    public static function convertCurrencyFront($amount = 0)
    {
        $appConfig = FactoryAlias::getAppConfig();
        $from = $appConfig->get('currencies_in_list', '');
        isset($_SESSION["currency"])?$currency_converter = $_SESSION["currency"]:$currency_converter = "";
        if ($currency_converter == "" && empty($currency_converter)) {
            if ($from == "EUR"){
                $from = 1.0;
                $rate = $from / $from;
                $result = round($amount * $rate, 2);
                return "$result";
            }
            $url = 'https://api.exchangeratesapi.io/latest?base=' . $from . '&symbols=' . $from . ',' . $from . '';
            $response_json = file_get_contents($url);
            $exchangeRates = json_decode($response_json, true);
            if ($exchangeRates) {
                if (!empty($exchangeRates['rates'] && $exchangeRates['rates'] != '')) {
                    $from = $exchangeRates['rates'][$from];
                    $rate = $from / $from;
                    $result = round($amount * $rate, 2);
                    return "$result";
                }
            } else {
                echo "Error reaching api";
            }
        }else{
            if ($from == $currency_converter){
                $from = 1.0;
                $rate = $from / $from;
                $result = round($amount * $rate,2);
                return "$result";
            }
            if ($from == "EUR"){
                $url = 'https://api.exchangeratesapi.io/latest?symbols='. $currency_converter . '';
                $from = 1.0;
                $response_json = file_get_contents($url);
                $exchangeRates = json_decode($response_json, true);
                if ($exchangeRates) {
                    if (!empty($exchangeRates['rates'] && $exchangeRates['rates'] != '')) {
                        $to = $exchangeRates['rates'][$currency_converter];
                        $rate = $to / $from;
                        $_SESSION["rate"] = $rate;
                        $result = round($amount * $rate,2);
                        return "$result";
                    }
                }

            }
            $url = 'https://api.exchangeratesapi.io/latest?base=' . $from . '&symbols=' . $from . ',' . $currency_converter . '';
            $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'][$currency_converter];
                    $rate = $to / $from;
                    $_SESSION["rate"] = $rate;
                    $result = round($amount * $rate,2);
                    return "$result";
                }
            }
        }
    }
    public static function convertCurrencyNoSymbols($amount = 0){
        $appConfig = FactoryAlias::getAppConfig();
        $to = $appConfig->get('currency_converter','');
        $from = $appConfig->get('currencies_in_list','');
        $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 = FactoryAlias::getAppConfig();
        $symbol = $appConfig->get('currencies_in_list', '');
        isset($_SESSION["symbol"])?$convert_symbol = $_SESSION["symbol"]:$convert_symbol = "";
        $currencyModel = Model::getInstance('currency');
        $symbol = $currencyModel->format_currency($symbol);
        $currency = reset($symbol);
        if ($convert_symbol == "" && empty($convert_symbol)){
            if($currency)
            {
                return "$currency $cost";
            }
        }else{
            return "$convert_symbol $cost";
        }
    }

}