package cn.hmsoft.weixin.util;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.hmsoft.application.SpringHelper;
import cn.hmsoft.exception.app.ModuleNotLoadException;
import cn.hmsoft.frame.exception.BusinessException;
import cn.hmsoft.helper.StringHelper;
import cn.hmsoft.weixin.data.model.WxDefine;
import cn.hmsoft.weixin.data.model.WxMsgProcessor;
import cn.hmsoft.weixin.data.model.WxUser;
import cn.hmsoft.weixin.entity.response.OauthAccessTokenResponseJson;
import cn.hmsoft.weixin.entity.response.WxResponseJson;
import cn.hmsoft.weixin.request.OauthAccessTokenRequest;
import cn.hmsoft.weixin.request.UserinfoRequest;

public abstract class WeiXinUtil {

	/*****************************
	 * API错误列表
	 */
	public static Map<Integer, String> ApiErrorMap = new HashMap<Integer, String>();

	/*****************************
	 * 微信公众号列表
	 */
	public static Map<String, WxDefine> WeiXinDefineMap;
	public static List<WxDefine> WeiXinDefineArray;

	public static String getErrorMsg(WxResponseJson json) {
		if (ApiErrorMap.containsKey(json.getErrcode()))
			return ApiErrorMap.get(json.getErrcode());
		return json.getErrmsg();
	}

	public static WxMsgProcessor getProcessorClass(WxDefine wx, String msg_type) {
		if (wx.getProcessorMap().containsKey(msg_type))
			return wx.getProcessorMap().get(msg_type);
		return null;

	}

	public static WxMsgProcessor getProcessorClass(String app_id, String msg_type) {
		WxDefine wx = getWxDefine(app_id);
		return getProcessorClass(wx, msg_type);

	}

	/*************************
	 * 通过开发者微信号
	 */
	public static WxDefine getWxDefineByCode(String wx_code) {
		if (WeiXinDefineMap == null || StringHelper.isEmpty(wx_code))
			throw new ModuleNotLoadException(WeiXinParamUtil.ModuleName);
		for (WxDefine wf : WeiXinDefineMap.values())
			if (wx_code.equals(wf.getWx_code()))
				return wf;
		throw new ModuleNotLoadException(WeiXinParamUtil.ModuleName);
	}

	/*************************
	 * 获取公众号，获取不到则报错
	 */
	public static WxDefine getWxDefine(String app_id) {
		if (WeiXinDefineMap == null || !WeiXinDefineMap.containsKey(app_id))
			throw new ModuleNotLoadException(WeiXinParamUtil.ModuleName);
		return WeiXinDefineMap.get(app_id);
	}

	/*************************
	 * 获取公众号，获取不到则报错
	 */
	public static WxDefine getWxDefine(int wx_id) {
		if (WeiXinDefineMap == null)
			throw new ModuleNotLoadException(WeiXinParamUtil.ModuleName);
		for (WxDefine wx : WeiXinDefineMap.values()) {
			if (wx.getWx_id() - wx_id == 0)
				return wx;
		}
		throw new ModuleNotLoadException(WeiXinParamUtil.ModuleName);
	}

	/******************************
	 * 查询绑定用户
	 */
	public static WxUser findUser(String app_id, String code) {
		WxDefine wx = getWxDefine(app_id);
		return findUser(wx.getWx_id(), code);
	}

	/******************************
	 * 查询绑定用户
	 */
	public static WxUser findUser(int wx_id, String code) {
		OauthAccessTokenRequest rq = new OauthAccessTokenRequest(code);
		rq.setWxDefine(wx_id);
		OauthAccessTokenResponseJson json = rq.request();
		if (json == null || StringHelper.isEmpty(json.getOpenid()))
			throw new BusinessException("系统错误：未找到微信用户，请退出微信重试");
		WxUser user = SpringHelper.AppGobalDao.find(WxUser.class, "open_id", json.getOpenid());

		if (user == null) {
			UserinfoRequest uReq = new UserinfoRequest(json.getOpenid());
			uReq.setWxDefine(wx_id);
			user = (WxUser) uReq.request();
		}
		if (user == null)
			throw new BusinessException("系统错误：未找到微信用户，请退出微信重试");
		return user;

	}

}
