<?php

class UserController extends Controller {

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

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

    public function processRequest()
    {
         switch ($this->request->getHttpMethod()) {
            case 'GET':
                if ($this->request->getUriParams())
                    return $this->getUser($this->request->getUriParams()[0]);

                return $this->getAllUsers();
                break;
            case 'PUT':
                if ($this->request->getUriParams())
                    return $this->updateUser(array_merge($this->request->getData(),['id'=>$this->request->getUriParams()[0]]));
                break;
        }
        return Response::errorResponse("unsupported parameters or method in users");
    }

    protected function getAllUsers()
    {
        $users = User::getList();
        $response = Response::okResponse(json_encode($users));
        //var_dump($json);die;
        // TODO
        return $response;
    }

    protected function getUser($id){
        $user = User::getRow($id);
        $response = Response::okResponse(json_encode($user));
        return $response;
    }
    protected function updateUser($array){
        User::updateUser($array);
        $response = Response::okResponse('User succesfully updated !');
        return $response;
    }
}