
package org.tio.sitexxx.server.utils;

import cn.hutool.core.util.StrUtil;
import org.apache.commons.lang3.StringUtils;
import org.tio.http.common.HttpRequest;
import org.tio.http.common.session.HttpSession;
import org.tio.sitexxx.service.model.main.User;
import org.tio.sitexxx.service.service.base.UserService;
import org.tio.sitexxx.service.utils.AESUtils;
import org.tio.sitexxx.service.vo.*;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author tanyaowu
 * 2016年8月10日 上午10:59:58
 */
public class WebUtils {
	private static UserService	userService	= UserService.ME;

	/**
	 * 把"/user/curr"这样的path变成"/api/user/curr.php"
	 * 把"/user/curr?name=tan"这样的path变成"/api/user/curr.php?name=tan"
	 * @param path
	 * @return
	 */
	public static String path(String path) {
		String contextpath = org.tio.sitexxx.service.vo.Const.API_CONTEXTPATH;
		String suffix = org.tio.sitexxx.service.vo.Const.API_SUFFIX;

		int x = StringUtils.indexOf(path, "?");
		if (x == -1) {
			return contextpath + path + suffix;
		} else {
			String path1 = path.substring(0, x);
			String queryStr = path.substring(x + 1);
			return contextpath + path1 + suffix + "?" + queryStr;
		}
	}

	/**
	 * 获取当前用户
	 * @param session
	 * @return
	 * @author tanyaowu
	 */
	public static User currUser(HttpRequest request) {
		Integer userid = currUserId(request);
		if (userid != null) {
			User user = userService.getById(userid);
			return UserService.ME.checkUserStatus(user) ? user : null;
		}
		return null;
	}

	public static User currUser(String token) {
		if (StrUtil.isNotBlank(token)) {
			token = token.replace(" ","+");
			String tokenStr = AESUtils.decrypt(token);
			String[] str = tokenStr.split("###");
			int id = Integer.parseInt(str[1]);
			User user = userService.getById(id);
			return UserService.ME.checkUserStatus(user) ? user : null;
		}
		return null;
	}

	/**
	 * 获取当前用户的userid
	 * @param request 请求
	 * @return
	 */
	public static Integer currUserId(HttpRequest request) {
		String token = request.getHeader(SessionKey.TOKEN_KEY);
		if (StrUtil.isNotBlank(token)) {
			token = token.replace(" ","+");
			String tokenStr = AESUtils.decrypt(token);
			String[] user = tokenStr.split("###");
			return Integer.parseInt(user[1]);
		}
		HttpSession session = request.getHttpSession();
		SessionExt sessionExt = WebUtils.getSessionExt(session);
		return sessionExt.getUid();
	}

	public static Integer currUserId(String token) {
		if (StrUtil.isNotBlank(token)) {
			token = token.replace(" ","+");
			String tokenStr = AESUtils.decrypt(token);
			String[] user = tokenStr.split("###");
			return Integer.parseInt(user[1]);
		}
		return null;
	}

	/**
	 * 获取令牌
	 * @param userId 用户序号
	 * @return 令牌
	 */
	public static String getToken(Integer userId){
		return AESUtils.encrypt(new Date().getTime()+"###"+userId+"###xinxunhui");
	}

	/**
	 * 获取SessionExt对象
	 * @param request
	 * @return
	 */
	public static SessionExt getSessionExt(HttpRequest request) {
		return getSessionExt(request.getHttpSession());
	}

	/**
	 * 获取SessionExt对象
	 * @param session
	 * @return
	 */
	public static SessionExt getSessionExt(HttpSession session) {
		return session.getAttribute(SessionKey.SESSION_EXT, SessionExt.class);
	}

	/**
	 * 根据HttpSession对象获取当前用户的userid
	 * @param session
	 * @return
	 */
	public static Integer getUserIdBySession(HttpSession session) {
		if (session == null) {
			return null;
		}
		SessionExt sessionExt = WebUtils.getSessionExt(session);
		return sessionExt.getUid();
	}

	/**
	 * RequestExt对象是个非常有用的对象
	 * @param request
	 * @return
	 */
	public static RequestExt getRequestExt(HttpRequest request) {
		return (RequestExt) request.getAttribute(RequestKey.REQUEST_EXT);
	}

	/**
	 * 获取IM服务器的端口
	 * @param request
	 * @return
	 * @author: tanyaowu
	 */
	public static int getImServerPort(HttpRequest request) {
		RequestExt requestExt = WebUtils.getRequestExt(request);
		if (requestExt.isFromBrowser()) {
			return Const.ImPort.WS;
		} else {
			return Const.ImPort.APP;
		}
	}

	/**
	 * @param request
	 * @return
	 */
	public static Map<String, Object> getMapParams(HttpRequest request) {
		Map<String, Object> params = new HashMap<>();
		if (request.getParams() != null) {
			Map<String, Object[]> paramArray = request.getParams();
			for (String key : paramArray.keySet()) {
				Object[] param = paramArray.get(key);
				if (param != null && param.length >= 1) {
					params.put(key, param[0]);
				}
			}
		}
		return params;
	}
}
