package com.uas.account.web;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.ui.ModelMap;

import com.alibaba.fastjson.JSON;

/**
 * controller基础类
 * 
 * @author yingp
 *
 */
public class BaseController {

	protected static final String defultCharset = "UTF-8";

	@Autowired
	protected HttpServletRequest request;

	@Autowired
	protected HttpServletResponse response;

	protected static boolean isSuccess(ModelMap map) {
		return Boolean.TRUE.equals(map.get("success"));
	}

	protected static Object getContent(ModelMap map) {
		return map.get("content");
	}

	protected static ModelMap success() {
		return new ModelMap("success", true);
	}

	protected static ModelMap success(Object data) {
		return new ModelMap("success", true).addAttribute("content", data);
	}

	protected static ModelMap error(String errMsg) {
		return new ModelMap("error", true).addAttribute("errMsg", errMsg);
	}

	protected static ModelMap error(Object detail) {
		return new ModelMap("error", true).addAttribute("errDetail", detail);
	}

	protected static ModelMap error(String errCode, String errMsg) {
		return new ModelMap("error", true).addAttribute("errCode", errCode).addAttribute("errMsg", errMsg);
	}

	/**
	 * 输出json格式
	 * 
	 * @param obj
	 * @throws IOException
	 */
	protected void printJson(Object obj) throws IOException {
		response.addHeader("Content-Type", "application/json; charset=" + defultCharset);
		PrintWriter printWriter = response.getWriter();
		printWriter.append(JSON.toJSONString(obj));
		printWriter.flush();
		printWriter.close();
	}

	/**
	 * 响应Ajax请求
	 * 
	 * @param content
	 *            响应内容
	 * @throws IOException
	 */
	protected void printJsonP(String callback, Object content) throws IOException {
		if (!content.getClass().isAssignableFrom(String.class)) {
			content = JSON.toJSON(content);
		}
		response.setContentType("text/html;charset=" + defultCharset);
		PrintWriter out = response.getWriter();
		out.print(callback + "(" + content + ")");
		out.flush();
	}

	/**
	 * 输出流
	 * 
	 * @param fileName
	 *            文件名
	 * @param bytes
	 * @throws IOException
	 */
	protected ResponseEntity<byte[]> outputStream(String fileName, byte[] bytes) {
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
		headers.setContentDispositionFormData("attachment", fileName);
		return new ResponseEntity<byte[]>(bytes, headers, HttpStatus.CREATED);
	}

	protected Short checkPasswordLevel(String password) throws Exception {
		String strongRegex = "^(?=.*[0-9].*)(?=.*[A-Z].*)(?=.*[a-z].*).{8,20}$";
		String mediumRegex = "^(?=.{8,20})(((?=.*[A-Z])(?=.*[a-z]))|((?=.*[A-Z])(?=.*[0-9]))|((?=.*[a-z])(?=.*[0-9]))).*$";
		if (password == null) {
			throw new Exception("密码不能为空");
		}
		if (password.matches(strongRegex)) {
			return 3;
		} else if (password.matches(mediumRegex)) {
			return 2;
		} else {
			return 1;
		}
	}
}
