 <?php

    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.
     */
    class Runes_Page extends Page{

        /**
         * Array of @{see Rune}s.
         * NOTE: In this case, the monster member is not the id, but a monster
         * instance.
         */
        public $runes = [];

        /**
         * Custom filters for this page.
         */
        public $custom_filters = [];

        /**
         * The filters the page can handle.
         */
        public $filters = [
            "type" => [],
            "main" => [],
            "sub" => [],
            "slot" => [],
            "min_quality" => 0,
            "max_quality" => 4,
            "min_original_quality" => 0,
            "max_original_quality" => 4,
            "min_stars" => 1,
            "max_stars" => 6,
            "min_level" => 0,
            "max_level" => 15,
            "min_efficiency" => 0,
            "max_efficiency" => 100,
            "min_max_efficiency" => 0,
            "max_max_efficiency" => 100,
            "assigned" => 1,
            "group" => 0 // 0-> SLOT, 1->TYPE
        ];

        /**
         * Constructor.
         *
         * Retrieves the data and initializes the variables.
         *
         * @param SQLite3 $db Connection to the database.
         */
        public function __construct($db){
            global $path;
            global $root;
            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 = $root . "runes/";
        }

        /**
         * Parses the request looking for the selected filters, validates them
         * and adds them to the $filters array.
         */
        private function parse_filters(){
            global $QUALITY;
            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["NORMAL"] && intval($_GET["min_quality"]) <= $QUALITY["LEGEND"]){
                $this->filters["min_quality"] = $_GET["min_quality"];
            }
            if (isset($_GET["max_quality"]) && intval($_GET["max_quality"]) >= $QUALITY["NORMAL"] && intval($_GET["max_quality"]) <= $QUALITY["LEGEND"]){
                $this->filters["max_quality"] = $_GET["max_quality"];
            }
            if (isset($_GET["min_original_quality"]) && intval($_GET["min_original_quality"]) >= $QUALITY["NORMAL"] && intval($_GET["min_original_quality"]) <= $QUALITY["LEGEND"]){
                $this->filters["min_original_quality"] = $_GET["min_original_quality"];
            }
            if (isset($_GET["max_original_quality"]) && intval($_GET["max_original_quality"]) >= $QUALITY["NORMAL"] && intval($_GET["max_original_quality"]) <= $QUALITY["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"];
            }
            return;
        }

        /**
         * Builds the query to the rune table using the a custom filter.
         * 
         * @param int $filter Custom filter ID.
         * @return The query to be executed.
         */
        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;
            ";
            error_log("CUSTOM QUERY: " . $s);
            return $s;
        }

        /**
         * Builds the query to the rune table using the selected or default
         * filters.
         * 
         * @return The query to be executed.
         */
        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:
                    // TODO
                    //$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;
        }
    }
?>
