package com.icontrols.bridge;

import java.util.ArrayList;
import java.util.HashMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.icontrols.bridge.common.COMMON_INFO;
import com.icontrols.bridge.common.CommonUtil;
import com.icontrols.bridge.dto.ServiceDetail;
import com.icontrols.bridge.wallpad.IMapMultiSocket;

public class Main {
	private static Logger logger = LoggerFactory.getLogger(Main.class);

	public static void main(String[] args) {

		logger.info("*************************************************************************");
		logger.info("*                                                                       *");
		logger.info("*  *****   *         *******   *          **       *      *    *    *   *");
		logger.info("*  *       *            *      *          **       *      *    *    *   *");
		logger.info("*  *       ***         * *     *         *  *   ****      ******    *   *");
		logger.info("*  *       *          *   *    *        *    *     *      *    *  ***   *");
		logger.info("*  *****   *         *     *   *       *      *    *      *    *    *   *");
		logger.info("*       *                      *      *        *   *      ******    *   *");
		logger.info("*       *                                                               *");
		logger.info("*       *******                                                         *");
		logger.info("*                                                       BRIDGE BY PANGS *");
		logger.info("*************************************************************************");

		// 초기 설정
		String str = CommonUtil.readFile(CommonUtil.getPropFileDir());
		JsonObject object = CommonUtil.getJsonObject(str);

		COMMON_INFO.propList = (JsonArray) object.get("services");

		if (COMMON_INFO.propList == null) {
			logger.error("설정정보를 가져올 수 없습니다......");
			return;
		}
		COMMON_INFO.serviceDtailList = new HashMap<String, ServiceDetail>();

		for (JsonElement jsonObj : COMMON_INFO.propList) {
			ServiceDetail service = new Gson().fromJson(jsonObj.toString(), ServiceDetail.class);
			COMMON_INFO.serviceDtailList.put(service.getName(), service);
		}

		if (COMMON_INFO.propList.size() == 0) {
			logger.error("설정정보가 존재하지 않습니다....");
			return;
		}

		StringBuffer bufStr = new StringBuffer();
		bufStr.append("설정된 서비스 \n");
		for (String key : COMMON_INFO.serviceDtailList.keySet())
			bufStr.append(COMMON_INFO.serviceDtailList.get(key).toString()).append("\n");

		logger.info(bufStr.toString());

		// 소켓 서버 시작
		new IMapMultiSocket();
	}

}
