<?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 GameController 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->getGame($id);
                break;

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

    public function getGame($id)
    {
        $game = Game::getGame($id);
        if($game == Array()){
            $response = Response::errorInParametersResponse("Game not found");
        }else{
            $response = new Response(200,json_encode($game));
        }
        return $response;
    }

    public function createGame($data)
    {
        if(array_key_exists("GAME_DESC", $data)){
            $game = Game::createGame(array( ":GAME_DESC" => $data['GAME_DESC']));
            $response = new Response(200,json_encode($game));
        }else{
            $response = Response::errorInParametersResponse("Parameters missing");
        }
        return $response;
    }

    public function updateGame($id, $data)
    {
        $gameValues = Game::getGame($id);
        $gameValues=($gameValues[0]);
        if($gameValues == []){
            $response = Response::errorResponse("Game not found");
            return $response;
        }else{
            if(array_key_exists('GAME_DESC',$data)){
                $desc = $data['GAME_DESC'];
            }else{
                $desc = $gameValues->GAME_DESC;
            }

            $game = Game::updateGame(array(":GAME_ID" => $id, ":GAME_DESC" => $desc));
            $response = new Response(200,json_encode($game));
            return $response;
        }
    }

    public function deleteGame($id)
    {
        $game = Game::getGame($id);
        if($game == Array()){
            $response = Response::errorInParametersResponse(json_encode("Game not found"));
        }else{
            $res = Game::deleteGame($id);
            $response = Response::okResponse(json_encode($res));
        }
        return $response;
    }

}