package com.zhiwei.manage.servlet;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.zhiwei.manage.bean.JsonResult;
import com.zhiwei.manage.bean.PageEty;
import com.zhiwei.manage.bean.Person;
import com.zhiwei.manage.service.PersonService;

/**
 * 个人控制
 * 
 * @author admin
 *
 */
@Controller
public class PersonController {

	@Resource
	private PersonService personService;

	/**
	 * 登录
	 * 
	 * @param req
	 * @param res
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "/login", produces = "application/json;charset=utf-8")
	@ResponseBody
	public String login(HttpServletRequest req, HttpServletResponse res, @RequestBody Map<String, Object> map) {
		String username = (String) map.get("username");
		String password = (String) map.get("password");
		Person p = personService.login(username, password);
		if (p != null) {
			Cookie cookie;
			try {
				username = URLEncoder.encode(username, "utf-8");
				cookie = new Cookie("username", username);
				res.addCookie(cookie);
				cookie.setMaxAge(1000 * 60 * 60);
				cookie.setPath("/");
				req.setAttribute("username", username);
				req.setAttribute("lv", p.getLv());
				p.setPassword("");
				JsonResult result = new JsonResult(true, 200, "登录成功", p);
				return JSON.toJSONString(result);
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
				JsonResult result = new JsonResult(false, 200, "登录失败", "");
				return JSON.toJSONString(result);
			}
		} else {
			JsonResult result = new JsonResult(false, 200, "登录失败", "");
			return JSON.toJSONString(result);
		}
	}

	/**
	 * 登出
	 * 
	 * @param req
	 * @param res
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "/leave", produces = "application/json;charset=utf-8")
	@ResponseBody
	public String leave(HttpServletRequest req, HttpServletResponse res, @RequestBody Map<String, Object> map) {
		String username = (String) map.get("username");
		Cookie[] cookies = req.getCookies();
		for (Cookie cookie : cookies) {
			if (cookie.getName().equals(username)) {
				Cookie ck = new Cookie(username, null);
				ck.setMaxAge(0);
			}
		}
		JsonResult result = new JsonResult(true, 200, "退出成功", "");
		return JSON.toJSONString(result);
	}

	/**
	 * cookie获取
	 * 
	 * @param req
	 * @param res
	 * @return
	 */
	@RequestMapping(value = "/getcookie", produces = "application/json;charset=utf-8")
	@ResponseBody
	public String getCookie(HttpServletRequest req, HttpServletResponse res) {
		Cookie[] cookies = req.getCookies();
		String result = "";
		for (Cookie cookie : cookies) {
			result += cookie.getName() + ":" + cookie.getValue() + "\n";
		}
		return JSON.toJSONString(result);
	}

	/**
	 * 获取用户信息
	 * 
	 * @param res
	 * @param req
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "/getPerson", produces = "application/json;charset=utf-8")
	@ResponseBody
	public String getPersonByUser(HttpServletResponse res, HttpServletRequest req,
			@RequestBody Map<String, Object> map) {
		String username = (String) map.get("username");
		int pageSize = (int) map.get("pageSize");
		int pageNo = (int) map.get("pageNo");
		int lv = (int) map.get("lv");
		if (username == null || username.equals("")) {
			PageEty page = personService.findAll(pageNo, pageSize, lv);
			JsonResult result = new JsonResult(true, 200, "获取成功", page);
			return JSON.toJSONString(result);
		} else {
			PageEty page = personService.findByUserName(username, pageNo, pageSize, lv);
			JsonResult result = new JsonResult(true, 200, "获取成功", page);
			return JSON.toJSONString(result);
		}
	}

	/**
	 * 更新用户信息
	 * 
	 * @param person
	 * @return
	 */
	@RequestMapping(value = "/updatePerson", produces = "application/json;charset=utf-8")
	@ResponseBody
	public String updatePerson(@RequestBody Person person) {
		boolean b = personService.update(person);
		JsonResult result = new JsonResult(b ? true : false, 200, b ? "操作成功" : "操作失败", "");
		return JSON.toJSONString(result);
	}

	/**
	 * 删除用户信息
	 * 
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "/deletePerson", produces = "application/json;charset=utf-8")
	@ResponseBody
	public String deletePerson(@RequestBody Map<String, Object> map) {
		List<String> deleteList = (List<String>) map.get("personId");
		boolean b = personService.delete(deleteList);
		JsonResult result = new JsonResult(b ? true : false, 200, b ? "操作成功" : "操作失败");
		return JSON.toJSONString(result);
	}

	/**
	 * 存储用户信息
	 * 
	 * @param req
	 * @param person
	 * @return
	 */
	@RequestMapping(value = "/savePerson", produces = "application/json;charset=utf-8")
	@ResponseBody
	public String savePerson(HttpServletRequest req, @RequestBody Person person) {
		Cookie[] cookies = req.getCookies();
		for (Cookie cookie : cookies) {
			System.out.println(cookie.getName());
			if (cookie.getName().equals(person.getUsername())) {
				JsonResult result = new JsonResult(false, 200, "该用户在线，添加同名");
				return JSON.toJSONString(result);
			}
		}
		boolean b = personService.insert(person);
		JsonResult result = new JsonResult(b ? true : false, 200, "操作成功", "");
		return JSON.toJSONString(result);
	}

}
