<?php
    /**
     * Monster view.
     *
     * Contains the view layout and some code to present the data.
     *
     * @category View
     * @property_read Monster_Page $page The page model.
     * @property_read int $UID Player ID.
     */

    namespace swdb;
?>
<!DOCTYPE html>
<html lang='en'>
    <head>
        <meta content='text/html; charset=utf-8' http-equiv='content-type'/>
        <meta name='viewport' content='width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1'/>
        <title><?=$page->title?></title>
        <link rel='shortcut icon' href='<?=$page->favicon?>'/>
        <!-- CSS files -->
        <link rel='stylesheet' type='text/css' href='<?=URL::CSS?>ui.css'/>
        <link rel='stylesheet' type='text/css' href='<?=URL::CSS?>monster.css'/>
        <!-- Meta tags -->
        <link rel='canonical' href='<?=$page->canonical?>'/>
        <link rel='author' href='<?=$page->author?>'/>
        <link rel='publisher' href='<?=$page->author?>'/>
        <meta name='description' content='<?=$page->description?>'/>
        <meta property='og:title' content='<?=$page->title?>'/>
        <meta property='og:url' content='<?=$page->canonical?>'/>
        <meta property='og:description' content='<?=$page->description?>'/>
        <meta property='og:image' content='<?=$page->icon?>'/>
        <meta property='og:site_name' content='<?=$page->name?>'/>
        <meta property='og:type' content='website'/>
        <meta property='og:locale' content='en'/>
        <meta name='twitter:card' content='summary'/>
        <meta name='twitter:title' content='<?=$page->title?>'/>
        <meta name='twitter:description' content='<?=$page->description?>'/>
        <meta name='twitter:image' content='<?=$page->icon?>'/>
        <meta name='twitter:url' content='<?=$page->canonical?>'/>
        <meta name='robots' content='index follow'/>
    </head>
    <body>
<?php
        include __DIR__ . "/inc/header.php";
?>
        <section id='monster' class='content'>
            <h2>
                <span>
                    <?=$page->unit->title?>
                </span>
            </h2>
            <article id='profile'>
                <div id='monster_info'>
                    <div class='monster_panel'>
                        <?=HTML::unit_panel($page->unit)?>
                    </div>
<?php
                    if ($page->unit->experience_level != 0){
                        $exp_pct = ceil($page->unit->exp_gained * 100 / $page->unit->experience_level);
                        $exp_txt = $page->unit->exp_gained . " / " . $page->unit->experience_level;
                    }
                    else{
                        $exp_pct = 100;
                        $exp_txt = "MAX";
                    }
                    $now = time();
                    //$datediff = $now - $page->unit->create_time->getTimestamp();
                    $datediff = $now - strtotime($page->unit->create_time->format('Y-m-d H:i:s'));
                    $datediff = floor($datediff / (60 * 60 * 24));
?>
                    <div id='monster_details'>
                        <div id='exp'>
                            <span id='exp_bar' style='width:<?=$exp_pct?>%;'>
                            </span>
                            <span id='exp_text'>
                                <?=$exp_txt?>
                            </span>
                        </div>
                        Obtained on <?=$page->unit->create_time->format('Y-m-d')?> (<?=$datediff?> days ago)<!-- from <?=$page->unit->source->name?> TODO SOurce name, days ago -->
<?php
                        if (sizeof($page->unit->teams) > 0){
?>
                            <div id='teams'>
                                <h4>
                                    Teams:
                                </h4>
                                <ul>
<?php
                                    foreach($page->unit->teams as $team){
?>
                                        <li>
                                            <a target='blank' href='/<?=$UID?>/teams/<?=$team->id?>'>
                                                <?=$team->name?>
                                            </a>
                                        </li>
<?php
                                    }
?>
                                </ul>
                            </div>
<?php
                        }
?>
                    </div>
                </div>
<?php
                $building_attack = floor(($page->unit->attack * $page->building_stats["atk"] / 100) + ($page->unit->attack * $page->building_stats["elm"][$page->unit->unit->element] / 100));
                $building_defense = floor($page->building_stats["def"] * $page->unit->defense / 100);
                $building_hp = floor($page->building_stats["hp"] * $page->unit->hp / 100);
                $building_speed = $page->building_stats["spd"];
                $building_crit_rate = 0;
                $building_crit_damage = $page->building_stats["crd"];
                $building_accuracy = 0;
                $building_resistance = 0;
?>
                <table id='stats'>
                    <tr>
                        <th>
                            Stats
                        </th>
                        <th>
                            Base
                        </th>
                        <th>
                            Runes
                        </th>
                        <th>
                            Buildings
                        </th>
                        <th>
                            Total
                        </th>
                    </tr>
                    <tr>
                        <th>
                            ATK
                        </th>
                        <td class='base'>
                            <?=$page->unit->attack?>
                        </td>
                        <td class='extra'>
                            +<?=$page->unit->rune_attack?>
                        </td>
                        <td class='extra'>
                            +<?=$building_attack?>
                        </td>
                        <td class='total'>
                            <?=$page->unit->attack + $page->unit->rune_attack + $building_attack?>
                        </td>
                    </tr>
                    <tr>
                        <th>
                            DEF
                        </th>
                        <td class='base'>
                            <?=$page->unit->defense?>
                        </td>
                        <td class='extra'>
                            +<?=$page->unit->rune_defense?>
                        </td>
                        <td class='extra'>
                            +<?=$building_defense?>
                        </td>
                        <td class='total'>
                            <?=$page->unit->defense + $page->unit->rune_defense + $building_defense?>
                        </td>
                    </tr>
                    <tr>
                        <th>
                            HP
                        </th>
                        <td class='base'>
                            <?=$page->unit->hp?>
                        </td>
                        <td class='extra'>
                            +<?=$page->unit->rune_hp?>
                        </td>
                        <td class='extra'>
                            +<?=$building_hp?>
                        </td>
                        <td class='total'>
                            <?=$page->unit->hp + $page->unit->rune_hp + $building_hp?>
                        </td>
                    </tr>
                    <tr>
                        <th>
                            SPD
                        </th>
                        <td class='base'>
                            <?=$page->unit->speed?>
                        </td>
                        <td class='extra'>
                            +<?=$page->unit->rune_speed?>
                        </td>
                        <td class='extra'>
                            +<?=$building_speed?>
                        </td>
                        <td class='total'>
                            <?=$page->unit->speed + $page->unit->rune_speed + $building_speed?>
                        </td>
                    </tr>
                    <tr>
                        <th>
                            CRR
                        </th>
                        <td class='base'>
                            <?=$page->unit->crit_rate?>%
                        </td>
                        <td class='extra'>
                            +<?=$page->unit->rune_crit_rate?>%
                        </td>
                        <td class='extra'>
                            +<?=$building_crit_rate?>%
                        </td>
                        <td class='total'>
                            <?=$page->unit->crit_rate + $page->unit->rune_crit_rate + $building_crit_rate?>%
                        </td>
                    </tr>
                    <tr>
                        <th>
                            CRD
                        </th>
                        <td class='base'>
                            <?=$page->unit->crit_damage?>%
                        </td>
                        <td class='extra'>
                            +<?=$page->unit->rune_crit_damage?>%
                        </td>
                        <td class='extra'>
                            +<?=$building_crit_damage?>%
                        </td>
                        <td class='total'>
                            <?=$page->unit->crit_damage + $page->unit->rune_crit_damage + $building_crit_damage?>%
                        </td>
                    </tr>
                    <tr>
                        <th>
                            ACC
                        </th>
                        <td class='base'>
                            <?=$page->unit->accuracy?>%
                        </td>
                        <td class='extra'>
                            +<?=$page->unit->rune_accuracy?>%
                        </td>
                        <td class='extra'>
                            +<?=$building_accuracy?>%
                        </td>
                        <td class='total'>
                            <?=$page->unit->accuracy + $page->unit->rune_accuracy + $building_accuracy?>%
                        </td>
                    </tr>
                    <tr>
                        <th>
                            RES
                        </th>
                        <td class='base'>
                            <?=$page->unit->resistance?>%
                        </td>
                        <td class='extra'>
                            +<?=$page->unit->rune_resistance?>%
                        </td>
                        <td class='extra'>
                            +<?=$building_resistance?>%
                        </td>
                        <td class='total'>
                            <?=$page->unit->resistance + $page->unit->rune_resistance + $building_resistance?>%
                        </td>
                    </tr>
                </table>
                <table id='skills'>
                    <tr>
<?php
                        $colspan = sizeof($page->unit->unit->skill);
                        if ($page->unit->unit->leader_skill){
                            $colspan ++;
                        }
?>
                        <th colspan='<?=$colspan?>'>
                            Skills
                        </th>
                    </tr>
                    <tr>
<?php
                        if ($page->unit->unit->leader_skill){
?>
                            <td class='skill'>
                                <img class='skill_img' src='<?=$page->unit->unit->leader_skill->get_image()?>'/>
                                <span class='skill_level'>
                                    &nbsp;
                                </span>
                            </td>
<?php
                        }
                        $skill_num = 0;
                        $absolute_level = 0;
                        $absolute_max_level = 0;
                        $ponderated_level = 0;
                        $ponderated_max_level = 0;
                        foreach ($page->unit->unit->skill as $skill){
                            if ($skill->max_level > 1){
                                $absolute_max_level += $skill->max_level - 1;
                            }
                            $cur_lvl = $page->unit->skill_levels[$skill_num];
                            if ($skill->max_level > 1){
                                $absolute_level += $cur_lvl - 1;
                                $ponderated_level += ($cur_lvl - 1) * (1 + ($skill_num / 2));
                                $ponderated_max_level += ($skill->max_level - 1) * (1 + ($skill_num / 2));
                            }
                            $max_lvl = $skill->max_level;
                            if ($max_lvl == $cur_lvl){
                                $maxed = "maxed";
                            }
                            else{
                                $maxed = "";
                            }
                            $skill_num ++;
?>
                            <td class='skill'>
                                <img class='skill_img' src='<?=$skill->get_image()?>'/>
                                <span class='skill_level <?=$maxed?>'>
                                    <?=$cur_lvl?>/<?=$max_lvl?>
                                </span>
                            </td>
<?php
                        }
?>
                    </tr>
                    <tr>
<?php
                        if ($page->unit->unit->leader_skill){
?>
                            <td>
                                <span class='skill_name'>
                                    Leader skill
                                </span>
                                <span class='skill_description'>
                                    <?=$page->unit->unit->leader_skill->create_description()?>
                                </span>
                            </td>
<?php
                        }
                        foreach ($page->unit->unit->skill as $skill){
?>
                            <td>
                                <span class='skill_name'>
                                    <?=$skill->name?>
                                </span>
                                <span class='skill_description'>
                                    <?=$skill->description?>
                                </span>
                            </td>
<?php
                        }
?>
                    </tr>
                    <tr>
<?php
                        if ($page->unit->unit->leader_skill){
?>
                            <td></td>
<?php
                        }
                        foreach ($page->unit->unit->skill as $skill){
?>
                            <td>
                                <span class='skill_formula'>
                                    <?=$skill->multiplier_formula?>
                                </span>
                            </td>
<?php
                        }
?>
                    </tr>
                    <tr>
<?php
                        if ($page->unit->unit->leader_skill){
?>
                            <td></td>
<?php
                        }
                        foreach ($page->unit->unit->skill as $skill){
?>
                            <td>
                                <span class='skill_effects'>
<?php
                                    foreach ($skill->effect as $effect){
?>
                                        <img class='effect_img' title='<?=$effect->name?>' src='<?=$effect->get_image()?>'/>
<?php
                                    }
?>
                                </span>
                            </td>
<?php
                        }
?>
                    </tr>
                    <tr>
<?php
                        if ($page->unit->unit->leader_skill){
?>
                            <td></td>
<?php
                        }
                        foreach ($page->unit->unit->skill as $skill){
?>
                            <td class='skill_up'>
<?php
                                foreach ($skill->level as $level){
                                    if ($level->level > 1){
                                        $aquired = "";
                                        if ($level->level <= $cur_lvl){
                                            $aquired = "aquired";
                                        }
?>
                                        <span class='level <?=$aquired?>'>
                                            Lv. <?=$level->level?>
                                        </span>
                                        <span class='description <?=$aquired?>'>
                                            <?=$level->description?>
                                        </span>
<?php
                                    }
                                }
?>
                            </td>
<?php
                        }
?>
                    </tr>
                    <tr>
                        <td colspan='<?=$colspan?>' id='skill_averages'>
                            Absolute skill-ups: <?=number_format($absolute_level * 100 / $absolute_max_level, 2)?>%
                            <br/>
                            Ponderated skill-ups: <?=number_format($ponderated_level * 100 / $ponderated_max_level, 2)?>%
                        </td>
                    </tr>
                </table>
            </article> <!-- #profile -->
        </section>
        <section id='runes' class='list'>
            <h2>
                <span>
                    Runes
                </span>
            </h2>
<?php
            $total_efficiency = 0;
            $total_max_efficiency = 0;
            $total_reached_efficiency = 0;
            $total_level = 0;
            $total_stars = 0;
            $total_value = 0;
            $total_runes = 0;
            foreach ($page->unit->rune as $rune){
                $total_efficiency += $rune->efficiency;
                $total_max_efficiency += $rune->max_efficiency;
                $total_reached_efficiency += ($rune->efficiency * 100 / $rune->max_efficiency);
                $total_level += $rune->level;
                $total_stars += $rune->stars;
                $total_value += $rune->value;
                $total_runes ++;
?>
                <article>
                    <?=HTML::rune_table($rune)?>
                </article>
<?php
            }
?>
            <article id='rune_averages'>
                <table id='table_averages'>
                    <tr>
                        <td class='title'>
                            Avg. level:
                        </td>
                        <td class='value'>
                            <?=number_format($total_level / $total_runes, 2)?>
                        </td>
                    </tr>
                    <tr>
                        <td class='title'>
                            Avg. stars:
                        </td>
                        <td class='value'>
                            <?=number_format($total_stars / $total_runes, 2)?>
                        </td>
                    </tr>
                    <tr>
                        <td class='title'>
                            Avg. value:
                        </td>
                        <td class='value'>
                            <?=number_format($total_value / $total_runes, 2, '.', '')?>
                        </td>
                    </tr>
                    <tr>
                        <td class='title'>
                            Total value:
                        </td>
                        <td class='value'>
                            <?=$total_value?>
                        </td>
                    </tr>
                    <tr>
                        <td class='title'>
                            Avg. efficiency:
                        </td>
                        <td class='value'>
                            <?=number_format($total_efficiency / $total_runes, 2)?>%
                        </td>
                    </tr>
                    <tr>
                        <td class='title'>
                            Avg. max. efficiency:
                        </td>
                        <td class='value'>
                            <?=number_format($total_max_efficiency / $total_runes, 2)?>%
                        </td>
                    </tr>
                    <tr>
                        <td class='title'>
                            Avg. reached efficiency:
                        </td>
                        <td class='value'>
                            <?=number_format($total_reached_efficiency / $total_runes, 2)?>%
                        </td>
                    </tr>
                </table>
            </article>
        </section> <!-- runes -->

<?php
        include __DIR__ . "/inc/footer.php";
?>
    </body>
</html>

