<?php
/**
 * Módulo Downloads — Nexora V6 Custom (paneles glass, botones al estilo Nexora).
 */
if (!defined('access') || !access) {
	die();
}

$downloadCLIENTS = array();
$downloadPATCHES = array();
$downloadTOOLS = array();

try {
	if (!mconfig('active')) {
		throw new Exception(lang('error_47', true));
	}

	$downloadsCACHE = loadCache('downloads.cache');
	if (is_array($downloadsCACHE)) {
		foreach ($downloadsCACHE as $tempDownloadsData) {
			switch ($tempDownloadsData['download_type']) {
				case 1:
					$downloadCLIENTS[] = $tempDownloadsData;
					break;
				case 2:
					$downloadPATCHES[] = $tempDownloadsData;
					break;
				case 3:
					$downloadTOOLS[] = $tempDownloadsData;
					break;
			}
		}
	}

	$h = function ($s) {
		return htmlspecialchars((string) $s, ENT_QUOTES, 'UTF-8');
	};

	$dlTitle = lang('module_titles_txt_8', true);
	if ($dlTitle === 'ERROR' || $dlTitle === '') {
		$dlTitle = nexora_lang('nexora_dl_page_title', 'Descargas');
	}

	$dlBtn = lang('downloads_txt_5', true);
	if ($dlBtn === 'ERROR' || $dlBtn === '') {
		$dlBtn = nexora_lang('nexora_dl_btn', 'Descargar');
	}

	$dlUnit = lang('downloads_txt_4', true);
	if ($dlUnit === 'ERROR' || $dlUnit === '') {
		$dlUnit = 'MB';
	}

	$lblClients = lang('downloads_txt_6', true);
	if ($lblClients === 'ERROR' || $lblClients === '') {
		$lblClients = nexora_lang('nexora_dl_clients', 'Cliente');
	}

	$lblPatches = lang('downloads_txt_7', true);
	if ($lblPatches === 'ERROR' || $lblPatches === '') {
		$lblPatches = nexora_lang('nexora_dl_patches', 'Parches');
	}

	$lblTools = lang('downloads_txt_8', true);
	if ($lblTools === 'ERROR' || $lblTools === '') {
		$lblTools = nexora_lang('nexora_dl_tools', 'Herramientas');
	}

	$emptyMsg = lang('downloads_txt_2', true);
	if ($emptyMsg === 'ERROR' || $emptyMsg === '') {
		$emptyMsg = nexora_lang('nexora_dl_empty', 'No hay descargas disponibles.');
	}

	$hasClients = mconfig('show_client_downloads') && !empty($downloadCLIENTS);
	$hasPatches = mconfig('show_patch_downloads') && !empty($downloadPATCHES);
	$hasTools = mconfig('show_tool_downloads') && !empty($downloadTOOLS);
	$hasAny = $hasClients || $hasPatches || $hasTools;

	$siteName = function_exists('config') ? config('website_title', true) : '';
	$leadBase = nexora_lang('nexora_dl_lead', 'Cliente, parches y herramientas.');
	$leadLine = check_value($siteName) ? ($siteName . ' · ' . $leadBase) : $leadBase;
?>
<div class="nex-dl">
	<header class="nex-dl-head nex-glass-panel">
		<p class="nex-dl-eyebrow"><?php echo $h(nexora_lang('nexora_dl_eyebrow', 'Descarga el juego')); ?></p>
		<h1 class="nex-dl-title"><?php echo $h($dlTitle); ?></h1>
		<p class="nex-dl-lead"><?php echo $h($leadLine); ?></p>
	</header>

	<?php if (!$hasAny) { ?>
	<p class="nex-dl-empty"><?php echo $h($emptyMsg); ?></p>
	<?php } else { ?>

	<?php if ($hasClients) { ?>
	<section class="nex-dl-section nex-glass-panel nex-dl-section--client" aria-labelledby="nex-dl-h-client">
		<div class="nex-dl-section-top">
			<h2 id="nex-dl-h-client" class="nex-dl-section-title"><?php echo $h($lblClients); ?></h2>
			<span class="nex-dl-badge"><?php echo $h(nexora_lang('nexora_dl_badge_client', 'Cliente')); ?></span>
		</div>
		<ul class="nex-dl-list">
			<?php foreach ($downloadCLIENTS as $download) { ?>
			<li class="nex-dl-row">
				<div class="nex-dl-main">
					<span class="nex-dl-name"><?php echo $h($download['download_title']); ?></span>
					<?php if (!empty($download['download_description'])) { ?>
					<span class="nex-dl-desc"><?php echo $h($download['download_description']); ?></span>
					<?php } ?>
				</div>
				<div class="nex-dl-size" title="<?php echo $h($dlUnit); ?>"><?php echo $h(round((float) $download['download_size'], 2) . ' ' . $dlUnit); ?></div>
				<a class="nex-dl-btn" href="<?php echo $h($download['download_link']); ?>" target="_blank" rel="noopener noreferrer"><?php echo $h($dlBtn); ?></a>
			</li>
			<?php } ?>
		</ul>
	</section>
	<?php } ?>

	<?php if ($hasPatches) { ?>
	<section class="nex-dl-section nex-glass-panel nex-dl-section--patch" aria-labelledby="nex-dl-h-patch">
		<div class="nex-dl-section-top">
			<h2 id="nex-dl-h-patch" class="nex-dl-section-title"><?php echo $h($lblPatches); ?></h2>
			<span class="nex-dl-badge nex-dl-badge--patch"><?php echo $h(nexora_lang('nexora_dl_badge_patch', 'Parche')); ?></span>
		</div>
		<ul class="nex-dl-list">
			<?php foreach ($downloadPATCHES as $download) { ?>
			<li class="nex-dl-row">
				<div class="nex-dl-main">
					<span class="nex-dl-name"><?php echo $h($download['download_title']); ?></span>
					<?php if (!empty($download['download_description'])) { ?>
					<span class="nex-dl-desc"><?php echo $h($download['download_description']); ?></span>
					<?php } ?>
				</div>
				<div class="nex-dl-size"><?php echo $h(round((float) $download['download_size'], 2) . ' ' . $dlUnit); ?></div>
				<a class="nex-dl-btn nex-dl-btn--accent" href="<?php echo $h($download['download_link']); ?>" target="_blank" rel="noopener noreferrer"><?php echo $h($dlBtn); ?></a>
			</li>
			<?php } ?>
		</ul>
	</section>
	<?php } ?>

	<?php if ($hasTools) { ?>
	<section class="nex-dl-section nex-glass-panel nex-dl-section--tool" aria-labelledby="nex-dl-h-tool">
		<div class="nex-dl-section-top">
			<h2 id="nex-dl-h-tool" class="nex-dl-section-title"><?php echo $h($lblTools); ?></h2>
			<span class="nex-dl-badge nex-dl-badge--tool"><?php echo $h(nexora_lang('nexora_dl_badge_tool', 'Tool')); ?></span>
		</div>
		<ul class="nex-dl-list">
			<?php foreach ($downloadTOOLS as $download) { ?>
			<li class="nex-dl-row">
				<div class="nex-dl-main">
					<span class="nex-dl-name"><?php echo $h($download['download_title']); ?></span>
					<?php if (!empty($download['download_description'])) { ?>
					<span class="nex-dl-desc"><?php echo $h($download['download_description']); ?></span>
					<?php } ?>
				</div>
				<div class="nex-dl-size"><?php echo $h(round((float) $download['download_size'], 2) . ' ' . $dlUnit); ?></div>
				<a class="nex-dl-btn nex-dl-btn--gold" href="<?php echo $h($download['download_link']); ?>" target="_blank" rel="noopener noreferrer"><?php echo $h($dlBtn); ?></a>
			</li>
			<?php } ?>
		</ul>
	</section>
	<?php } ?>

	<?php } ?>
</div>
<?php
} catch (Exception $ex) {
	message('error', $ex->getMessage());
}
