package org.bench4q.master.api;


import org.bench4q.master.domain.service.UserService;
import org.bench4q.master.domain.service.auth.AccessToken;
import org.bench4q.master.domain.service.auth.AuthenticationManager;
import org.bench4q.share.models.master.AuthorizeResponseModel;
import org.bench4q.share.models.master.RegisterResponseModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/user")
public class UserController {
	private UserService userService;
	private AuthenticationManager authenticationManager;

	private UserService getUserService() {
		return userService;
	}

	@Autowired
	private void setUserService(UserService userService) {
		this.userService = userService;
	}

	private AuthenticationManager getAuthenticationManager() {
		return authenticationManager;
	}

	@Autowired
	private void setAuthenticationManager(
			AuthenticationManager authenticationManager) {
		this.authenticationManager = authenticationManager;
	}

	@RequestMapping(value = "/register", method = RequestMethod.GET)
	@ResponseBody
	public RegisterResponseModel register(@RequestParam String userName,
			@RequestParam String password, @RequestParam byte scope) {
		boolean success = this.getUserService().register(userName, password,
				scope);
		RegisterResponseModel registerUserResponseModel = new RegisterResponseModel();
		registerUserResponseModel.setSuccess(success);
		return registerUserResponseModel;
	}

	private AuthorizeResponseModel authorize(String userName, String password,
			byte requiredScope) {
		boolean authorized = this.getUserService().validateUser(userName,
				password, requiredScope);
		if (!authorized) {
			return buildAuthorizeResponseModel(null, -1, false);
		}
		AccessToken accessToken = this.getAuthenticationManager()
				.generateAccessToken(userName);
		String credential = this.getAuthenticationManager().generateCredential(
				userName);
		return buildAuthorizeResponseModel(credential,
				accessToken.getExpiresIn(), true);
	}

	private AuthorizeResponseModel buildAuthorizeResponseModel(
			String accessToken, int expiresIn, boolean success) {
		AuthorizeResponseModel authorizeResponseModel = new AuthorizeResponseModel();
		authorizeResponseModel.setAccessToken(accessToken);
		authorizeResponseModel.setExpiresIn(expiresIn);
		authorizeResponseModel.setSuccess(success);
		return authorizeResponseModel;
	}

	@RequestMapping(value = "/adminAuthorize", method = RequestMethod.GET)
	@ResponseBody
	public AuthorizeResponseModel adminAuthorize(@RequestParam String userName,
			@RequestParam String password) {
		return authorize(userName, password, UserService.SUPER_AUTHENTICATION);
	}

	@RequestMapping(value = "/normalAuthorize", method = RequestMethod.GET)
	@ResponseBody
	public AuthorizeResponseModel normalAuthorize(
			@RequestParam String userName, @RequestParam String password) {
		return authorize(userName, password, UserService.NORAML_AUTHENTICATION);
	}

	@RequestMapping(value = "/deleteUser/{userName}", method = RequestMethod.GET)
	@ResponseBody
	public AuthorizeResponseModel deleteUser(@PathVariable String userName) {

		AuthorizeResponseModel authorizeResponseModel = new AuthorizeResponseModel();
		authorizeResponseModel.setSuccess(this.getUserService().deleteUser(
				userName));
		return authorizeResponseModel;

	}

}
