package org.fouram.core.base.controller;

import java.io.IOException;

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

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.util.WebUtils;
import org.fouram.core.base.constants.SysConstants;
import org.fouram.core.base.model.PageData;
import org.fouram.core.util.LoggerUtil;
import org.fouram.core.util.UUIDUtil;
import org.fouram.entity.SysUser;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.ModelAndView;

public class BaseController {

	protected HttpServletRequest request;
	protected HttpServletResponse response;

	@ModelAttribute
	private void validate(HttpServletRequest request, HttpServletResponse response) throws Exception {
		PageData pd = this.getPageData();
		for (Object key : pd.keySet()) {
			if (pd.get(key) instanceof String) {
				pd.put(key, pd.getString(key).trim());
			}
		}
	}

	@ModelAttribute
	public void setReqAndRes(HttpServletRequest request, HttpServletResponse response) {
		this.request = request;
		this.response = response;
	}

	public void writeString(String str) {
		try {
			response.setContentType("text/json;charset=utf-8");
			response.getWriter().write(str);
			response.getWriter().flush();
			response.getWriter().close();
		} catch (IOException e) {
			LoggerUtil.error(e);
		}
	}

	protected SysUser getUser() {
		SysUser user = (SysUser) getSession().getAttribute(SysConstants.ConstSession.SESSION_CMS_USER);
		return user;
	}

	protected String getRequestUrl() {
		try {
			return WebUtils.getSavedRequest(request).getRequestUrl();
		} catch (Exception e) {
			return "";
		}
	}

	protected String getMenuId() {
		return (String) getSession().getAttribute(SysConstants.ConstSession.SESSION_MENU_ID);
	}

	protected Session getSession() {
		Subject currentUser = SecurityUtils.getSubject();
		return currentUser.getSession();
	}

	public PageData getPageData() {
		return new PageData(getRequest());
	}

	public ModelAndView getModelAndView() {
		return new ModelAndView();
	}

	public HttpServletRequest getRequest() {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
				.getRequest();
		return request;
	}

	public String getRootPath() {
		return request.getServletContext().getRealPath("/");
	}

	public String get32UUID() {
		return UUIDUtil.get32UUID();
	}

	public void shiroLogin(String userName, String password) {
		Subject subject = SecurityUtils.getSubject();
		UsernamePasswordToken token = new UsernamePasswordToken(userName, password);
		subject.login(token);
	}

	public void shiroLogout() {
		Subject subject = SecurityUtils.getSubject();
		subject.logout();
	}
}