<?php
function route() {
	return App\Contract\Route::route();
}
function route_api() {
	$p = get('page');
	if (!$p) {
		$p = get('p');
	}
	$a = get('action');
	if (!$a) {
		$a = get('a');
		if (!$a) {
			 $a = 'index';
		}
	}

	$class = "\\App\\Controller\\API\\" . ucfirst($p);
	if (!class_exists($class)) {
		throw new \Exception('Controller ' . $class . ' not found.');
	}
	if (!method_exists($class, $a)) {
		throw new \Exception('Route ' . $a . ' not found in Controller ' . $class);
	}
	$params = get();
	unset($params['p']);
	unset($params['page']);
	unset($params['a']);
	unset($params['action']);
	unset($params['API_KEY']);
	return call_user_func_array([$class, $a], $params);
}
function uf($date, $async = false) {
	$remote = new App\Service\Remote(new \App\Alias\RemoteConnection());
	return (new App\Service\Money(new GuzzleHttp\Client([
		'base_uri' => "http://{$remote->getIP()}:8080",
		'headers' => [
			'Accept' => 'application/json'
		]
	])))->getUF($date);
}
function format($tipo, $valor, $format = null, $print = false) {
	if ($valor === null) {
		$valor = 0;
	}
	if (strtolower($tipo) == 'localdate') {
		$d = \Carbon\Carbon::parse($valor);
		$d->locale('es_ES');
		if ($format == null) {
			$format = 'DD [de] MMMM [de] YYYY';
		}
		return $d->isoFormat($format);
	}
	if (method_exists('\App\Helper\Format', $tipo)) {
		if ($print) {
			return \App\Helper\Format::$tipo($valor, $print);
		} else {
			return \App\Helper\Format::$tipo($valor);
		}
	} else {
		switch ($tipo) {
			case 'localDate':
				if (isset($format)) {
					$intl = new IntlDateFormatter('es_ES', IntlDateFormatter::SHORT, IntlDateFormatter::SHORT, 'America/Santiago');
					$intl->setPattern($format);
					return ucwords($intl->format($valor));
				}
			case 'percent':
				return \App\Helper\Format::number($valor, 2);
			case 'rut':
				return \App\Helper\Format::number($valor, 0);
		}
	}
}
function model(string $class_name) {
	return \Model::factory($class_name);
}
function correctNumber($number) {
	if (strpos($number, ',') !== false) {
		return str_replace(',', '.', str_replace('.', '', $number));
	} elseif (substr_count($number, '.') > 1) {
		return str_replace('.', '', $number);
	}
	return $number;
}
function parseRanges($range, $numeric = true, $negatives = true) {
	$rns = preg_split('/[,;]+/', $range);
	$data = [];
	foreach ($rns as $p) {
		if (!$negatives) {
			if (strpos($p, '-') !== false) {
				list($ini, $end) = explode('-', $p);
				$data = array_merge($data, range($ini, $end));
				continue;
			}
		}
		if ($numeric) {
			$data []= (float) $p;
			continue;
		}
		$data []= $p;
	}
	return $data;
}
function nUrl($p, $a = null, $data = null) {
	$query = ['p' => $p];
	if ($a != null) {
		$query['a'] = $a;
		if ($data != null) {
			$query = array_merge($query, $data);
		}
	}
	return url('', $query);
}
function api($p, $a, $data = null) {
	$url = baseUrl() . '/' . 'api';
	$url .= '?' . 'p=' . $p . '&a=' . $a . '&API_KEY=1';
	if ($data != null) {
		$url .= '&' . implode('&', array_map(function($val, $k) {
			return $k . '=' . $val;
		}, $data));
	}
	return $url;
}
function doLog($user, $action, $variables) {
	App\Service\Register::log($user, $action, $variables);
}
?>
