package org.tio.sitexxx.server.controller.base;

import org.tio.http.server.annotation.RequestPath;
import org.tio.jfinal.plugin.activerecord.Record;
import org.tio.sitexxx.service.cache.CacheConfig;
import org.tio.sitexxx.service.cache.Caches;
import org.tio.sitexxx.service.service.ConfService;
import org.tio.sitexxx.service.vo.Const;
import org.tio.utils.cache.CacheUtils;
import org.tio.utils.cache.ICache;
import org.tio.utils.resp.Resp;

/**
 * @author tanyaowu
 * 2016年6月29日 下午7:53:59
 */
@RequestPath(value = "/config")
public class ConfigController {
	/**
	 * 获取view model数据，主要用于vue
	 * @return
	 * @author tanyaowu
	 */
	@RequestPath(value = "/viewmodel")
	public Resp viewmodel() {
		ICache cache = Caches.getCache(CacheConfig.CONFIG_BASE);
		Record map = CacheUtils.get(cache, "view_model", () -> {
			Record config = new Record();
			config.set(Const.ModelKey.TITLE, ConfService.getString("seo.title", "t-io - 解决其它网络框架没有解决的痛点"));
			config.set(Const.ModelKey.KEYWORDS, ConfService.getString("seo.keywords", "rpc,osc,tcp,im,长连接"));
			config.set(Const.ModelKey.DESCRIPTION, ConfService.getString("seo.description", "解决其它网络框架没有解决的痛点"));
			config.set("tioim_title", ConfService.getString("tioim.title", "信讯会"));
			config.set("tioim_keywords", ConfService.getString("tioim.keywords", "IM,高并发,集群,私有部署"));
			config.set("tioim_description", ConfService.getString("tioim.description", "信讯会是基于研发的类似微信的完整IM，支持集群、语音通话、视频通话、私聊、群聊"));
			config.set(Const.ModelKey.HTTP_SESSION_COOKIE_NAME, Const.Http.SESSION_COOKIE_NAME);
			config.set(Const.ModelKey.API_CONTEXTPATH, org.tio.sitexxx.service.vo.Const.API_CONTEXTPATH);
			config.set(Const.ModelKey.API_SUFFIX, org.tio.sitexxx.service.vo.Const.API_SUFFIX);
			config.set(Const.ModelKey.JS_VERSION, ConfService.getString("js_version", "1"));
			config.set(Const.ModelKey.SITE_NAME, ConfService.getString("sitename", "社交IM平台"));
			config.set("im_heartbeat_timeout", Const.IM_HEARTBEAT_TIMEOUT);
			return config;
		});
		return Resp.ok(map);
	}

	/**
	 * 检查服务
	 */
	@RequestPath(value = "/checkServer")
	public String checkServer() {
		return "success";
	}

	/**
	 * 获取基础配置信息
	 * @return
	 * @throws Exception
	 * @author tanyaowu
	 * 已优化(1)
	 */
	@RequestPath(value = "/base")
	public Resp config() throws Exception {
		ICache cache = Caches.getCache(CacheConfig.CONFIG_BASE);
		Record map = CacheUtils.get(cache, "config_base", ()-> {
			Record config = new Record();
			config.set(Const.ModelKey.RES_SERVER, Const.RES_SERVER); //用户上传数据服务器
			config.set(Const.ModelKey.HTTP_SESSION_COOKIE_NAME, Const.Http.SESSION_COOKIE_NAME);
			config.set("downUrl", ConfService.getString("downUrl", ""));
			config.set(Const.ModelKey.OSS_KEY, ConfService.getString(Const.ModelKey.OSS_KEY, ""));
			config.set(Const.ModelKey.OSS_APP_ID, ConfService.getString(Const.ModelKey.OSS_APP_ID, ""));
			config.set(Const.ModelKey.OSS_ADDRESS, ConfService.getString(Const.ModelKey.OSS_ADDRESS, ""));
			config.set(Const.ModelKey.OSS_BUCKET, ConfService.getString(Const.ModelKey.OSS_BUCKET, ""));
			config.set(Const.ModelKey.OSS_IS_OSS, ConfService.getString(Const.ModelKey.OSS_IS_OSS, ""));
			config.set("policy", ConfService.getString("zhengce", ""));
			config.set("protocol", ConfService.getString("xieyi", ""));
			//1 开启  0 关闭

			//1 开启  0 关闭
			config.set("smscode", ConfService.getString("smscode", "0"));
			//1 展示  0 隐藏
			config.set("showteam", ConfService.getString("showteam", "0"));
			config.set("isUpdate", ConfService.getString("version_an", ""));
			String string = ConfService.getString("keFu", "100000");
			config.set("keUid", string);
			//1 开启  2 关闭
			config.set("openKefu", ConfService.getString("openKefu", "1"));
			config.set("unit", ConfService.getString("danwei", "usdt"));
			config.set(Const.ModelKey.SWITCH_ADDRESS,ConfService.getString(Const.ModelKey.SWITCH_ADDRESS, ""));
			config.set(Const.ModelKey.SOCKET_ADDRESS, ConfService.getString(Const.ModelKey.SOCKET_ADDRESS, ""));
			config.set("im_heartbeat_timeout", Const.IM_HEARTBEAT_TIMEOUT);
			config.set("isShort", ConfService.getString("smscode", "1"));
			config.set("invitecode", ConfService.getInt("invitecode", 0));
			config.set("uploadImage", 1);
			config.set("uploadI", ConfService.getInt("uploadImage", 0));
			config.set("isShowTask", ConfService.getInt("isShowTask", 0));
			config.set("isShowAi", ConfService.getInt("isShowAi", 0));
			return config;
		});
		return Resp.ok(map);
	}
}
