<?php
include_once "../objects/db_table_object.php";
include_once "../objects/db_participates.php";

class GameSession extends DBTableObject
{
    // database connection and table name
    //private $conn;
    protected $table_name = SESSIONS_TABLE_NAME;
    protected $array_key = "session";
	
	
	// object properties
    public int $id = -1;
    public $sessionType = 0;
    public string $sessionName = "";
	public $sessionDate = "";
	public string $mapName = "";
    public string $scenarioName = "";
	public $success = 0;
    public float $timeToFinish = 0.0;
	public $score = 0;
    public int $nbEnemyHit = 0;
    public int $nbCivilsHit = 0;
    public float $damageTaken = 0.0;
    public $replayFileName = "";
	

	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	public function toArray ()  : array
	{
		return array (
			"id" => (int)$this->id,
			"sessionTypeAsInt" => (int)$this->sessionType ?? 0,
			"sessionName" => $this->sessionName ?? "",
			"sessionDateAsString" => $this->sessionDate,
			"mapName" => $this->mapName ?? "",
			"scenarioName" => $this->scenarioName ?? "",
			"success" => $this->success == 1 ? true : false,
			"timeToFinish" => (float)$this->timeToFinish ?? 0.0,
			"score" => (int)$this->score ?? 0,
			"nbEnemyHit" => (int)$this->nbEnemyHit ?? 0,
			"nbCivilsHit" => (int)$this->nbCivilsHit ?? 0,
			"damageTaken" => (float)$this->damageTaken ?? 0.0,
			"replayFileName" => $this->replayFileName ?? ""
		);
	}

	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	public static function withRow ($db, array $row)
	{
        $instance = new self($db);
        $instance->readRow($row);
        return $instance;
    }
	
	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	function readRow (array $row)
	{
        $this->id = (int)$row['id'];
		$this->sessionType = $row['sessionType'];
		$this->sessionName = $row['sessionName'];
		$this->sessionDate = $row['sessionDate'];
		$this->mapName = $row['mapName'];
		$this->scenarioName = $row['scenarioName'];
		$this->success = $row['success'];
		$this->timeToFinish = (float)$row['timeToFinish'];
		$this->score = $row['score'];
		$this->nbEnemyHit = (int)$row['nbEnemyHit'];
		$this->nbCivilsHit = (int)$row['nbCivilsHit'];
		$this->damageTaken = (float)$row['damageTaken'];
		$this->replayFileName = $row['replayFileName'];
    }

	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	function load ()
	{
		// select all query with user inputed username and password
        $query = "SELECT * FROM " . $this->table_name . " WHERE id='" . $this->id . "'";
		
        // prepare query statement
        $stmt = $this->conn->prepare($query);
        // execute query
        $stmt->execute();
		
		if($stmt->rowCount() > 0)
		{
			// get retrieved row
			$row = $stmt->fetch(PDO::FETCH_ASSOC);
			// retrieve user values
            $this->readRow($row);
			return true;
		}
		
		return false;
	}
	
	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	function sanitize ()
	{
		$this->sessionType=htmlspecialchars(strip_tags($this->sessionType));
        $this->sessionName=htmlspecialchars(strip_tags($this->sessionName));
        $this->sessionDate=htmlspecialchars(strip_tags($this->sessionDate));
        $this->mapName=htmlspecialchars(strip_tags($this->mapName));
        $this->scenarioName=htmlspecialchars(strip_tags($this->scenarioName));
        $this->replayFileName=htmlspecialchars(strip_tags($this->replayFileName));
	}

	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	function sanitize_stop()
	{
		$this->success=htmlspecialchars(strip_tags($this->success));
		$this->timeToFinish=htmlspecialchars(strip_tags($this->timeToFinish));
		$this->score=htmlspecialchars(strip_tags($this->score));
		//$this->id=htmlspecialchars(strip_tags($this->id));
		//$this->nbEnemyHit=htmlspecialchars(strip_tags($this->nbEnemyHit));
		//$this->nbCivilsHit=htmlspecialchars(strip_tags($this->nbCivilsHit));
		//$this->damageTaken=htmlspecialchars(strip_tags($this->damageTaken));
		//$this->replayFileName=htmlspecialchars(strip_tags($this->replayFileName));
	}

	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	function start ()
	{
        // query to insert record of new user signup
		$query = "INSERT INTO " . $this->table_name . " 
                SET sessionType=:sessionType, sessionName=:sessionName, sessionDate=:sessionDate, mapName=:mapName, scenarioName=:scenarioName, " . "
				replayFileName=:replayFileName";
		
        // prepare query
        $stmt = $this->conn->prepare($query);
		
        // sanitize
        $this->sanitize();
		
        // bind values
        $stmt->bindParam(":sessionType", $this->sessionType);
        $stmt->bindParam(":sessionName", $this->sessionName);
        $stmt->bindParam(":sessionDate", $this->sessionDate);
        $stmt->bindParam(":mapName", $this->mapName);
        $stmt->bindParam(":scenarioName", $this->scenarioName);
        $stmt->bindParam(":replayFileName", $this->replayFileName);
		
        // execute query
        if($stmt->execute())
		{
            $this->id = $this->conn->lastInsertId();
            return true;
        }
		
        return false;
    }
	
	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	function stop ()
	{
		//if (this->load())
		{
			$query = "UPDATE " . $this->table_name . "
				SET success=:success, timeToFinish=:timeToFinish, score=:score, " . "
				nbEnemyHit=(SELECT COUNT(DISTINCT Re.srcEventIndex, Re.hitTargetName) FROM " . REACTEVENTS_TABLE_NAME . " Re WHERE Re.srcEventSessionId=:id AND Re.reactType=0), " . "
				nbCivilsHit=(SELECT COUNT(DISTINCT Rc.srcEventIndex, Rc.hitTargetName) FROM " . REACTEVENTS_TABLE_NAME . " Rc WHERE Rc.srcEventSessionId=:id AND Rc.reactType=1) " . "
				WHERE id=:id";
			
			// prepare query
			$stmt = $this->conn->prepare($query);
			
			// sanitize
			$this->sanitize_stop();
		
			// bind values
			$stmt->bindParam(":id", $this->id);
			$stmt->bindParam(":success", $this->success);
			$stmt->bindParam(":timeToFinish", $this->timeToFinish);
			$stmt->bindParam(":score", $this->score);
			//$stmt->bindParam(":nbEnemyHit", $this->nbEnemyHit);
			//$stmt->bindParam(":nbCivilsHit", $this->nbCivilsHit);
			//$stmt->bindParam(":damageTaken", $this->damageTaken);
			//$stmt->bindParam(":replayFileName", $this->replayFileName);
			
			// execute query
			return $stmt->execute();
		}
    
        return false;
	}

	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	function getUsers ()
	{
		$query = "SELECT U.* FROM " . USERS_TABLE_NAME . " U, " . PARTICIPATES_TABLE_NAME . " P WHERE U.id=P.userId AND P.sessionId =  '" . $this->id . "'";
		
        // prepare query
        $stmt = $this->conn->prepare($query);
		
        // execute query
        return $stmt->execute();
	}
}
?>