package com.goafanti.common.controller;

import java.text.DateFormat;
import java.text.MessageFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

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

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.core.io.support.ResourcePropertySource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;

import com.goafanti.common.bo.Error;
import com.goafanti.common.constant.AFTConstants;
import com.goafanti.common.model.Admin;
import com.goafanti.common.model.User;
import com.goafanti.core.shiro.token.TokenManager;

@Controller
public class BaseController {
	@Resource(name = "errorResource")
	private ResourcePropertySource	errorResource;

	@Value(value = "${static.host}")
	private String					staticHost			= null;

	@Value(value = "${avatar.host}")
	private String					avatarHost			= null;

	@Value(value = "${upload.path}")
	private String					uploadPath			= null;

	@Value(value = "${upload.private.path}")
	private String					uploadPrivatePath	= null;

	@Value(value = "${accessKey}")
	private String					accessKey			= null;

	@Value(value = "${accessSecret}")
	private String					accessSecret		= null;

	@Value(value = "${portal.host}")
	private String					portalHost			= null;

	@Value(value = "${avatar.upload.host}")
	private String					avatarUploadHost	= null;

	protected Error buildError(String key) {
		return buildError(key, null);
	}

	protected Error buildError(String key, String message) {
		String msg = (String) errorResource.getProperty(key);
		if (StringUtils.isEmpty(msg)) {
			msg = message;
		}
		return new Error(key, msg);
	}

	/**
	 * 带参数的error message
	 * 
	 * @param key
	 * @param message
	 * @param object
	 * @return
	 */
	protected Error buildError(String key, String message, Object... object) {
		String msg = (String) errorResource.getProperty(key);
		if (StringUtils.isEmpty(msg)) {
			msg = message;
		}
		msg = MessageFormat.format(msg, object);
		return new Error(key, msg);
	}

	protected Error buildErrorByMsg(String msg, Object... params) {
		return new Error(MessageFormat.format(msg, params));
	}

	protected String getErrorMessage(String key) {
		Object msg = errorResource.getProperty(key);
		if (msg == null) {
			return key;
		}
		return (String) msg;
	}

	protected String getErrorMessage(String key, Object... obj) {
		return MessageFormat.format(getErrorMessage(key), obj);
	}

	@InitBinder
	public void initBinder(WebDataBinder binder) {
		DateFormat dateFormat = new SimpleDateFormat(AFTConstants.YYYYMMDDHHMMSS);
		dateFormat.setLenient(true);
		binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
	}

	@ModelAttribute
	public void prepareModel(HttpServletRequest request, Model model) {

		model.addAttribute("staticDomain", avatarHost);
		model.addAttribute("staticHost", staticHost);
		model.addAttribute("avatarHost", avatarHost);
		model.addAttribute("avatarUploadHost", avatarUploadHost);
		model.addAttribute("portalHost", portalHost);
		model.addAttribute("basePath", request.getContextPath());
		model.addAttribute("shiro", SecurityUtils.getSubject());
		model.addAttribute("isLogin", TokenManager.isLogin());
		model.addAttribute("csrf", TokenManager.getCSRF());
		model.addAttribute("isAdmin", false);
		if (TokenManager.isLogin() && TokenManager.getToken() instanceof User) {
			model.addAttribute("userData", TokenManager.getUserToken());
			model.addAttribute("userName", TokenManager.getUserToken().getNickname());
		}

		if (TokenManager.isLogin() && TokenManager.getToken() instanceof Admin) {
			model.addAttribute("adminData", TokenManager.getAdminToken());
			model.addAttribute("userName", TokenManager.getAdminToken().getName());
			model.addAttribute("departmentId", TokenManager.getAdminToken().getDepartmentId());
			model.addAttribute("isAdmin", true);
		}
	}
}
