
package org.tio.sitexxx.server.controller;

import cn.hutool.core.util.StrUtil;
import org.tio.sitexxx.server.utils.WebUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tio.clu.client.CluClient;
import org.tio.clu.common.bs.BestNodeResp;
import org.tio.core.Node;
import org.tio.http.common.HttpRequest;
import org.tio.http.server.annotation.RequestPath;
import org.tio.sitexxx.im.server.TioSiteImServerStarter;
import org.tio.sitexxx.service.service.ConfService;
import org.tio.sitexxx.service.vo.Const;
import org.tio.sitexxx.service.vo.RequestExt;
import org.tio.utils.jfinal.P;
import org.tio.utils.resp.Resp;

import java.util.ArrayList;
import java.util.List;
/**
 * 
 * @author tanyaowu
 */
@RequestPath(value = "/im")
public class ImController {
	
	private static Logger log = LoggerFactory.getLogger(ImController.class);

	/**
	 * 获取im服务器地址
	 * @param request
	 * @return
	 */
	@RequestPath(value = "/imserver")
	public Resp imserver(HttpRequest request) {
		RequestExt requestExt = WebUtils.getRequestExt(request);
		String protocol = requestExt.isFromBrowser() ? Const.Protocol.IM_WS : Const.Protocol.IM_APP;
		Integer currUid = WebUtils.currUserId(request);

		Node node;
		BestNodeResp bestNodeResp = null;

		if (Const.USE_TIO_CLU) {
			try {
				bestNodeResp = CluClient.bestNodeReq(TioSiteImServerStarter.cluClientStarter, currUid, protocol, 1500L);
			} catch (Exception e) {
				log.error("获取最佳服务器时异常", e);
			}
		}

		if (bestNodeResp != null && bestNodeResp.isOk()) {
			node = bestNodeResp.getNode();
		} else {
			node = new Node(Const.MY_IP, WebUtils.getImServerPort(request));
			if (!Const.IM_SSL_FLAG) {
				node.setSsl((byte) Const.YesOrNo.NO);
			}
		}

		if (StrUtil.isNotBlank(P.get("tio.clu.select.sever.force"))) {
			node.setIp(P.get("tio.clu.select.sever.force"));
		}

		node.setTimeout(Const.IM_HEARTBEAT_TIMEOUT);
		return Resp.ok().data(node);
	}

	/**
	 *  获取turnserver
	 * @return
	 * @throws Exception
	 * @author tanyaowu
	 */
	@RequestPath(value = "/turnserver")
	public Resp turnserver() {
		TurnServer turnServer = new TurnServer();
		turnServer.setUrls(ConfService.getString("turnserver.url", "turn:112.74.183.177:3478"));  //turn:112.74.183.177:3478 stun:112.74.183.177:3478
		turnServer.setCredential(ConfService.getString("turnserver.credential", "8654269566"));
		turnServer.setUsername(ConfService.getString("turnserver.username", "tan"));
		
		List<TurnServer> list = new ArrayList<>(1);
		list.add(turnServer);
		return Resp.ok().data(list);
	}
}
