package edu.fiu.controllers;

import edu.fiu.models.ActivityProgress;
import edu.fiu.models.ActivityProgressDTO;
import edu.fiu.models.Session;
import edu.fiu.repositories.ActivityProgressRepository;
import edu.fiu.repositories.SessionRepository;
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/progress")
public class ActivityProgressController
{
    private SessionRepository sessions;
    private ActivityProgressRepository repo;
    private Request req;

    @Inject
    public ActivityProgressController(ActivityProgressRepository repo, SessionRepository sessions, Request req) {
        this.repo = repo;
        this.sessions = sessions;
        this.req = req;
    }

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

    /*
    @GET
    @Path(":userId")
    public List<ActivityProgress> ListByUser() {
        //LAZY!! :(
        Integer userId = new Integer(req.param("userId").intValue());
        return repo.list(userId);
    }*/

    @GET
    @Path("/session")
    public List<Integer> ListBySession(){
        String token = req.param("session").value();
        Session ses = sessions.get(token);

        ArrayList<Integer> completed = new ArrayList<Integer>();

        //session does not exist
        if(ses == null)
            return new ArrayList<Integer>();

        for (Integer userId:  ses.getUserIds())
        {

            List<ActivityProgress> l = repo.list(userId);
            for(ActivityProgress p : l){
                completed.add(p.activityId);
            }
        }

        return completed;
    }

    @Path(":userId/:activityType")
    public ActivityProgress Get(){
        Integer userId = new Integer(req.param("userId").intValue());
        Integer activityId = new Integer(req.param("activityId").intValue());

        ActivityProgress progress = repo.get(userId, activityId);
        if (progress == null)
            throw new Err(Status.NOT_FOUND);

        return progress;
    }

    @POST
    public ActivityProgress Post() throws Exception {
        //had to create a dto cause ActivityProgress is not Serializable
        ActivityProgressDTO p = req.body(ActivityProgressDTO.class);
        ActivityProgress progress = new ActivityProgress(
                "DONE",
                p.getUserId(),
                "",
                p.getActivityId(),
                System.currentTimeMillis() / 1000L);
        if (progress.state == null)
            progress.state = ActivityProgress.ActivityProgressState.BLANK;
        if (progress.content == null)
            progress.content = "";
        if (progress.date <= 0)
            progress.date = System.currentTimeMillis() / 1000L;

        repo.create(progress);

        return progress;
    }

    @PUT
    @Path(":userId/:activityType")
    public ActivityProgress Put() throws Exception {
        Integer userId = new Integer(req.param("userId").intValue());
        Integer activityId = new Integer(req.param("activityId").intValue());
        ActivityProgress progress = req.body(ActivityProgress.class);
        progress.userId = userId;
        progress.activityId = activityId;

        ActivityProgress current = repo.get(progress.userId, progress.activityId);
        if (current == null)
            throw new Err(Status.NOT_FOUND);

        if (progress.state == null)
            progress.state = current.state;
        if (progress.content == null)
            progress.content = current.content;
        if (progress.date <= 0)
            progress.date = System.currentTimeMillis() / 1000L;

        if(!repo.update(progress))
            throw new Err(Status.SERVER_ERROR);

        return progress;
    }

    @GET
    @Path("/delete/")
    public boolean delete(){
        Integer userId = new Integer(req.param("userId").intValue());
        Integer activityId = new Integer(req.param("activityId").intValue());

        repo.delete(userId, activityId);
        
        //if(!repo.delete(userId, activityId))
            //throw new Err(Status.NOT_FOUND);

        return true;
    }

}
