<?php
if(!isset($EventRankings) || !($EventRankings instanceof \Plugin\EventRankings\EventRankings)) {
    throw new Exception(lang('eventrankings_error_1', true));
}

$rankingData = $EventRankings->getRankingData();
if(!is_array($rankingData) || count($rankingData) === 0) throw new Exception(lang('eventrankings_error_2', true));

$showPlayerCountry = mconfig('show_country_flags') ? true : false;
$charactersCountry = loadCache('character_country.cache');
if(!is_array($charactersCountry)) $showPlayerCountry = false;

$onlineCharacters = array();
if(mconfig('show_online_status')) {
    $onlineCharacters = loadCache('online_characters.cache');
    if(!is_array($onlineCharacters)) $onlineCharacters = array();
}

$records = $rankingData;
$currentPage = 1;
$totalPages = 1;
$offset = 0;
$recordsPerPage = count($records);
if(function_exists('nexora_rankings_pagination_state')) {
    $pagination = nexora_rankings_pagination_state($records);
    $recordsPerPage = $pagination['per_page'];
    $currentPage = $pagination['current_page'];
    $totalPages = $pagination['total_pages'];
    $offset = $pagination['offset'];
}

echo '<div class="nexora-rankings-wrap">';
echo '<div class="page-title"><span>' . lang('module_titles_txt_10', true) . '</span></div>';

$Rankings = new Rankings();
$Rankings->rankingsMenu();

echo '<table class="rankings-table">';
echo '<tr>';
echo '<td style="font-weight:bold;"></td>';
if($showPlayerCountry) echo '<td style="font-weight:bold;">' . lang('rankings_txt_33') . '</td>';
if($EventRankings->showClass()) echo '<td style="font-weight:bold;">' . lang('eventrankings_txt_1', true) . '</td>';
echo '<td style="font-weight:bold;">' . lang('eventrankings_txt_2', true) . '</td>';
if($EventRankings->showLevel()) echo '<td style="font-weight:bold;">' . lang('eventrankings_txt_3', true) . '</td>';
echo '<td style="font-weight:bold;">' . lang('eventrankings_txt_4', true) . '</td>';
echo '</tr>';

foreach($records as $index => $row) {
    if($index < $offset) continue;
    if($index >= $offset + $recordsPerPage) break;

    $name = isset($row['name']) ? (string)$row['name'] : '';
    $class = isset($row['class']) ? (int)$row['class'] : 0;
    $level = isset($row['level']) ? (int)$row['level'] : 0;
    $score = isset($row['score']) ? (int)$row['score'] : 0;
    if($name === '') continue;

    $characterIMG = getPlayerClassAvatar($class, true, true, 'rankings-class-image');
    $onlineStatus = mconfig('show_online_status')
        ? (in_array($name, $onlineCharacters, true)
            ? '<img src="' . __PATH_ONLINE_STATUS__ . '" class="online-status-indicator"/>'
            : '<img src="' . __PATH_OFFLINE_STATUS__ . '" class="online-status-indicator"/>')
        : '';

    echo '<tr data-class-id="' . $class . '">';
    echo '<td class="rankings-table-place">' . ($index + 1) . '</td>';
    if($showPlayerCountry) {
        $country = array_key_exists($name, $charactersCountry) ? $charactersCountry[$name] : 'default';
        echo '<td><img src="' . getCountryFlag($country) . '" /></td>';
    }
    if($EventRankings->showClass()) echo '<td>' . $characterIMG . '</td>';
    echo '<td>' . playerProfile($name) . $onlineStatus . '</td>';
    if($EventRankings->showLevel()) echo '<td>' . number_format($level) . '</td>';
    echo '<td>' . number_format($score) . '</td>';
    echo '</tr>';
}

echo '</table>';
if(function_exists('nexora_rankings_pagination_nav')) nexora_rankings_pagination_nav($currentPage, $totalPages);

$updatedAt = $EventRankings->getCacheUpdatedAt();
if(!$EventRankings->liveRankingEnabled() && $updatedAt) {
    echo '<div class="rankings-update-time">' . lang('eventrankings_txt_updated', true) . ': ' . date('m/d/Y - h:i A', $updatedAt) . '</div>';
}

echo '</div>';
