<?php

    /**
     * Generates a monster panel.
     * 
     * @param unit Unit/K_Unit Entity to get the info from.
     * @return string HTML content for a monster panel. Empty on error.
     */
    function html_unit_panel($unit) {
        global $path;
        if ($unit instanceof Unit){
            $k = $unit->unit;
            $stars = $unit->stars;
            $level = true;
        }
        elseif ($unit instanceof K_Unit){
            $k = $unit;
            $stars = $unit->base_stars;
            $level = false;
        }
        else{
            return "";
        }
        $html = "";
        if ($k->awakens_from == "" && $k->awakens_to == ""){
            // No to, no from, silver monster.
            $star_class ="star_silver";
        }
        elseif ($k->awakens_from != ""){
            // Already awakened
            if ($k->base_stars - $k->natural_stars == 1){
                // Normal awaken.
                $star_class ="star_purple";
            }
            else{
                // Second awaken.
                $star_class ="star_red";
            }
        }
        elseif ($k->awakens_to != ""){
            // Awakeable.
            $star_class ="star_gold";
        }
        $html .= "<span class='stars'>\n";
        for ($i = 0; $i < $stars; $i ++){
            $html .= "<img class='star $star_class' src='" . $path["img"]["icon"] . "star.png'/>\n";
        }
        $html .= "</span>\n";
        $html .= "<img title='" . $unit->title . "' class='monster border_" . $k->element_name . "' src='" . $k->get_image() . "'/>\n";
        if ($level){
            $html .= "<span class='level'>\n";
            $html .= $unit->level . "\n";
            $html .= "</span>\n";
        }
        return $html;
    }

    /**
     * Generates a rune panel.
     * 
     * @param Rune rune Entity to get the info from.
     * @param Unit unit Optional. The unit the rune is assigned to.
     * @return string HTML content for a rune panel. Empty on error.
     */
    function html_rune_panel($rune, $unit = null) {
        global $path;
        if (!($rune instanceof Rune)){
            return "";
        }
        $html = "";
        $html .= "<div class='rune_panel rune_slot_" . $rune->slot . " rune_level_" . $rune->level . " rune_quality_" . strtolower($rune->quality_name) . " rune_original_quality_" . strtolower($rune->original_quality_name) . " rune_ancient_" . $rune->ancient . "'>\n";
        $html .= "<img class='rune_base' src='" . $path["img"]["rune"] . "base.png'/>\n";
        $html .= "<img class='rune_icon' src='" . $rune->type->get_image() . "'/>\n";
        $html .= "<span class='rune_stars'>\n";
        for ($i = 0; $i < $rune->stars; $i ++){
            $html .= "<img class='star' src='" . $path["img"]["icon"] . "star.png'/>\n";
        }
        $html .= "</span>\n";
        $html .= "<span class='rune_level'>\n";
        $html .= $rune->level . "\n";
        $html .= "</span>\n";
        $html .= "</div>\n";
        if ($unit instanceof Unit){
            $html .= "<div class='assigned'>\n";
            $html .= "<a target='blank' href='/monsters/" . $unit->id . "'>\n";
            $html .= "<img title='" . $unit->title . "' class='monster_image border_" . $unit->unit->element_name . "' src='" . $unit->unit->get_image() . "'/>\n";
            $html .= "</a>\n";
            $html .= "</div>\n";
        }
        return $html;
    }

    /**
     * Generates a rune table.
     * 
     * @param Rune rune Entity to get the info from.
     * @param Unit unit Optional. The unit the rune is assigned to.
     * @return string HTML content for a rune table. Empty on error.
     */
    function html_rune_table($rune, $unit = null) {
        global $path;
        if (!($rune instanceof Rune)){
            return "";
        }
        $html = "";
        $html .= "<table class='rune'>\n";
        $html .= "<tr>\n";
        $html .= "<td class='icon'>\n";
        $html .= html_rune_panel($rune, $unit) . "\n";
        $html .= "</td>\n";
        $html .= "<td class='stats'>\n";
        $html .= "<table>\n";
        // Main stat
        $html .= "<tr class='main_stat'>\n";
        $html .= "<td class='stat'>\n";
        if (in_array($rune->main_stat_name, array("CRR", "CRD", "RES", "ACC", "HP%", "ATK%", "DEF%"))){
            $stat_name = str_replace("%", "", $rune->main_stat_name);
            $stat_value = "+ " . $rune->main_stat_value . "%";
        }
        else{
            $stat_name = $rune->main_stat_name;
            $stat_value = "+ " . $rune->main_stat_value;
        }
        $html .= $stat_name . "\n";
        $html .= "</td>\n";
        $html .= "<td class='value'>\n";
        $html .= $stat_value . "\n";
        $html .= "<td>\n";
        $html .= "</tr>\n";
        // Innate stat
        $html .= "<tr class='innate_stat'>\n";
        $html .= "<td class='stat'>\n";
        if (strlen($rune->innate_stat) == 0){
            $stat_name = "&nbsp;";
            $stat_value = "";
        }
        elseif (in_array($rune->innate_stat_name, array("CRR", "CRD", "RES", "ACC", "HP%", "ATK%", "DEF%"))){
            $stat_name = str_replace("%", "", $rune->innate_stat_name);
            $stat_value = "+ " . $rune->innate_stat_value . "%";
        }
        else{
            $stat_name = $rune->innate_stat_name;
            $stat_value = "+ " . $rune->innate_stat_value;
        }
        $html .= $stat_name . "\n";
        $html .= "</td>\n";
        $html .= "<td class='value'>\n";
        $html .= $stat_value . "\n";
        $html .= "<td>\n";
        $html .= "</tr>\n";
        $substats = array($rune->substat_1, $rune->substat_2, $rune->substat_3, $rune->substat_4);
        $substats_name = array($rune->substat_1_name, $rune->substat_2_name, $rune->substat_3_name, $rune->substat_4_name);
        $substats_value = array($rune->substat_1_value, $rune->substat_2_value, $rune->substat_3_value, $rune->substat_4_value);
        $substats_grind = array($rune->substat_1_grind, $rune->substat_2_grind, $rune->substat_3_grind, $rune->substat_4_grind);
        $substats_enchant = array($rune->substat_1_enchant, $rune->substat_2_enchant, $rune->substat_3_enchant, $rune->substat_4_enchant);
        for ($i = 0; $i < 4; $i ++){
            if (strlen($substats_name[$i]) == 0){
                $stat_name = "&nbsp;";
                $stat_value = "";
            }
            elseif (in_array($substats_name[$i], array("CRR", "CRD", "RES", "ACC", "HP%", "ATK%", "DEF%"))){
                $stat_name = str_replace("%", "", $substats_name[$i]);
                $stat_value = "+ " . $substats_value[$i] . "%";
            }
            else{
                $stat_name = $substats_name[$i];
                $stat_value = "+ " . $substats_value[$i];
            }
            $grind = "";
            if ($substats_grind[$i] > 0){
                $grind = "+" . $substats_grind[$i];
                if (in_array($substats_name[$i], array("CRR", "CRD", "RES", "ACC", "HP%", "ATK%", "DEF%"))){
                    $grind = $grind . "%";
                }
            }
            $html .= "<tr class=substat>\n";
            $html .= "<td class='stat'>\n";
            $html .= $stat_name . "\n";
            $html .= "</td>\n";
            $html .= "<td class='value'>\n";
            $html .= $stat_value . "\n";
            $html .= "<span class='grind grind_$grind'>\n";
            $html .= $grind . "\n";
            $html .= "</span>\n";
            if ($substats_enchant[$i] == 1){
                $html .= "<img title='Enchanted' src='" . $path["img"]["rune"] . "enchanted.png'/>\n";
            }
            $html .= "</td>\n";
            $html .= "</tr>\n";
        }
        $html .= "</table>\n";
        $html .= "</td>\n";
        // Details
        $html .= "<td class='details'>\n";
        $html .= "<table class='table_details'>\n";
        $html .= "<tr>\n";
        $html .= "<td class='title'>\n";
        $html .= "Quality:\n";
        $html .= "</td>\n";
        $html .= "<td class='value'>\n";
        $html .= "<span class='quality quality_" . strtolower($rune->quality_name) . "'>\n";
        $html .= $rune->quality_name . "\n";
        $html .= "</span>\n";
        $html .= "</td>\n";
        $html .= "</tr>\n";
        $html .= "<tr>\n";
        $html .= "<td class='title'>\n";
        $html .= "Origina q.:\n";
        $html .= "</td>\n";
        $html .= "<td class='value'>\n";
        $html .= "<span class='quality quality_" . strtolower($rune->original_quality_name) . "'>\n";
        $html .= $rune->original_quality_name . "\n";
        $html .= "</span>\n";
        $html .= "</td>\n";
        $html .= "</tr>\n";
        $html .= "<tr>\n";
        $html .= "<td class='title'>\n";
        $html .= "Value:\n";
        $html .= "</td>\n";
        $html .= "<td class='value'>\n";
        $html .= number_format($rune->value, 0, ".", ",") . "\n";
        $html .= "</td>\n";
        $html .= "</tr>\n";
        $html .= "<tr>\n";
        $html .= "<td class='title'>\n";
        $html .= "Efficiency:\n";
        $html .= "</td>\n";
        $html .= "<td class='value'>\n";
        $html .= number_format($rune->efficiency, 2, ".", ",") . "%\n";
        $html .= "</td>\n";
        $html .= "</tr>\n";
        $html .= "<tr>\n";
        $html .= "<td class='title'>\n";
        $html .= "Max. eff.:\n";
        $html .= "</td>\n";
        $html .= "<td class='value'>\n";
        $html .= number_format($rune->max_efficiency, 2, ".", ",") . "%\n";
        $html .= "</td>\n";
        $html .= "</tr>\n";
        $html .= "<tr>\n";
        $html .= "<td class='title'>\n";
        $html .= "Reached. eff.:\n";
        $html .= "</td>\n";
        $html .= "<td class='value'>\n";
        $html .= number_format($rune->efficiency * 100 / $rune->max_efficiency, 2, ".", ",") . "%\n";
        $html .= "</td>\n";
        $html .= "</tr>\n";
        $html .= "</table>\n";
        $html .= "</td>\n";
        $html .= "</tr>\n";
        $html .= "</table>\n";
        return $html;
    }

    /**
     * Generates a grindstone or gem panel.
     * 
     * @param Rune_Craft craft Entity to get the info from.
     * @return string HTML content for a rune panel. Empty on error.
     */
    function html_craft_panel($craft) {
        global $path;
        if (!($craft instanceof Rune_Craft)){
            return "";
        }
        $html = "";
        if ($craft->gem == 1){
            if ($craft->inmemorial == 1){
                $base = $path["img"]["grind"] . "gem_inmemorial.png";
            }
            else{
                $base = $path["img"]["grind"] . "gem.png";
            }
        }
        else{
            if ($craft->inmemorial == 1){
                $base = $path["img"]["grind"] . "grind_inmemorial.png";
            }
            else{
                $base = $path["img"]["grind"] . "grind.png";
            }
        }
        if ($craft->inmemorial == 0 && isset($craft->rune)){
            $icon = "<img class='craft_icon' src='" . $craft->rune->get_image() . "'/>\n";
        }
        else{
            $icon = "";
        }
        if (in_array($craft->stat_name, array("CRR", "CRD", "RES", "ACC", "HP%", "ATK%", "DEF%"))){
            $stat_name = str_replace("%", "", $craft->stat_name);
            $stat_value = "+ " . $craft->min . "~" . $craft->max . "%";
        }
        else{
            $stat_name = $craft->stat_name;
            $stat_value = "+ " . $craft->min . "~" . $craft->max;
        }
        $html .= "<div class='craft_panel craft_quality_" . strtolower($craft->quality_name) . " rune_ancient_" . $craft->ancient . "'>\n";
        $html .= "<img class='craft_base' src='" . $base . "'/>\n";
        $html .= $icon;
        $html .= "<span class='craft_stat'>\n";
        $html .= "<span class='craft_stat_name'>" . $stat_name . "</span>\n";
        $html .= "<span class='craft_stat_value'>" . $stat_value . "</span>\n";
        $html .= "</span>\n";
        $html .= "</div>\n";
        return $html;
    }

    /**
     * Generates a building panel.
     *
     * @param unit Building/Decoration/K_Building/K_Decoration Entity to get the info from.
     * @return string HTML content for a building panel. Empty on error.
     */
    function html_building_panel($building) {
        global $path;
        if ($building instanceof Building){
            $level = false;
            $title = $building->building->name;
            $description = $building->building->description;
            $img = $building->building->get_image();
        }
        elseif ($building instanceof K_Building){
            $level = false;
            $title = $building->name;
            $description = $building->building->description;
            $img = $building->get_image();
        }
        elseif ($building instanceof Decoration){
            $level = $building->level;
            $title = $building->decoration->name;
            $description = $building->decoration->description;
            $img = $building->decoration->get_image();
        }
        elseif ($building instanceof K_Decoration){
            $level = false;
            $title = $building->name;
            $description = $building->building->description;
            $img = $building->get_image();
        }
        else{
            return "";
        }
        if (strlen($description) > 0){
            $title .= ": " . $description;
        }
        $html = "";
        $html = "<div class='building_panel'>\n";
        $html .= "<img title='" . $title . "' class='building' src='" . $img . "'/>\n";
        if ($level){
            $html .= "<span class='level'>\n";
            $html .= $level . "\n";
            $html .= "</span>\n";
        }
        $html .= "</div>\n";
        return $html;
    }

    /**
     * Generates a item panel.
     *
     * @param item Inventory Entity to get the info from.
     * @return string HTML content for a building panel. Empty on error.
     */
    function html_item_panel($item) {
        global $path;
        global $INVENTORY_TYPE;
        if (!($item instanceof Inventory)){
            return "";
        }
        $html = "";
        $html = "<div class='item_panel'>\n";
        $html .= "<img title='" . $item->name . "' class='item' src='" . $item->get_image() . "'/>\n";
        $html .= "<span class='amount'>\n";
        $html .= $item->amount . "\n";
        $html .= "</span>\n";
        $html .= "</div>\n";
        return $html;
    }

?>
