<?php
    /**
     * Saved run entity file.
     *
     * Creates the entity and makes it available.
     *
     * @category Entity
     */

    /**
     * Require dependent entities if not present.
     */
    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'.
     *
     * @category Entity
     */
    class Run extends Entity{

        /**
         * @var int Player identifier.
         */
        public $uid;

        /**
         * @var int Run identifier.
         */
        public $id;

        /**
         * @var DateTime Run start datestamp.
         */
        public $dtime;

        /**
         * @var K_Area Area.
         */
        public $area;

        /**
         * @var int Stage.
         */
        public $stage;

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

        /**
         * @var bool Win or lost run.
         */
        public $win;

        /**
         * @var int Clear time, milliseconds.
         */
        public $time;

        /**
         * @var int Reward mana.
         */
        public $mana;

        /**
         * @var int Reward energy.
         */
        public $energy;

        /**
         * @var int Reward crystal.
         */
        public $crystal;

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

        /**
         * @var \Unit[]|\K_Unit[] Uits used.
         * 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 = [];

        /**
         * @var \Inventory[] Dropped items.
         */
        public $item = [];

        /**
         * @var \K_Unit[] Units dropped.
         */
        public $unit = [];

        /**
         * @var int K_Unit[] Units whose secret dungeons were found on the run.
         */
        public $sd= [];

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

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

        /**
         * @var \Rune[] List of Runes dropped.
         */
        public $rune= [];

        /**
         * Constructor.
         *
         * Searches the database and retrieves the information about the
         * run, populating it and it's items.
         *
         * @param resource $db Connection to the database.
         * @param int $id Run identifier.
         */
        public function __construct($db, $id){
            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 (property_exists(RAID_RIFT_DUNGEON_ID, $r["area"]) && $r["stage"] < 1){
                $type = AREA_TYPE::RIFT_RAID_DUNGEON;
            }
            elseif (property_exists(SCENARIO_ID, $r["area"])){
                $type = AREA_TYPE::SCENARIO;
            }
            elseif (property_exists(ELEMENTAL_RIFT_DUNGEON_ID, $r["area"]) && $r["stage"] < 1){
                $type = AREA_TYPE::ELEMENTAL_RIFT_DUNGEON;
            }
            elseif (property_exists(DUNGEON_ID, $r["area"])){
                $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);
            }
        }

    }
?>
