
package org.tio.sitexxx.view;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tio.sitexxx.service.cache.Caches;
import org.tio.sitexxx.service.init.JFInit;
import org.tio.sitexxx.service.init.PropInit;
import org.tio.sitexxx.service.init.RedisInit;
import org.tio.sitexxx.service.ip2region.Ip2RegionInit;
import org.tio.sitexxx.service.service.ConfService;
import org.tio.sitexxx.service.utils.LogUtils;
import org.tio.sitexxx.service.vo.Const;
import org.tio.sitexxx.view.http.WebViewInit;
import org.tio.sitexxx.view.http.WebViewModelGenerator;
import org.tio.sitexxx.view.http.pathmodel.PathModelGenerator;
import org.tio.sitexxx.view.init.TopicInit;
import org.tio.utils.jfinal.P;
import org.tio.utils.json.Json;
import org.tio.webpack.model.Root;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author tanyaowu
 * 2016年8月7日 上午10:58:03
 */
public class WebViewStarter {
	private static Logger				log			= LoggerFactory.getLogger(WebViewStarter.class);
	public static Root					model		= null;
	public static Map<Object, Object>	mapModel	= null;

	public static WebViewModelGenerator tioWebpackModelGenerator;

	/**
	 * @param args
	 * @author tanyaowu
	 * @throws IOException
	 */

	public static void main(String[] args) throws Exception {
		try {

			PropInit.init();

			Ip2RegionInit.init();

			Caches.init();

			// jfinal 初始化
			JFInit.init();

			RedisInit.init(true);
			//
			TopicInit.init();

			initView(args);
		} catch (Throwable e) {
			log.error("", e);
			System.exit(1);
		}
	}

	@SuppressWarnings("unchecked")
	private static void initView1() {
		model = null;
		String modelFilePath = P.get("view.model.config.file", "webpack.json");

		// 使用 ClassLoader 从类路径中读取资源
		ClassLoader classLoader = WebViewStarter.class.getClassLoader();
		try (InputStream inputStream = classLoader.getResourceAsStream(modelFilePath)) {
			if (inputStream == null) {
				throw new FileNotFoundException("Resource not found: " + modelFilePath);
			}

			String content = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8))
					.lines().collect(Collectors.joining("\n"));

			model = Json.toBean(content, Root.class);
			mapModel = Json.toBean(content, Map.class);

			// 获取配置信息
			String title = ConfService.getString("seo.title", "");
			String keywords = ConfService.getString("seo.keywords", "");
			String description = ConfService.getString("seo.description", "");
			String jsVersion = ConfService.getString("js_version", "1");

			// 填充模型数据
			mapModel.put(Const.ModelKey.TITLE, title);
			mapModel.put(Const.ModelKey.KEYWORDS, keywords);
			mapModel.put(Const.ModelKey.DESCRIPTION, description);
			mapModel.put(Const.ModelKey.RES_SERVER, Const.RES_SERVER); // 用户上传数据服务器
			mapModel.put(Const.ModelKey.JS_VERSION, jsVersion); // js版本号

			mapModel.put(Const.ModelKey.API_CONTEXTPATH, org.tio.sitexxx.service.vo.Const.API_CONTEXTPATH);
			mapModel.put(Const.ModelKey.API_SUFFIX, org.tio.sitexxx.service.vo.Const.API_SUFFIX);
			mapModel.put(Const.ModelKey.HTTP_SESSION_COOKIE_NAME, Const.Http.SESSION_COOKIE_NAME);
			mapModel.put(Const.ModelKey.SITE_NAME, ConfService.getString("sitename", "t-io社交IM平台"));

			String tioim_title = ConfService.getString("tioim.title", "ONS");
			String tioim_keywords = ConfService.getString("tioim.keywords", "IM,t-io,高并发,集群,私有部署");
			String tioim_description = ConfService.getString("tioim.description", "ONS是基于t-io研发的类似微信的完整IM，支持集群、语音通话、视频通话、私聊、群聊");
			mapModel.put("tioim_title", tioim_title);
			mapModel.put("tioim_keywords", tioim_keywords);
			mapModel.put("tioim_description", tioim_description);

			// 初始化 WebViewModelGenerator
			tioWebpackModelGenerator = new WebViewModelGenerator(mapModel, new String[] { PathModelGenerator.class.getPackage().getName() });

		} catch (Exception e) {
			e.printStackTrace();
		}
	}


	public static void initView(String[] args) throws Exception {

		initView1();
		LogUtils.logJvmStartTime(WebViewStarter.class.getName() + ".initView1()");

		WebViewInit.init(model, tioWebpackModelGenerator);
		LogUtils.logJvmStartTime(WebViewInit.class.getName() + ".init()");
	}
}
