package de.jottyfan.camporganizer.module.business.privileges;

import javax.annotation.security.RolesAllowed;
import javax.servlet.http.HttpServletRequest;
import javax.websocket.server.PathParam;

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.PostMapping;

import de.jottyfan.camporganizer.module.business.business.IBusinessService;
import de.jottyfan.camporganizer.module.business.privileges.impl.PrivilegesBean;

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

	@Autowired
	private HttpServletRequest request;

	@Autowired
	private IPrivilegesService privilegesService;

	@Autowired
	private IBusinessService indexService;

	@GetMapping("/business/privileges")
	@RolesAllowed({ "admin" })
	public String getIndex(Model model) {
		String username = indexService.getCurrentUser(request);
		model.addAttribute("currentUser", username);
		model.addAttribute("privileges", privilegesService.getPrivileges());
		model.addAttribute("profiles", privilegesService.getProfiles());
		model.addAttribute("bean", new PrivilegesBean());
		return "business/privileges";
	}

	@PostMapping("/business/privileges/add")
	@RolesAllowed({ "admin" })
	public String getAdd(@ModelAttribute PrivilegesBean bean, Model model) {
		privilegesService.add(bean);
		return getIndex(model);
	}

	@GetMapping("/business/privileges/delete")
	@RolesAllowed({ "admin" })
	public String getDelete(@PathParam(value = "fkCamp") Integer fkCamp,
			@PathParam(value = "fkProfile") Integer fkProfile, Model model) {
		PrivilegesBean bean = new PrivilegesBean();
		bean.setFkCamp(fkCamp);
		bean.setFkProfile(fkProfile);
		privilegesService.remove(bean, indexService.getCurrentUser(request));
		return getIndex(model);
	}
}
