 <?php
    /**
     * Urgent Skillup report page file.
     *
     * Provides a class with all the properties and methods to display the page.
     *
     * @category Page
     */

    /**
     * Require dependent files if not present.
     */
    require_once(PATH::PAGE . "Page.php");
    require_once(PATH::ENTITY . "Unit.php");

    /**
     * Urgent Skillup report page model.
     *
     * @category Page
     */
    class Report_Skillup_Page extends Page{

        /**
         * @var \Unit[] Sorted list of units.
         */
        public $units = [];

        /**
         * Constructor.
         *
         * Retrieves the data and initializes the variables.
         *
         * @param resource $db Connection to the database.
         */
        public function __construct($db){
            parent::__construct($db);
            $this->view = PATH::VIEW . "report_skillup.php";
            $this->parse_filters();
            $s_mon = $this->build_query();
            $q_mon = $this->db->query($s_mon);
            while ($r_mon = $q_mon->fetchArray(SQLITE3_ASSOC)){
                array_push($this->units, new Unit($db, $r_mon["id"]));
            }
            $this->title = "Urgent skill-ups - SWDB";
            $this->description = "Monster in need of skilling up";
            $this->canonical = URL::BASE . "report/skillups";
        }

        /**
         * Parses the request looking for the selected filters, validates them
         * and adds them to the $filters array.
         */
        private function parse_filters(){
            if (isset($_GET["min_stars"]) && intval($_GET["min_stars"]) > 0 && intval($_GET["min_stars"]) < 7){
                $this->filters["min_stars"] = $_GET["min_stars"];
            }
            if (isset($_GET["max_stars"]) && intval($_GET["max_stars"]) > 0 && intval($_GET["max_stars"]) < 7){
                $this->filters["max_stars"] = $_GET["max_stars"];
            }
            if (isset($_GET["min_natural_stars"]) && intval($_GET["min_natural_stars"]) > 0 && intval($_GET["min_natural_stars"]) < 6){
                $this->filters["min_natural_stars"] = $_GET["min_natural_stars"];
            }
            if (isset($_GET["max_natural_stars"]) && intval($_GET["max_natural_stars"]) > 0 && intval($_GET["max_natural_stars"]) < 6){
                $this->filters["max_natural_stars"] = $_GET["max_natural_stars"];
            }
            if (isset($_GET["in_storage"]) && $_GET["in_storage"] == "on"){
                $this->filters["in_storage"] = true;
            }
            if (isset($_GET["without_runes"]) && $_GET["without_runes"] == "on"){
                $this->filters["without_runes"] = true;
            }
            if (isset($_GET["family"]) && $_GET["family"] == "on"){
                $this->filters["family"] = true;
            }
            if (isset($_GET["maxed"]) && $_GET["maxed"] == "on"){
                $this->filters["maxed"] = true;
            }
            if (isset($_GET["2a"]) && $_GET["2a"] == "on"){
                $this->filters["2a"] = true;
            }
            if (isset($_GET["homunculus"]) && $_GET["homunculus"] == "on"){
                $this->filters["homunculus"] = true;
            }

            return;
        }

        /**
         * Builds the query to the rune table using the selected or default
         * filters.
         *
         * @return string The query to be executed.
         * @global int Player ID.
         */
        private function build_query(){
            global $UID;
            $s = "
              SELECT DISTINCT
                unit.id AS id,
                sum(unit_skill.level * (CAST(k_skill.slot + 1 AS FLOAT) / 2.0)) AS ponderated_lvl,
                sum(unit_skill.level) AS lvl,
                sum(k_skill.max_level * (CAST(k_skill.slot + 1 AS FLOAT) / 2.0)) AS ponderated_max_lvl,
                sum(k_skill.max_level) AS max_lvl
              FROM
                unit,
                k_unit,
                unit_skill,
                k_unit_skill,
                k_skill
              WHERE
                unit.uid = '$UID' AND
                unit.unit = k_unit.id AND
                unit_skill.unit = unit.id AND
                unit_skill.skill = k_unit_skill.skill AND
                k_unit.id = k_unit_skill.unit AND
                k_skill.id = k_unit_skill.skill AND
                k_skill.max_level > 1 AND
                unit.stars >= " . $this->filters["min_stars"] . " AND
                unit.stars <= " . $this->filters["max_stars"]
            ;
            if ($this->filters["2a"]){
                $s = $s . "
                    AND ((
                      k_unit.natural_stars >= " . $this->filters["min_natural_stars"] . " AND
                      k_unit.natural_stars <= " . $this->filters["max_natural_stars"] . "
                    ) OR (
                      k_unit.base_stars - k_unit.natural_stars > 1
                    ))
                ";
            }
            else{
                $s = $s . "
                    AND k_unit.natural_stars >= " . $this->filters["min_natural_stars"] . " AND
                    k_unit.natural_stars <= " . $this->filters["max_natural_stars"]
                ;
            }
            if (!$this->filters["in_storage"]){
                $s = $s . " AND unit.in_storage = 0 ";
            }
            if (!$this->filters["without_runes"]){
                $s = $s . " AND unit.id IN (SELECT DISTINCT assigned_to FROM rune) ";
            }
            if (!$this->filters["family"]){
                $s = $s . " AND unit.unit NOT IN (SELECT id FROM k_unit m WHERE family IN (SELECT family FROM k_unit k WHERE k.family = m.family AND k.natural_stars < m.natural_stars AND k.natural_stars < " . $this->filters["min_natural_stars"] . ")) ";
                // Special case: Eirgar (Dark Vampire lord). Nat5 can be powered with 4 star Vampires.
                if ($this->filters["min_natural_stars"] >= 5){
                    $s = $s . " AND k_unit.id <> 19114 AND k_unit.id <> 19104 ";
                }
                // Special case: Fran (Light Fairy Queen). Nat3 can be powered with 2 star Fairies.
                if ($this->filters["min_natural_stars"] >= 3){
                    $s = $s . " AND k_unit.id <> 23015 AND k_unit.id <> 23005 ";
                }
            }
            if (!$this->filters["homunculus"]){
                $s = $s . " AND k_unit.homunculus <> 1 ";
            }
            $s = $s .
              "GROUP BY unit.id ";
            if (!$this->filters["maxed"]){
                $s = $s . " HAVING ponderated_lvl < ponderated_max_lvl ";
            }
            $s = $s . "
              ORDER BY
                CAST(ponderated_lvl AS FLOAT) / CAST(ponderated_max_lvl AS FLOAT),
                unit.stars DESC,
                unit.level DESC,
                CAST(lvl AS FLOAT) / CAST(max_lvl AS FLOAT),
                max_lvl - lvl DESC;
            ";
            return $s;
        }
    }
?>

