
package org.tio.sitexxx.im.server;

import org.tio.core.ChannelContext;
import org.tio.core.Tio;
import org.tio.sitexxx.service.Command;
import org.tio.sitexxx.im.common.ImPacket;
import org.tio.sitexxx.im.common.ImSessionContext;
import org.tio.sitexxx.im.common.bs.wx.WxHandshakeResp;
import org.tio.sitexxx.im.server.utils.ImUtils;

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

/**
 * @author tanyaowu 
 * 2016年9月8日 下午1:35:02
 */
public class Ims {
	static final Map<Integer, Integer> ROBOT_HOUR_COUNT_MAP = new HashMap<>();

	static {
		ROBOT_HOUR_COUNT_MAP.put(0, 10);
		ROBOT_HOUR_COUNT_MAP.put(1, 10);
		ROBOT_HOUR_COUNT_MAP.put(2, 10);
		ROBOT_HOUR_COUNT_MAP.put(3, 10);
		ROBOT_HOUR_COUNT_MAP.put(4, 10);
		ROBOT_HOUR_COUNT_MAP.put(5, 10);
		ROBOT_HOUR_COUNT_MAP.put(6, 10);
		ROBOT_HOUR_COUNT_MAP.put(7, 30);
		ROBOT_HOUR_COUNT_MAP.put(8, 30);
		ROBOT_HOUR_COUNT_MAP.put(9, 40);
		ROBOT_HOUR_COUNT_MAP.put(10, 40);
		ROBOT_HOUR_COUNT_MAP.put(11, 40);
		ROBOT_HOUR_COUNT_MAP.put(12, 40);
		ROBOT_HOUR_COUNT_MAP.put(13, 40);
		ROBOT_HOUR_COUNT_MAP.put(14, 40);
		ROBOT_HOUR_COUNT_MAP.put(15, 40);
		ROBOT_HOUR_COUNT_MAP.put(16, 40);
		ROBOT_HOUR_COUNT_MAP.put(17, 40);
		ROBOT_HOUR_COUNT_MAP.put(18, 40);
		ROBOT_HOUR_COUNT_MAP.put(19, 40);
		ROBOT_HOUR_COUNT_MAP.put(20, 40);
		ROBOT_HOUR_COUNT_MAP.put(21, 40);
		ROBOT_HOUR_COUNT_MAP.put(22, 30);
		ROBOT_HOUR_COUNT_MAP.put(23, 30);
		ROBOT_HOUR_COUNT_MAP.put(24, 10);
	}

	/**
	 *
	 * @param channelContext
	 * @param imPacket
	 */
	public static void send(ChannelContext channelContext, ImPacket imPacket) {
		Tio.send(channelContext, imPacket);
	}

	/**
	 * 发送握手包
	 * @param channelContext
	 * @author tanyaowu
	 */
	public static void sendHandshake(ChannelContext channelContext) {
		ImSessionContext imSessionContext = ImUtils.getImSessionContext(channelContext);
		ImPacket handshakeRespPacket = null;
		if (imSessionContext.isWx()) {
			WxHandshakeResp handshakeResp = new WxHandshakeResp();
			handshakeResp.setCid(channelContext.getId());
			handshakeResp.setIp(channelContext.getClientNode().getIp());
			handshakeRespPacket = new ImPacket(Command.WxHandshakeResp, handshakeResp);
		} else {
			Tio.remove(channelContext, "不支持非wx握手");
			return;
		}
		Ims.send(channelContext, handshakeRespPacket);
	}

	/**
	 *
	 * @param groupid
	 * @param imPacket
	 */
	public static void sendToGroup(Long groupid, ImPacket imPacket) {
		Tio.sendToGroup(TioSiteImServerStarter.tioServerConfigWs, groupid.toString(), imPacket);
	}

	/**
	 *
	 * @param userid
	 * @param imPacket
	 */
	public static void sendToUser(String userid, ImPacket imPacket) {
		Tio.sendToUser(TioSiteImServerStarter.tioServerConfigWs, userid, imPacket);
	}
	
	/**
	 *
	 * @param userid
	 * @param imPacket
	 */
	public static void sendToUser(Integer userid, ImPacket imPacket) {
		sendToUser("" + userid, imPacket);
	}

	/**
	 * 发送到指定的channelContextId
	 * @param channelContextId
	 * @param imPacket
	 */
	public static void sendToId(String channelContextId, ImPacket imPacket) {
		Tio.sendToId(TioSiteImServerStarter.tioServerConfigWs, channelContextId, imPacket);
	}

	/**
	 *
	 * @param imPacket
	 * @author: tanyaowu
	 */
	public static void sendToAll(ImPacket imPacket) {
		Tio.sendToAll(TioSiteImServerStarter.tioServerConfigWs, imPacket);
	}
}
