package com.controller.rest;

import com.entity.AddProject;
import com.entity.AjaxResponse;
import com.service.MemberProjectService;
import com.service.ProjectService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * Rest controller for the Projects page.
 *
 * @author Laurentiu Marcut
 */
@RestController
public class ProjectRestController {

    private static final Logger logger = Logger.getLogger(ProjectRestController.class);

    @Autowired
    ProjectService projectService;

    @Autowired
    MemberProjectService memberProjectService;

    /**
     *
     * Create project.
     *
     * @param addProject
     * @param request
     * @return response
     */
    @RequestMapping(value = "/projects/createProject", method = RequestMethod.POST)
    public @ResponseBody AjaxResponse createProject(@RequestBody AddProject addProject, HttpServletRequest request) {
        AjaxResponse response = new AjaxResponse();

        try {
            String member = request.getUserPrincipal().getName();
            this.projectService.insert(addProject.getProject(), member, addProject.getRole());
        } catch (Exception e) {
            response.setState("KO");
            response.setMessage("Can not create the project.");
            return response;
        }

        response.setState("OK");
        response.setMessage("Project created.");
        return response;
    }

    /**
     *
     * Unfollow project.
     *
     * @param cookieProject
     * @param project
     * @param httpRequest
     * @param httpResponse
     * @return response
     */
    @RequestMapping(value = "/projects/unfollowProject", method = RequestMethod.POST)
    public @ResponseBody AjaxResponse unfollowProject(@CookieValue(value = "currentProject", defaultValue = "") String cookieProject,
                                              @RequestBody String project, HttpServletRequest httpRequest, HttpServletResponse httpResponse) {
        AjaxResponse response = new AjaxResponse();
        String member = httpRequest.getUserPrincipal().getName();

        try {
            this.memberProjectService.delete(project, member);

            // Remove the cookie.
            if (cookieProject.equals(project)) {
                Cookie cookie = new Cookie("currentProject", null);
                cookie.setPath("/");
                cookie.setMaxAge(0);
                httpResponse.addCookie(cookie);
            }
        } catch (Exception e) {
            response.setState("KO");
            response.setMessage("Can not unfollow the project.");
            return response;
        }

        response.setState("OK");
        response.setMessage("Project unfollowed.");
        return response;
    }

    /**
     *
     * Delete project.
     *
     * @param cookieProject
     * @param project
     * @param httpResponse
     * @return response
     */
    @RequestMapping(value = "/projects/deleteProject", method = RequestMethod.POST)
    public @ResponseBody AjaxResponse deleteProject(@CookieValue(value = "currentProject", defaultValue = "") String cookieProject,
                                                    @RequestBody String project, HttpServletResponse httpResponse) {
        AjaxResponse response = new AjaxResponse();

        try {
            this.projectService.delete(project);

            // Remove the cookie.
            if (cookieProject.equals(project)) {
                Cookie cookie = new Cookie("currentProject", null);
                cookie.setPath("/");
                cookie.setMaxAge(0);
                httpResponse.addCookie(cookie);
            }
        } catch (Exception e) {
            response.setState("KO");
            response.setMessage("Can not remove the project.");
            return response;
        }

        response.setState("OK");
        response.setMessage("Project removed.");
        return response;
    }

    /**
     *
     * Load project.
     *
     * @param project
     * @param response
     */
    @RequestMapping(value = "/projects/loadProject", method = RequestMethod.POST)
    public void loadProject(@RequestBody String project, HttpServletResponse response) {
        Cookie cookie = new Cookie("currentProject", project);
        cookie.setPath("/");
        response.addCookie(cookie);
    }
}
