package cn.feast.coding.controller.system;


import cn.feast.coding.model.generator.Page;
import cn.feast.coding.model.system.Role;
import cn.feast.coding.model.system.User;
import cn.feast.coding.model.system.UserRole;
import cn.feast.coding.service.system.PasswordHelper;
import cn.feast.coding.service.system.RoleService;
import cn.feast.coding.service.system.UserRoleService;
import cn.feast.coding.service.system.UserService;
import cn.feast.coding.tools.JsonResult;
import cn.feast.coding.tools.PageUtils;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;
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.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController extends BaseController{
	@Autowired
	private UserService userService;
	@Autowired
	private RoleService roleService;
	@Autowired
	private UserRoleService userRoleService;
	@Autowired
	private PasswordHelper passwordHelper;
	
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String list(User user, Page page, ModelMap model){
		page.setMaxRows(5);
		List<User> list = userService.queryByUser(user);
		page.setStart(PageUtils.getPage(page.getPageNumber(), page.getTotalPage(), list.size(), page.getMaxRows()));
		page.setTotalPage(PageUtils.getTotalPage(page.getPageNumber(), page.getTotalPage(), list.size(), page.getMaxRows()));
		List<User> userList = userService.queryByList(page, user);
		model.put("page", page);
		model.put("userList", userList);
		model.put("user", user);
		return "user/user_list";
	}

	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public String add(ModelMap model){
		List<Role> roleList = roleService.queryAll();
		model.put("roleList", roleList);
		return "user/user_add";
	}
	
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public String add(User user, @RequestParam(value = "roles", required = false) Integer[] roles, ModelMap model){
		passwordHelper.encryptPassword(user);
		userService.save(user);
		UserRole userRole = new UserRole();
		if(null != roles && roles.length > 0){
			for(Integer i : roles){
				userRole.setUserid(user.getId());
				userRole.setRoleid(i);
				userRoleService.save(userRole);
			}
		}
		model.put("flag", 1);
		return "user/user_add";
	}

	@RequestMapping(value = "/check", method = RequestMethod.GET)
	@ResponseBody
	public JsonResult check(@RequestParam("userName") String userName){
		JsonResult jsonResult = new JsonResult();
		jsonResult.setState(JsonResult.SUCCESS);
		boolean result = userService.checkName(userName);
		if(result){
			jsonResult.setState(JsonResult.ERROR);
		}
		return jsonResult;
	}

	
	@RequestMapping(value = "/update", method = RequestMethod.GET)
	public String update(User user,ModelMap model){
		user = userService.queryById(user.getId());
		List<Role> roleList = roleService.queryAll();
		List<UserRole> myRoles = userRoleService.queryByUser(user.getId());
		model.put("myRoles", myRoles);
		model.put("roleList", roleList);
		model.put("user", user);
		return "user/user_edit";
	}
	
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public String update(User user,@RequestParam(value = "roles", required = false) Integer[] roles,HttpServletRequest request, ModelMap model){
		passwordHelper.encryptPassword(user);
		userService.update(user);
		userRoleService.deleteByUser(user.getId());
		UserRole userRole = new UserRole();
		if(null != roles && roles.length > 0){
			for(Integer i : roles){
				userRole.setUserid(user.getId());
				userRole.setRoleid(i);
				userRoleService.save(userRole);
			}
		}
		model.put("flag", 1);
		return "user/user_edit";
	}

	@RequestMapping(value = "/delete", method = RequestMethod.GET)
	@ResponseBody
	public void delete(User user){
		userRoleService.deleteByUser(user.getId());
		userService.deleteById(user.getId());
	}

	@RequestMapping(value = "/password", method = RequestMethod.POST)
	public String password(@RequestParam("oldPass") String oldPass,@RequestParam("newPass") String newPass, ModelMap model) {
		User user = getCurrentUser(userService);
		String oldPassword = (new SimpleHash("md5", oldPass, ByteSource.Util.bytes(user.getUserSalt()), 2)).toHex();
		if(!user.getUserPass().equals(oldPassword)){
			model.put("flag", "原密码不正确");
			return "change_password";
		}
		user.setUserPass(newPass);
		passwordHelper.encryptPassword(user);
		userService.update(user);
		model.put("flag", "修改成功");
		return "change_password";
	}


	public static void main(String[] args) {
		String oldPassword = (new SimpleHash("md5", "wangpeng12..", ByteSource.Util.bytes("4000e18ccbb3c6fd10853d10b9746bf1"), 2)).toHex();
		System.out.println(oldPassword);
	}

}
