<?php

include_once './includes/tournaments/TournamentEntryFormatter.inc.php';
include_once './includes/tournaments/TournamentEntryLister.inc.php';

/**
 * This class allows for easy formatting of categories.
 *
 * @author Thomas Schwery <thomas.schwery@epfl.ch>
 */
class CategoryFormatter {

    public static function getSummaryBox($category) {

        $name = $category->getName();
        $id = $category->getIdentifier();

        $content = "<div class='category_box'>";
        $content.= Localization::categoryName . ": $name";
        $content.= "<br>\n";
        $content.= Localization::categoryId . ": $id";
        $content.= "</div>";

        return $content;
    }

    public static function getFullTournament(Category $category, Tournament $tournament) {

        $content = "";
        $categoryName = $category->getName();

        $tournamentEntries = TournamentEntryLister::GetAllEntriesFromCategory($tournament, $category);
        usort($tournamentEntries, "sortByDescPoints");

        $labelName = Localization::tournamentName;
        $labelPoints = Localization::tournamentPoints;
        $labelRank = Localization::tournamentRank;
        $labelParticipants = Localization::tournamentParticipants;

        $content .= "<div class='category_box'>";

        $content .= "<h2>$categoryName</h2>";

        $content .= "<table style='width : 100%; text-align: center;'>\n";
        $content .= "<tr>\n";
        $content .= "<th>$labelName</th><th>$labelRank</th><th>$labelPoints</th><th>$labelParticipants</th>\n";
        $content .= "</tr>\n";

        foreach ($tournamentEntries as $tournamentEntry) {
            $content .= TournamentEntryFormatter::getTournamentLine($tournamentEntry);
        }
        $content .= "</table>";

        $content .= "</div>";

        return $content;
    }

    public static function getFullUser(Category $category, Member $user) {
        $content = "";
        $categoryName = $category->getName();

        $tournamentEntries = TournamentEntryLister::GetAllEntriesFromMemberAndCategory($user, $category);

        usort($tournamentEntries, "sortByDescPoints");

        $labelName = Localization::tournamentName;
        $labelPoints = Localization::tournamentPoints;
        $labelDate = Localization::tournamentDate;
        $labelRank = Localization::tournamentRank;
        $labelParticipants = Localization::tournamentParticipants;

        $content .= "<div class='category_box'>";

        $content .= "<h2>$categoryName</h2>";

        $content .= "<table style='width : 100%; text-align: center;'>\n";
        $content .= "<tr>\n";
        $content .= "<th>$labelName</th><th>$labelDate</th><th>$labelRank</th>\n";
        $content .= "<th>$labelPoints</th><th>$labelParticipants</th>\n";
        $content .= "</tr>\n";

        foreach ($tournamentEntries as $tournamentEntry) {
            $content .= TournamentEntryFormatter::getUserLine($tournamentEntry);
        }
        $content .= "</table>";

        $content .= "</div>";

        return $content;
    }
}

function sortByDescPoints($a, $b) {
    if ($a->getPoints() == $b->getPoints()) {
        return 0;
    }
    if ($a->getPoints() < $b->getPoints()) {
        return 1;
    } else {
        return -1;
    }
}
?>
