package edu.fiu.controllers;

import edu.fiu.models.Activity;
import edu.fiu.repositories.ActivityRepository;
import org.jooby.Err;
import org.jooby.Request;
import org.jooby.Status;
import org.jooby.mvc.*;

import javax.inject.Inject;

import java.util.ArrayList;
import java.util.List;

@Path("api/activity")
public class ActivityController {

    private ActivityRepository repo;
    private Request req;

    @Inject
    public ActivityController(ActivityRepository repo, Request req) {
        this.repo = repo;
        this.req = req;
    }

    @GET
    public List<Activity> List() {
        return repo.list();
    }

    @Path(":id")
    public Activity Get() {
        Integer id = new Integer(req.param("id").intValue());

        Activity act = repo.get(id);
        if (act == null)
            throw new Err(Status.NOT_FOUND);

        return act;
    }

    @POST
    public Activity Post() throws Exception {
        Activity activity = req.body(Activity.class);

        int id = repo.create(activity);
        activity.setId(new Integer(id));

        return activity;
    }

    @PUT
    public Activity Put() throws Exception {
        Activity user = req.body(Activity.class);

        if (!repo.update(user))
            throw new Err(Status.NOT_FOUND);

        return user;
    }

    @DELETE
    @Path(":id")
    public boolean delete() {
        int id = req.param("id").intValue();

        if (!repo.delete(id)) {
            throw new Err(Status.NOT_FOUND);
        }

        return true;
    }

    @POST
    @Path("/setStatus")
    public boolean setStatus() throws Exception {

        List<String> statuses = req.body().toList();

        for (int i = 0; i < statuses.size(); i++) {

            String status = statuses.get(i);
            repo.updateStatus(i, status);

            /*
            if (status == "stopped")
                repo.updateStatus(i, "stopped");
            else  if (status == "disabled")
                repo.updateStatus(i, "disabled");
            else 
                repo.updateStatus(i, "enabled");
            */
        }

        //System.out.println(statuses);
        return true;
    }

}
