<?php

include_once __ROOT_DIR . '/libs/php-jwt/src/BeforeValidException.php';
include_once __ROOT_DIR . '/libs/php-jwt/src/ExpiredException.php';
include_once __ROOT_DIR . '/libs/php-jwt/src/SignatureInvalidException.php';
include_once __ROOT_DIR . '/libs/php-jwt/src/JWT.php';
use \Firebase\JWT\JWT;

class OnlineController extends Controller {

    public function __construct($name, $request) {
        parent::__construct($name, $request);
    }

    // ==============
    // Actions
    // ==============

    public function processRequest()
    {
        switch ($this->request->getHttpMethod()) {
            case 'GET':
                $id = $this->request->getURIParams()[0];                
                return $this->returnUserOnline($id);

            case 'POST':
                $id = $this->request->getURIParams()[0];
                return $this->addUserOnline($id);
                break;
            
            case 'DELETE':
                $id = $this->request->getURIParams()[0];
                $data = json_decode(file_get_contents("php://input"),TRUE);
                return $this->deleteUserOnline($id,$data);
                break;
            
            case 'OPTIONS':
                return Response::okresponse(json_encode("OPTIONS"));
                            
        }
        return Response::errorResponse("unsupported parameters or method in game");
    }

    public function returnUserOnline($id)
    {
        $id = Online::returnUserOnline($id);
        $res = json_encode($id);
        return Response::okresponse($res);
    }

    public function addUserOnline($id)
    {
        Online::addUserOnline(array(":id" => $id));
        return Response::okresponse(json_encode("Online user added"));
    }

    public function deleteUserOnline($id,$data)
    {
        Online::deleteUserOnline(array(":id" => $id));
        Game::deleteUserGame(array(":GAME_ID" => $data['GAME_ID'], ':USER_ID' => $id));
        return Response::okresponse(json_encode("Online user deleted"));
    }

}