<?php
    class UserController extends Controller {
        public function __construct($name, $request) {
            parent::__construct($name, $request);
        }

        // ==============
        // Actions
        // ==============
        public function processRequest() {
            switch ($this->request->getHttpMethod()) {
                case 'POST':
                    return $this->createUser($_POST);
                break;
                case 'GET':
                    if (empty($this->request->getUriParameters()))
                        return $this->getAllUsers();
                    else 
                        return $this->getUserById($this->request->getUriParameters()[0]);
                break;
                case 'PUT':
                    $put = json_decode(file_get_contents("php://input"));
                    $id = $this->request->getUriParameters()[0];
                    return $this->updateUser($put, $id);
                break;
                case 'DELETE':
                    $id = $this->request->getUriParameters()[0];
                    return $this->deleteUser($id);
                break;
            }
            return Response::errorResponse("unsupported parameters or method in users");
        }

        protected function createUser($post) {
            if (isset($post['name']) && isset($post['email'])) {
                User::create($post);
                $response = Response::okResponse("Utilisateur ajouté");
            }
            else {
                $response = Response::notFoundResponse("Aucun utilisateur ajouté");
            }  

            return $response;
        }

        protected function getAllUsers() {
            $users = User::getList();

            if (!empty($users))
                $response = Response::okResponse(json_encode($users));
            else
                $response = Response::notFoundResponse("Aucune réponse");

            return $response;
        }

        protected function getUserById($id) {
            $user = User::getWithId($id);

            if (!empty($user))
                $response = Response::okResponse(json_encode($user));
            else
                $response = Response::notFoundResponse("Aucune réponse");

            return $response;
        }

        protected function updateUser($put, $id) {
            $user = User::getWithId($id);

            if (!empty($put) && !empty($user)) {
                User::update($put, $id);
                $response = Response::okResponse("Utilisateur modifié");
            }
            else {
                $response = Response::notFoundResponse("Aucun utilisateur modifié");
            }  

            return $response;
        }

        protected function deleteUser($id) {
            $user = User::getWithId($id);

            if (!empty($user)) {
                User::delete($id);
                $response = Response::okResponse("Utilisateur supprimé");
            }
            else {
                $response = Response::notFoundResponse("Aucun utilisateur supprimé");
            }  

            return $response;
        }
    }
?>