<?php
include_once '../objects/db_table_object.php';
include_once '../objects/db_triggerevent.php';

class ReactEvent extends DBTableObject
{
    // database connection and table name
    //private $conn;
    protected $table_name = REACTEVENTS_TABLE_NAME;
    protected $array_key = "reactevent";
	
	// object properties
    public int $id = -1;
    public int $srcEventIndex = -1;
    public int $srcEventSessionId = -1;
    public int $reactType = -1;
    public int $reactMode = -1;
    public int $hitUserId = -1;
	public string $hitTargetName = "";
	public string $hitBoneName = "";
    public float $damage = 0.0;
    public int $targetKilled = 0;
    public float$objectHitLocationX = 0.0;
    public float $objectHitLocationY = 0.0;
    public string $objectHitTagLocation = "";
	public float $hitPrecision = 0.0;
	public float $distance = 0.0;
    public float $reactTime = 0.0;
    public float $timeStamp = 0.0;
	
    
	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	public function toArray () : array
	{
		return array (
			"id" => (int)$this->id,
			"srcEventIndex" => (int)$this->srcEventIndex,
			"srcEventSessionId" => (int)$this->srcEventSessionId,
			"reactTypeAsInt" => (int)$this->reactType,
			"hitUserId" => (int)$this->hitUserId,
			"hitTargetName" => $this->hitTargetName ?? "",
			"hitBoneName" => $this->hitBoneName ?? "",
			"damage" => (float)$this->damage ?? 0.0,
			"targetKilled" => ($this->targetKilled ?? 0) == 1 ? true : false,
			"objectHitLocationX" => (float)$this->objectHitLocationX ?? 0.0,
			"objectHitLocationY" => (float)$this->objectHitLocationY ?? 0.0,
			"objectHitTagLocation" => $this->objectHitTagLocation ?? "",
			"hitPrecision" => (float)$this->hitPrecision ?? 0.0,
			"timestamp" => (float)$this->timeStamp ?? 0.0,
			"distance" => (float)$this->distance ?? 0.0,
            "reactTime" => (float)$this->reactTime ?? 0.0,
			"reactModeAsInt" => (int)$this->reactMode ?? 0
		);
	}
	
	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    function sanitize ()
    {
        $this->hitUserId=htmlspecialchars(strip_tags($this->hitUserId));
        $this->srcEventIndex=htmlspecialchars(strip_tags($this->srcEventIndex));
        $this->srcEventSessionId=htmlspecialchars(strip_tags($this->srcEventSessionId));
        $this->reactType=htmlspecialchars(strip_tags($this->reactType));
        $this->hitTargetName=htmlspecialchars(strip_tags($this->hitTargetName));
        $this->hitBoneName=htmlspecialchars(strip_tags($this->hitBoneName));
        $this->damage=htmlspecialchars(strip_tags($this->damage));
        $this->targetKilled=htmlspecialchars(strip_tags($this->targetKilled));
        $this->objectHitLocationX=htmlspecialchars(strip_tags($this->objectHitLocationX));
        $this->objectHitLocationY=htmlspecialchars(strip_tags($this->objectHitLocationY));
        $this->objectHitTagLocation=htmlspecialchars(strip_tags($this->objectHitTagLocation));
        $this->hitPrecision=htmlspecialchars(strip_tags($this->hitPrecision));
        $this->distance=htmlspecialchars(strip_tags($this->distance));
        $this->reactTime=htmlspecialchars(strip_tags($this->reactTime));
        $this->reactMode=htmlspecialchars(strip_tags($this->reactMode));
        $this->timeStamp=htmlspecialchars(strip_tags($this->timeStamp));
    }

	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	function record ()
	{
        $canAddReactEvent = true;

        // check that given srcEventId exists, otherwise create it
        $query = "SELECT T.indexCount, T.sessionId FROM " . TRIGGEREVENTS_TABLE_NAME . " T WHERE T.indexCount=" . $this->srcEventIndex . " AND T.sessionId=" . $this->srcEventSessionId;
        // prepare query
        $stmt = $this->conn->prepare($query);
        // execute query
        $stmt->execute();

        if($stmt->rowCount() == 0)
        {
            $canAddReactEvent = false;

            $event = new TriggerEvent($this->conn);
            $event->indexCount = $this->srcEventIndex;
            $event->sessionId = $this->srcEventSessionId;
            $event->srcUserId = 0;
            $event->type = 0;   // Fire
            $event->successful = 1;
            $event->timeStamp = $this->timeStamp;

            if ($event->recordFromReact())
                $canAddReactEvent = true;
        }

        if ($canAddReactEvent)
        {
            // query to insert record of new user signup
            $query = "INSERT INTO " . $this->table_name . "
                    SET srcEventIndex=:srcEventIndex, srcEventSessionId=:srcEventSessionId, reactType=:reactType, reactMode=:reactMode, " . "
                    hitUserId=:hitUserId, hitTargetName=:hitTargetName, hitBoneName=:hitBoneName, damage=:damage, targetKilled=:targetKilled, " . "
                    objectHitLocationX=:objectHitLocationX, objectHitLocationY=:objectHitLocationY, objectHitTagLocation=:objectHitTagLocation, " . "
                    hitPrecision=:hitPrecision, distance=:distance, timeStamp=:timeStamp, reactTime=:reactTime";
            
            // prepare query
            $stmt = $this->conn->prepare($query);
            
            // sanitize
            $this->sanitize();
            
            // bind values
            $stmt->bindParam(":hitUserId", $this->hitUserId);
            $stmt->bindParam(":srcEventIndex", $this->srcEventIndex);
            $stmt->bindParam(":srcEventSessionId", $this->srcEventSessionId);
            $stmt->bindParam(":reactType", $this->reactType);
            $stmt->bindParam(":hitTargetName", $this->hitTargetName);
            $stmt->bindParam(":hitBoneName", $this->hitBoneName);
            $stmt->bindParam(":damage", $this->damage);
            $stmt->bindParam(":targetKilled", $this->targetKilled);
            $stmt->bindParam(":objectHitTagLocation", $this->objectHitTagLocation);
            $stmt->bindParam(":objectHitLocationX", $this->objectHitLocationX);
            $stmt->bindParam(":objectHitLocationY", $this->objectHitLocationY);
            $stmt->bindParam(":hitPrecision", $this->hitPrecision);
            $stmt->bindParam(":distance", $this->distance);
            $stmt->bindParam(":reactTime", $this->reactTime);
            $stmt->bindParam(":reactMode", $this->reactMode);
            $stmt->bindParam(":timeStamp", $this->timeStamp);
		
            // execute query
            if ($stmt->execute())
            {
                $this->id = $this->conn->lastInsertId();
                return true;
            }
            
            return false;
        }
    }
}
?>