<?php

    require_once($path["entity"] . "Entity.php");
    require_once($path["entity"] . "Unit.php");
    require_once($path["entity"] . "K_Unit.php");
    require_once($path["entity"] . "Rune.php");
    require_once($path["entity"] . "Inventory.php");
    require_once($path["entity"] . "K_Area.php");
    require_once($path["entity"] . "K_Rune_Set.php");


    /**
     * A run.
     *
     * Represents an object from the table 'run'.
     */
    class Run extends Entity{

        /**
         * Player identifier.
         */
        public $uid;

        /**
         * Run identifier.
         */
        public $id;

        /**
         * Run start datestame.
         */
        public $dtime;

        /**
         * Area.
         */
        public $area;

        /**
         * Stage.
         */
        public $stage;

        /**
         * Difficulty (only scenario and dimension hole)
         */
        public $difficulty;

        /**
         * Win or lost run.
         */
        public $win;

        /**
         * Clear time.
         */
        public $time;

        /**
         * Reward mana.
         */
        public $mana;

        /**
         * Reward energy.
         */
        public $energy;

        /**
         * Reward crystal.
         */
        public $crystal;

        /**
         * Indicates if a frind/mentor helped.
         */
        public $helper;

        /**
         * Array of {@see Unit}s or {@see K_Unit}.
         * If the unit is still owned, it will be a instance of Unit.
         * If not, it will be a instance of K_Unit.
         */
        public $party = [];

        /**
         * List of {@see Inventory}, of dropped items.
         */
        public $item = [];

        /**
         * List of {@see K_Unit}s dropped.
         */
        public $unit = [];

        /**
         * List of {@see K_Unit}s whose secret dungeons were found on the run.
         */
        public $sd= [];

        /**
         * List of [{@see K_Unit}, amount] of dropped summon pieces.
         */
        public $unit_piece = [];

        /**
         * Number of shapeshifting stones dropped;
         */
        public $shapeshifting = 0;

        /**
         * List of {@see Rune}s dropped.
         */
        public $rune= [];

        /**
         * Constructor.
         *
         * Searches the database and retrieves the information about the
         * run, populating it and it's items.
         *
         * @param SQLite3 $db Connection to the database.
         * @param int $id Run identifier.
         */
        public function __construct($db, $id){
            global $path;
            global $AREA_TYPE;
            global $SCENARIO;
            global $DUNGEON;
            global $RAID_RIFT_DUNGEON;
            global $ELEMENTAL_RIFT_DUNGEON;
            parent::__construct($db);
            $s = "
                SELECT
                  id,
                  uid,
                  dtime,
                  area,
                  stage,
                  difficulty,
                  win,
                  time,
                  mana,
                  energy,
                  crystal,
                  helper
                FROM run
                WHERE id = $id;
            ";
            $q = $this->db->query($s);
            $r = $q->fetchArray(SQLITE3_ASSOC);
            $this->id = $r["id"];
            $this->uid = $r["uid"];
            $this->dtime = $r["dtime"];
            $type = 0;
            if (in_array($r["area"], $RAID_RIFT_DUNGEON) && $r["stage"] < 1){
                $type = $AREA_TYPE["RIFT_RAID_DUNGEON"];
            }
            elseif (in_array($r["area"], $SCENARIO)){
                $type = $AREA_TYPE["SCENARIO"];
            }
            elseif (in_array($r["area"], $ELEMENTAL_RIFT_DUNGEON) && $r["stage"] < 1){
                $type = $AREA_TYPE["ELEMENTAL_RIFT_DUNGEON"];
            }
            elseif (in_array($r["area"], $DUNGEON)){
                $type = $AREA_TYPE["CAIROS_DUNGEON"];
            }
            $this->area = new K_Area($this->db, $r["area"], $type);
            $this->stage = $r["stage"];
            $this->difficulty = $r["difficulty"];
            $this->win = $r["win"];
            $this->time = $r["time"];
            $this->mana = $r["mana"];
            $this->energy = $r["energy"];
            $this->crystal = $r["crystal"];
            $this->helper = $r["helper"];
            $s = "
                SELECT
                  unit,
                  k_unit,
                  (SELECT count(id) FROM unit WHERE unit.unit = k_unit) AS owned
                FROM run_party
                WHERE run = $id;
            ";
            $q = $this->db->query($s);
            while ($r = $q->fetchArray(SQLITE3_ASSOC)){
                if ($r["owned"] > 0){
                    array_push($this->party, new Unit($this->db, $r["unit"], false));
                }
                else{
                    array_push($this->party, new K_Unit($this->db, $r["k_unit"], false));
                }
            }
            $s = "
                SELECT
                  item,
                  amount
                FROM run_drop_item
                WHERE run = $this->id;
            ";
            $q = $this->db->query($s);
            while ($r = $q->fetchArray(SQLITE3_ASSOC)){
                $i = new Inventory($this->db, $r["item"]);
                $i->amount =  $r["amount"];
                array_push($this->item, $i);
            }
            $s = "
                SELECT unit
                FROM run_drop_unit
                WHERE run = $this->id;
            ";
            $q = $this->db->query($s);
            while ($r = $q->fetchArray(SQLITE3_ASSOC)){
                array_push($this->unit, new K_Unit($this->db, $r["unit"]));
            }
            $s = "
                SELECT unit
                FROM run_drop_sd
                WHERE run = $this->id;
            ";
            $q = $this->db->query($s);
            while ($r = $q->fetchArray(SQLITE3_ASSOC)){
                array_push($this->sd, new K_Unit($this->db, $r["unit"]));
            }
            $s = "
                SELECT
                  unit,
                  amount
                FROM run_drop_unit_pieces
                WHERE run = $this->id;
            ";
            $q = $this->db->query($s);
            while ($r = $q->fetchArray(SQLITE3_ASSOC)){
                $i = [
                    "unit" => new K_Unit($this->db, $r["unit"]),
                    "amount" => $r["amount"]
                ];
                array_push($this->unit_piece, $i);
            }
            $s = "
                SELECT amount
                FROM run_drop_shapeshifting
                WHERE run = $this->id;
            ";
            $q = $this->db->query($s);
            $r = $q->fetchArray(SQLITE3_ASSOC);
            if ($r){
                $this->shapeshifting = $r["amount"];
            }
            $s = "
                SELECT
                  id,
                  type,
                  slot,
                  stars,
                  ancient,
                  quality,
                  value,
                  efficiency,
                  main_stat,
                  main_stat_value,
                  innate_stat,
                  innate_stat_value,
                  substat_1,
                  substat_1_value,
                  substat_2,
                  substat_2_value,
                  substat_3,
                  substat_3_value,
                  substat_4,
                  substat_4_value
                FROM run_drop_rune
                WHERE run = $this->id;
            ";
            $q = $this->db->query($s);
            while ($r = $q->fetchArray(SQLITE3_ASSOC)){
                $rune = new Rune($this->db, $r["id"]);
                if (!isset($rune->id)){
                    $rune->id = $r["id"];
                    $rune->assigned_to = null;
                    $rune->type = new K_Rune_Set($this->db, $r["type"]);
                    $rune->slot = $r["slot"];
                    $rune->stars = $r["stars"];
                    $rune->ancient = $r["ancient"];
                    $rune->quality = $r["quality"];
                    $rune->original_quality = $r["quality"];
                    $rune->value = $r["value"];
                    $rune->value = 0;
                    $rune->efficiency = $r["efficiency"];
                    // TODO: Calculate?
                    $rune->max_efficiency = $r["efficiency"];
                    $rune->main_stat = $r["main_stat"];
                    $rune->main_stat_value = $r["main_stat_value"];
                    $rune->innate_stat = $r["innate_stat"];
                    $rune->innate_stat_value = $r["innate_stat_value"];
                    $rune->substat_1 = $r["substat_1"];
                    $rune->substat_1_value = $r["substat_1_value"];
                    $rune->substat_1_enchant = 0;
                    $rune->substat_1_grind = 0;
                    $rune->substat_2 = $r["substat_2"];
                    $rune->substat_2_value = $r["substat_2_value"];
                    $rune->substat_2_enchant = 0;
                    $rune->substat_2_grind = 0;
                    $rune->substat_3 = $r["substat_3"];
                    $rune->substat_3_value = $r["substat_3_value"];
                    $rune->substat_3_enchant = 0;
                    $rune->substat_3_grind = 0;
                    $rune->substat_4 = $r["substat_4"];
                    $rune->substat_4_value = $r["substat_4_value"];
                    $rune->substat_4_enchant = 0;
                    $rune->substat_4_grind = 0;
                    $rune->refresh_names();
                }
                array_push($this->rune, $rune);
            }
        }

    }
?>
