/**
 * 
 */
package de.bstly.board.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import de.bstly.board.businesslogic.CommentManager;
import de.bstly.board.businesslogic.EntryManager;
import de.bstly.board.businesslogic.FlagManager;
import de.bstly.board.controller.support.EntityResponseStatusException;
import de.bstly.board.model.Flag;
import de.bstly.board.model.support.Types;

/**
 * The Class FlagController.
 */
@RestController
@RequestMapping("/flags")
public class FlagController extends BaseController {

	@Autowired
	private FlagManager flagManager;
	@Autowired
	private EntryManager entryManager;
	@Autowired
	private CommentManager commentManager;

	/**
	 * Flag entry.
	 *
	 * @param id the id
	 */
	@PreAuthorize("isAuthenticated()")
	@PutMapping("/entry/{id}")
	public void flagEntry(@PathVariable("id") Long id) {
		if (!entryManager.exists(id)) {
			throw new EntityResponseStatusException(HttpStatus.UNPROCESSABLE_ENTITY);
		}

		Flag flag = flagManager.get(getCurrentUsername(), id, Types.entry);

		if (flag != null) {
			throw new EntityResponseStatusException(HttpStatus.NOT_MODIFIED);
		}

		flag = new Flag();
		flag.setTarget(id);
		flag.setAuthor(getCurrentUsername());
		flag.setTargetType(Types.entry);
		flagManager.save(flag);
		flagManager.checkEntryFlagStatus(id);
		throw new EntityResponseStatusException(HttpStatus.CREATED);
	}

	/**
	 * Unflag entry.
	 *
	 * @param id the id
	 */
	@PreAuthorize("isAuthenticated()")
	@DeleteMapping("/entry/{id}")
	public void unflagEntry(@PathVariable("id") Long id) {
		if (!entryManager.exists(id)) {
			throw new EntityResponseStatusException(HttpStatus.UNPROCESSABLE_ENTITY);
		}

		Flag flag = flagManager.get(getCurrentUsername(), id, Types.entry);
		if (flag == null) {
			throw new EntityResponseStatusException(HttpStatus.NOT_MODIFIED);
		}

		flagManager.delete(flag);
		flagManager.checkEntryFlagStatus(id);
	}

	/**
	 * Flag comment.
	 *
	 * @param id the id
	 */
	@PreAuthorize("isAuthenticated()")
	@PutMapping("/comment/{id}")
	public void flagComment(@PathVariable("id") Long id) {
		if (!commentManager.exists(id)) {
			throw new EntityResponseStatusException(HttpStatus.UNPROCESSABLE_ENTITY);
		}

		Flag flag = flagManager.get(getCurrentUsername(), id, Types.comment);

		if (flag != null) {
			throw new EntityResponseStatusException(HttpStatus.NOT_MODIFIED);
		}

		flag = new Flag();
		flag.setTarget(id);
		flag.setAuthor(getCurrentUsername());
		flag.setTargetType(Types.comment);
		flagManager.save(flag);
		flagManager.checkCommentFlagStatus(id);
		throw new EntityResponseStatusException(HttpStatus.CREATED);
	}

	/**
	 * Unflag comment.
	 *
	 * @param id the id
	 */
	@PreAuthorize("isAuthenticated()")
	@DeleteMapping("/comment/{id}")
	public void unflagComment(@PathVariable("id") Long id) {
		if (!commentManager.exists(id)) {
			throw new EntityResponseStatusException(HttpStatus.UNPROCESSABLE_ENTITY);
		}

		Flag flag = flagManager.get(getCurrentUsername(), id, Types.comment);
		if (flag == null) {
			throw new EntityResponseStatusException(HttpStatus.NOT_MODIFIED);
		}

		flagManager.delete(flag);
		flagManager.checkCommentFlagStatus(id);
	}

}
