package de.jottyfan.camporganizer.module.confirmation.person;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;

import de.jottyfan.camporganizer.module.confirmation.confirmation.IConfirmationService;
import de.jottyfan.camporganizer.module.confirmation.person.impl.PersonBean;

/**
 *
 * @author jotty
 *
 */
@Controller
public class PersonController {

	@Autowired
	private HttpServletRequest request;

	@Autowired
	private IConfirmationService confirmationService;

	@Autowired
	private IPersonService personService;

	@GetMapping("/confirmation/person/{pk}")
	public String getIndex(Model model, @PathVariable Integer pk) {
		String username = confirmationService.getCurrentUser(request);
		model.addAttribute("currentUser", username);
		model.addAttribute("person", personService.getPerson(username, pk));
		model.addAttribute("camps", personService.getCamps(username));
		model.addAttribute("annotations", personService.getAnnotations(pk));
		return "confirmation/person";
	}

	@PostMapping("/confirmation/person/update")
	public String doUpdate(@ModelAttribute PersonBean bean, Model model) {
		String username = confirmationService.getCurrentUser(request);
		personService.updatePerson(bean, username);
		return "redirect:/confirmation";
	}
}
