 <?php
 
    /**
     * Rune list 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 . "Custom_Filter.php");
    require_once(PATH::ENTITY . "Rune.php");
    require_once(PATH::ENTITY . "Unit.php");

    /**
     * Rune list page model.
     *
     * @category Page
     */
    class Runes_Page extends Page{

        /**
         * @var \Rune[] Runes to show.
         *
         * In this case, the monster member is not the id, but a monster
         * instance.
         */
        public $runes = [];

        /**
         * @var \Filter[] Custom filters for this page.
         */
        public $custom_filters = [];

        /**
         * Constructor.
         *
         * Retrieves the data and initializes the variables.
         *
         * @param resource $db Connection to the database.
         * @global int Player ID.
         */
        public function __construct($db){
            global $UID;
            parent::__construct($db);
            $this->view = PATH::VIEW . "runes.php";
            $this->parse_filters();
            if (!isset($_GET["custom_filter"])){
                $s = $this->build_query();
                $this->filters["group"] = 1;
            }
            else{
                $s = $this->build_custom_query($_GET["custom_filter"]);
            }
            $q = $this->db->query($s);
            while ($r = $q->fetchArray(SQLITE3_ASSOC)){
                $rune = new Rune($db, $r["id"]);
                if (strlen($rune->assigned_to) > 0){
                    $rune->assigned_to = new Unit($this->db, $rune->assigned_to, false);
                }
                array_push($this->runes, $rune);
            }
            $s = "
              SELECT id
              FROM filter
              WHERE
                uid = '$UID' AND
                page = 'RUNES';
            ";
            $q = $this->db->query($s);
            while ($r = $q->fetchArray(SQLITE3_ASSOC)){
                array_push($this->custom_filters, new Custom_Filter($this->db, $r["id"]));
            }
            $this->title = "Runes - SWDB";
            $this->description = "Rune inventory";
            $this->canonical = URL::BASE . "runes/";
        }

        /**
         * Parses the request looking for the selected filters, validates them
         * and adds them to the $filters array.
         */
        private function parse_filters(){
            if (isset($_GET["type"])){
                $types = $_GET["type"];
                foreach ($types as $type){
                    array_push($this->filters["type"], intval($type));
                }
            }
            if (isset($_GET["main"])){
                $mains = $_GET["main"];
                foreach ($mains as $main){
                    array_push($this->filters["main"], intval($main));
                }
            }
            if (isset($_GET["sub"])){
                $subs = $_GET["sub"];
                foreach ($subs as $sub){
                    array_push($this->filters["sub"], intval($sub));
                }
            }
            if (isset($_GET["slot"])){
                $slots = $_GET["slot"];
                foreach ($slots as $slot){
                    if (intval($slot) >= 1 && intval($slot) < 7){
                        array_push($this->filters["slot"], intval($slot));
                    }
                }
            }
            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_level"]) && intval($_GET["min_level"]) >= 0 && intval($_GET["min_level"]) <= 15){
                $this->filters["min_level"] = $_GET["min_level"];
            }
            if (isset($_GET["max_level"]) && intval($_GET["max_level"]) >= 0 && intval($_GET["max_level"]) <= 15){
                $this->filters["max_level"] = $_GET["max_level"];
            }
            if (isset($_GET["min_quality"]) && intval($_GET["min_quality"]) >= QUALITY_ID::NORMAL && intval($_GET["min_quality"]) <= QUALITY_ID::LEGEND){
                $this->filters["min_quality"] = $_GET["min_quality"];
            }
            if (isset($_GET["max_quality"]) && intval($_GET["max_quality"]) >= QUALITY_ID::NORMAL && intval($_GET["max_quality"]) <= QUALITY_ID::LEGEND){
                $this->filters["max_quality"] = $_GET["max_quality"];
            }
            if (isset($_GET["min_original_quality"]) && intval($_GET["min_original_quality"]) >= QUALITY_ID::NORMAL && intval($_GET["min_original_quality"]) <= QUALITY_ID::LEGEND){
                $this->filters["min_original_quality"] = $_GET["min_original_quality"];
            }
            if (isset($_GET["max_original_quality"]) && intval($_GET["max_original_quality"]) >= QUALITY_ID::NORMAL && intval($_GET["max_original_quality"]) <= QUALITY_ID::LEGEND){
                $this->filters["max_original_quality"] = $_GET["max_original_quality"];
            }
            if (isset($_GET["min_efficiency"]) && intval($_GET["min_efficiency"]) >= 0 && intval($_GET["min_efficiency"]) <= 100){
                $this->filters["min_efficiency"] = $_GET["min_efficiency"];
            }
            if (isset($_GET["max_efficiency"]) && intval($_GET["max_efficiency"]) >= 0 && intval($_GET["max_efficiency"]) <= 100){
                $this->filters["max_efficiency"] = $_GET["max_efficiency"];
            }
            if (isset($_GET["min_max_efficiency"]) && intval($_GET["min_max_efficiency"]) >= 0 && intval($_GET["min_max_efficiency"]) <= 100){
                $this->filters["min_max_efficiency"] = $_GET["min_max_efficiency"];
            }
            if (isset($_GET["max_max_efficiency"]) && intval($_GET["max_max_efficiency"]) >= 0 && intval($_GET["max_max_efficiency"]) <= 100){
                $this->filters["max_max_efficiency"] = $_GET["max_max_efficiency"];
            }
            if (isset($_GET["assigned"]) && intval($_GET["assigned"]) >= 0 && intval($_GET["assigned"]) <= 4){
                $this->filters["assigned"] = $_GET["assigned"];
            }
            if (isset($_GET["group"]) && ($_GET["group"] == "1" || $_GET["group"] == "0")){
                $this->filters["group"] = $_GET["group"];
            }
        }

        /**
         * Builds the query to the rune table using the a custom filter.
         * 
         * @param int $filter Custom filter ID.
         * @return string The query to be executed.
         * @global int Player ID.
         */
        private function build_custom_query($filter){
            global $UID;
            $s = "
              SELECT condition
              FROM filter
              WHERE
                uid = '$UID' AND
                id = $filter;
            ";
            $q = $this->db->query($s);
            $r = $q->fetchArray(SQLITE3_ASSOC);
            $cond = $r["condition"];
            $s = "
              SELECT
                id,
                assigned_to
              FROM rune
              WHERE uid = '$UID' AND (
                $cond
              )
              ORDER BY  slot, type, stars DESC, original_quality DESC, quality DESC, level;
            ";
            return $s;
        }

        /**
         * 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 " .
              "  id, " .
              "  assigned_to " .
              "FROM rune " .
              "WHERE " .
              "  uid = '$UID' AND " .
              "  stars >= " . $this->filters["min_stars"] . " AND " .
              "  stars <= " . $this->filters["max_stars"] . " AND " .
              "  level >= " . $this->filters["min_level"] . " AND " .
              "  level <= " . $this->filters["max_level"] . " AND " .
              "  quality >= " . $this->filters["min_quality"] . " AND " .
              "  quality <= " . $this->filters["max_quality"] . " AND " .
              "  original_quality >= " . $this->filters["min_original_quality"] . " AND " .
              "  original_quality <= " . $this->filters["max_original_quality"] . " AND " .
              "  efficiency >= " . $this->filters["min_efficiency"] . " AND " .
              "  efficiency <= " . $this->filters["max_efficiency"] . " AND " .
              "  max_efficiency >= " . $this->filters["min_max_efficiency"] . " AND " .
              "  max_efficiency <= " . $this->filters["max_max_efficiency"] . " ";
            if (count($this->filters["type"]) > 0){
                $s = $s . " AND upper(type) IN ( ";
                foreach($this->filters["type"] as $t){
                    $s = $s . "'$t', ";
                }
                $s = $s . "'DUMMY0') ";
            }
            if (count($this->filters["slot"]) > 0){
                $s = $s . " AND slot IN ( ";
                foreach($this->filters["slot"] as $t){
                    $s = $s . "$t, ";
                }
                $s = $s . "-1) ";
            }
            switch ($this->filters["assigned"]){
                case 1:
                    $s = $s . " AND assigned_to IS NOT NULL ";
                    break;
                case 2:
                    $s = $s . " AND assigned_to IS NULL ";
                    break;
                case 3:
                    $s = $s . " AND (assigned_to IS NULL OR assigned_to IN (SELECT DISTINCT id FROM unit WHERE in_storage = 0)) ";
                    break;
                case 4:
                    $s = $s . " AND assigned_to IN (SELECT DISTINCT id FROM unit WHERE in_storage = 0) ";
                    break;
            }
            $s = $s . " ORDER BY ";
            if ($this->filters["group"] == 1){ // 1: Type, 0: Slot
                $s = $s . " type, slot, stars DESC, original_quality DESC, quality DESC, level;";
            }
            else{
                $s = $s . " slot, type, stars DESC, original_quality DESC, quality DESC, level, main_stat;";
            }
            return $s;
        }
    }
?>

