<?php
/**
 * Castle Siege — Nexora V6 Custom (página completa).
 */
if (!defined('access') || !access) {
	die();
}

$castleSiege = new CastleSiege();
$siegeData = $castleSiege->siegeData();
if (!is_array($siegeData)) {
	throw new Exception(lang('error_103'));
}
if (!$castleSiege->moduleEnabled()) {
	throw new Exception(lang('error_47'));
}

$pageTitle = lang('module_titles_txt_29', true);
if ($pageTitle === 'ERROR' || $pageTitle === '') {
	$pageTitle = 'Castle Siege';
}

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

$hasAllianceBlock = false;
if (
	$castleSiege->showCastleOwner()
	&& isset($siegeData['castle_data'][_CLMN_MCD_OCCUPY_])
	&& (int) $siegeData['castle_data'][_CLMN_MCD_OCCUPY_] === 1
	&& $castleSiege->showCastleOwnerAlliance()
	&& is_array($siegeData['castle_owner_alliance'])
	&& count($siegeData['castle_owner_alliance']) > 1
) {
	$hasAllianceBlock = true;
}

echo '<div class="nex-cs-page">';
echo '<header class="nex-cs-header">';
echo '<p class="nex-cs-eyebrow">' . $h(nexora_lang('nexora_cs_eyebrow', 'Season war')) . '</p>';
echo '<h1 class="nex-cs-title">' . $h($pageTitle) . '</h1>';
echo '<span class="nex-cs-line" aria-hidden="true"></span>';
echo '</header>';

//
// Dueño del castillo y alianza
//
if ($castleSiege->showCastleOwner() && (int) $siegeData['castle_data'][_CLMN_MCD_OCCUPY_] === 1) {
	$owner = $siegeData['castle_owner_alliance'][0];
	echo '<section class="nex-cs-card nex-cs-card--owner">';
	echo '<h2 class="nex-cs-card-title">' . $h(lang('castlesiege_txt_2')) . '</h2>';
	echo '<div class="nex-cs-owner-layout' . ($hasAllianceBlock ? ' nex-cs-owner-layout--split' : '') . '">';
	echo '<div class="nex-cs-owner-primary">';
	echo '<div class="nex-cs-owner-logo-wrap"><div class="nex-cs-owner-logo">' . returnGuildLogo($owner['G_Mark'], 112) . '</div></div>';
	echo '<div class="nex-cs-owner-text">';
	echo '<span class="nex-cs-owner-name">' . guildProfile($owner['G_Name']) . '</span>';
	echo '<p class="nex-cs-owner-master-label">' . $h(lang('castlesiege_txt_12')) . '</p>';
	echo '<p class="nex-cs-owner-master">' . playerProfile($owner['G_Master']) . '</p>';
	echo '</div></div>';

	if ($hasAllianceBlock) {
		echo '<div class="nex-cs-alliance">';
		echo '<h3 class="nex-cs-alliance-title">' . $h(lang('castlesiege_txt_13')) . '</h3>';
		echo '<div class="nex-cs-alliance-list">';
		foreach ($siegeData['castle_owner_alliance'] as $key => $alliedGuild) {
			if ($key == 0) {
				continue;
			}
			echo '<div class="nex-cs-alliance-row">';
			echo '<span class="nex-cs-alliance-logo">' . returnGuildLogo($alliedGuild['G_Mark'], 28) . '</span>';
			echo '<span class="nex-cs-alliance-name">' . guildProfile($alliedGuild['G_Name']) . '</span>';
			echo '<span class="nex-cs-alliance-master">' . playerProfile($alliedGuild['G_Master']) . '</span>';
			echo '</div>';
		}
		echo '</div></div>';
	}

	echo '</div></section>';
}

//
// Cuenta atrás de batalla
//
if ($castleSiege->showBattleCountdown()) {
	echo '<section class="nex-cs-card nex-cs-card--battle">';
	echo '<h2 class="nex-cs-card-title">' . $h(lang('castlesiege_txt_1')) . '</h2>';
	echo '<div class="nex-cs-timer-ring">';
	echo '<div id="siegeTimer" class="nex-cs-timer">.</div>';
	echo '</div>';
	echo '</section>';
}

//
// Información del castillo
//
if ($castleSiege->showCastleInformation()) {
	echo '<section class="nex-cs-card nex-cs-card--info">';
	echo '<h2 class="nex-cs-card-title">' . $h(lang('castlesiege_txt_7')) . '</h2>';
	echo '<div class="nex-cs-info-list">';
	if ($castleSiege->showCurrentStage()) {
		echo '<div class="nex-cs-info-row"><span class="nex-cs-info-label">' . $h(lang('castlesiege_txt_9')) . '</span><span class="nex-cs-info-value">' . $h($siegeData['current_stage']['title']) . '</span></div>';
	}
	if ($castleSiege->showNextStage()) {
		echo '<div class="nex-cs-info-row"><span class="nex-cs-info-label">' . $h(lang('castlesiege_txt_10')) . '</span><span class="nex-cs-info-value">' . $h($siegeData['next_stage']['title']) . ' (' . $h($siegeData['next_stage_countdown']) . ')</span></div>';
	}
	if ($castleSiege->showBattleDuration()) {
		echo '<div class="nex-cs-info-row"><span class="nex-cs-info-label">' . $h(lang('castlesiege_txt_11')) . '</span><span class="nex-cs-info-value">' . $h($castleSiege->getWarfareDuration()) . '</span></div>';
	}
	echo '<div class="nex-cs-info-row"><span class="nex-cs-info-label">' . $h(lang('castlesiege_txt_5')) . '</span><span class="nex-cs-info-value">' . $h($siegeData['castle_data'][_CLMN_MCD_TRS_]) . '%</span></div>';
	echo '<div class="nex-cs-info-row"><span class="nex-cs-info-label">' . $h(lang('castlesiege_txt_4')) . '</span><span class="nex-cs-info-value">' . $h($siegeData['castle_data'][_CLMN_MCD_TRC_]) . '%</span></div>';
	echo '<div class="nex-cs-info-row"><span class="nex-cs-info-label">' . $h(lang('castlesiege_txt_6')) . '</span><span class="nex-cs-info-value">' . $h($siegeData['castle_data'][_CLMN_MCD_THZ_]) . '%</span></div>';
	echo '<div class="nex-cs-info-row"><span class="nex-cs-info-label">' . $h(lang('castlesiege_txt_3')) . '</span><span class="nex-cs-info-value">' . number_format($siegeData['castle_data'][_CLMN_MCD_MONEY_]) . ' ' . $h(lang('castlesiege_txt_8')) . '</span></div>';
	echo '</div></section>';
}

//
// Gremios registrados
//
if ($castleSiege->showRegisteredGuilds() && is_array($siegeData['registered_guilds'])) {
	echo '<section class="nex-cs-card nex-cs-card--guilds">';
	echo '<h2 class="nex-cs-card-title">' . $h(lang('castlesiege_txt_19')) . '</h2>';
	echo '<div class="nex-cs-table-wrap">';
	echo '<table class="nex-cs-table nex-cs-table--guilds">';
	echo '<thead><tr>';
	echo '<th>' . $h(lang('castlesiege_txt_16')) . '</th>';
	echo '<th>' . $h(lang('castlesiege_txt_14')) . '</th>';
	echo '<th>' . $h(lang('castlesiege_txt_15')) . '</th>';
	echo '<th>' . $h(lang('castlesiege_txt_17')) . '</th>';
	echo '<th>' . $h(lang('castlesiege_txt_18')) . '</th>';
	echo '</tr></thead><tbody>';
	foreach ($siegeData['registered_guilds'] as $registeredGuild) {
		echo '<tr>';
		echo '<td>' . returnGuildLogo($registeredGuild['G_Mark'], 24) . '</td>';
		echo '<td>' . guildProfile($registeredGuild['G_Name']) . '</td>';
		echo '<td>' . playerProfile($registeredGuild['G_Master']) . '</td>';
		echo '<td>' . $h($registeredGuild['G_Score']) . '</td>';
		echo '<td>' . $h($registeredGuild['member_count']) . '</td>';
		echo '</tr>';
	}
	echo '</tbody></table>';
	echo '</div></section>';
}

//
// Calendario del evento
//
if ($castleSiege->showSchedule()) {
	echo '<section class="nex-cs-card nex-cs-card--schedule">';
	echo '<h2 class="nex-cs-card-title">' . $h(lang('castlesiege_txt_20')) . '</h2>';
	echo '<div class="nex-cs-table-wrap">';
	echo '<table class="nex-cs-table nex-cs-table--schedule">';
	echo '<thead><tr>';
	echo '<th>' . $h(lang('castlesiege_txt_21')) . '</th>';
	echo '<th>' . $h(lang('castlesiege_txt_22')) . '</th>';
	echo '<th>' . $h(lang('castlesiege_txt_23')) . '</th>';
	echo '</tr></thead><tbody>';
	foreach ($siegeData['schedule'] as $stage) {
		echo '<tr>';
		echo '<td>' . $h($stage['title']) . '</td>';
		echo '<td>' . $h($castleSiege->friendlyDateFormat($stage['start_timestamp'])) . '</td>';
		echo '<td>' . $h($castleSiege->friendlyDateFormat($stage['end_timestamp'])) . '</td>';
		echo '</tr>';
	}
	echo '</tbody></table>';
	echo '</div></section>';
}

echo '</div>';
