package org.bench4q.web.api;

import java.util.HashMap;
import java.util.Map;
import org.bench4q.share.models.master.AuthorizeResponseModel;
import org.bench4q.share.models.master.RegisterResponseModel;
import org.bench4q.share.models.master.UserModel;
import org.bench4q.web.masterMessager.UserMessager;
import org.bench4q.web.validation.UserValidate;
import org.bench4q.web.validation.ValidateResponseModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

@Controller
@SessionAttributes({ "accessToken", "username", "testPlanTaskList" })
public class UserController extends BaseController {

	private UserMessager userMessager;
	private UserValidate userValidate;

	public UserMessager getUserMessager() {
		return userMessager;
	}

	@Autowired
	public void setUserMessager(UserMessager userMessager) {
		this.userMessager = userMessager;
	}

	public UserValidate getUserValidate() {
		return userValidate;
	}

	@Autowired
	public void setUserValidate(UserValidate userValidate) {
		this.userValidate = userValidate;
	}

	@RequestMapping("register")
	@ResponseBody
	public Map<String, Object> register(@RequestParam String userName,
			@RequestParam String password, @RequestParam String scope) {
		Map<String, Object> map = new HashMap<String, Object>();
		ValidateResponseModel validateResponseModel = this.getUserValidate()
				.validateRegisterUser(userName, password, scope);
		if (!validateResponseModel.isSuccess()) {
			return fail(map, validateResponseModel.getMessage());
		}
		RegisterResponseModel registerResponseModel = this.getUserMessager()
				.register(userName, password, scope);
		if(registerResponseModel==null){
			return fail(map, "server error");
		}
		if (registerResponseModel.isSuccess()) {
			return success(map);

		} else {
			return fail(map, "user existed");
		}

	}

	@RequestMapping("login")
	@ResponseBody
	public Map<String, Object> normalLogin(UserModel userModel,
			ModelMap modelMap) {
		Map<String, Object> map = new HashMap<String, Object>();
		ValidateResponseModel validateResponseModel = this.getUserValidate()
				.validateUserLogin(userModel.getUserName(),
						userModel.getPassword());
		if (!validateResponseModel.isSuccess()) {
			return fail(map, "error userName or password");
		}
		AuthorizeResponseModel authorizeResponseModel = this.getUserMessager()
				.normalLogin(userModel.getUserName(), userModel.getPassword());
		if(authorizeResponseModel==null){
			return fail(map, "server error!");
		}
		if (authorizeResponseModel.isSuccess()) {
			updateSessionAttribute(modelMap, authorizeResponseModel,
					userModel.getUserName());
			return success(map);

		} else {
			return fail(map, "error userName or password");
		}

	}

	@RequestMapping("adminLogin")
	@ResponseBody
	public Map<String, Object> adminLogin(UserModel userModel, ModelMap modelMap) {
		Map<String, Object> map = new HashMap<String, Object>();
		System.out.println(userModel.getUserName());
		ValidateResponseModel validateResponseModel = this.getUserValidate()
				.validateUserLogin(userModel.getUserName(),
						userModel.getPassword());
		if (!validateResponseModel.isSuccess()) {
			return fail(map, "error userName or password");
		}
		AuthorizeResponseModel authorizeResponseModel = this.getUserMessager()
				.adminLogin(userModel.getUserName(), userModel.getPassword());
		if(authorizeResponseModel==null){
			return fail(map, "server error");
		}
		if (authorizeResponseModel.isSuccess()) {
			updateSessionAttribute(modelMap, authorizeResponseModel,
					userModel.getUserName());
			return success(map);
		} else {

			return fail(map, "error userName or password");
		}
	}

	private void updateSessionAttribute(ModelMap modelMap,
			AuthorizeResponseModel authorizeResponseModel, String userName) {
		modelMap.addAttribute("accessToken",
				authorizeResponseModel.getAccessToken());
		modelMap.addAttribute("username", userName);

	}

}