<?php

class GameController extends Controller
{
    public function __construct($name, $request)
    {
        parent::__construct($name, $request);
    }

    /**
    * Process incoming request for the /game endpoint
    *
    * @return    Response
    */
    public function processRequest()
    {
        $httpMethod=$this->request->getHttpMethod();
        $uriParams=$this->request->getUriParams();

        switch ($httpMethod) {
            case 'GET':
                // If there is a uriParams, it is the /game/{id} endpoint
                if ($uriParams) {
                    return $this->getGame($uriParams[0]);
                }
                // Else, it is the /game endpoint
                return $this->getAllGames();
                break;
        }
        $message = json_encode(["message" => "unsupported parameters or method in game"]);
        return Response::errorResponse($message);
    }

    /**
    * (GET) Get all games in Game table
    *
    * @return    Response
    */
    protected function getAllGames()
    {
        $games = Game::getList();
        $response = Response::okResponse(json_encode($games, JSON_PRETTY_PRINT));
        return $response;
    }

    /**
    * (GET) Get a specific game in Game table based 
    *
    * @return    Response
    */
    protected function getGame($id)
    {
        $games = Game::getRow($id);
        $response = Response::okResponse(json_encode($games, JSON_PRETTY_PRINT));
        return $response;
    }
}