package io.teiler.api.endpoint;

import static spark.Spark.delete;
import static spark.Spark.exception;
import static spark.Spark.get;
import static spark.Spark.post;
import static spark.Spark.put;

import com.google.gson.Gson;
import io.teiler.api.service.PersonService;
import io.teiler.server.dto.Person;
import io.teiler.server.util.Error;
import io.teiler.server.util.GsonUtil;
import io.teiler.server.util.Normalize;
import io.teiler.server.util.exceptions.PeopleNameConflictException;
import io.teiler.server.util.exceptions.PersonNotFoundException;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

/**
 * Controller for Group-related endpoints.
 * @author lroellin
 */
@Controller
public class PersonEndpointController implements EndpointController {

    private Gson gson = GsonUtil.getHomebrewGson();

    @Autowired
    private PersonService personService;

    private static final int DEFAULT_QUERY_LIMIT = 20;
    private static final String PERSON_ID_PARAM = ":personid";
    private static final String LIMIT_PARAM = "limit";

    private static final String BASE_URL = GlobalEndpointController.URL_VERSION + "/groups/:groupid/people";
    private static final String URL_WITH_PERSON_ID = BASE_URL + "/:personid";

    @Override
    public void register() {
        post(BASE_URL, (req, res) -> {
            String groupId = req.params(GroupEndpointController.GROUP_ID_PARAM);
            groupId = Normalize.normalizeGroupId(groupId);
            Person requestPerson = gson.fromJson(req.body(), Person.class);
            Person newPerson = personService.createPerson(groupId, requestPerson.getName());
            return gson.toJson(newPerson);
        });

        get(BASE_URL, (req, res) -> {
            String groupId = req.params(GroupEndpointController.GROUP_ID_PARAM);
            groupId = Normalize.normalizeGroupId(groupId);
            String limitString = req.queryParams(LIMIT_PARAM);
            long limit = DEFAULT_QUERY_LIMIT;
            if (limitString != null) {
                limit = Long.parseLong(limitString);
            }
            List<Person> people = personService.getPeople(groupId, limit);
            return gson.toJson(people);
        });

        put(URL_WITH_PERSON_ID, (req, res) -> {
            String groupId = req.params(GroupEndpointController.GROUP_ID_PARAM);
            groupId = Normalize.normalizeGroupId(groupId);
            int personId = Integer.parseInt(req.params(PERSON_ID_PARAM));
            Person changedPerson = gson.fromJson(req.body(), Person.class);
            Person person = personService.editPerson(groupId, personId, changedPerson);
            return gson.toJson(person);
        });

        delete(URL_WITH_PERSON_ID, (req, res) -> {
            String groupId = req.params(GroupEndpointController.GROUP_ID_PARAM);
            groupId = Normalize.normalizeGroupId(groupId);
            int personId = Integer.parseInt(req.params(PERSON_ID_PARAM));
            personService.deletePerson(groupId, personId);
            return "";
        });

        exception(PersonNotFoundException.class, (e, request, response) -> {
            response.status(404);
            Error error = new Error(e.getMessage());
            response.body(gson.toJson(error));
        });

        exception(PeopleNameConflictException.class, (e, request, response) -> {
            response.status(409);
            Error error = new Error(e.getMessage());
            response.body(gson.toJson(error));
        });
    }

}

@@ -709,21 +709,22 @@
 ler for 
-Group
+Person
 -related
@@ -735,16 +735,20 @@
 points.%0A
+ * %0A
  * @auth
@@ -759,16 +759,36 @@
 roellin%0A
+ * @author pbaechli%0A
  */%0A@Con
@@ -3078,18 +3078,22 @@
 rvice.de
-le
+activa
 tePerson

add endpoint to deactivate person