package com.saga.thread.common;

import java.io.FileReader;
import java.util.ArrayList;
import java.util.Map.Entry;

import org.zillion.util.log.LogUtil;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.saga.util.Constant;
import com.saga.util.HBaseUtil;
import com.zillion.database.agent.ZillionAgent;

public class LoadConfigThread extends Thread {
	ZillionAgent agent;

	public LoadConfigThread() {
		try {
			this.agent = Constant.agent;

		} catch (Exception e) {
			// TODO Auto-generated catch block
			LogUtil.error("LoadConfigThread.Exception: " + e.toString());
			System.exit(0);
		}
	}

	public void run() {
		LogUtil.info("LoadConfigThread" + this.getName() + " Starting ...");
		try {
			Thread.sleep(Constant.Millisecond.MINUTE_30);
		} catch (InterruptedException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		while (true) {
			try {
				// 加载config配置文件
				this.loadConfig();
				// 加载DB点位列表
				this.loadPointList2();
				// 加载采集器配置文件
				// this.loadCJQ();
				Thread.sleep(Constant.Millisecond.MINUTE_5);

			} catch (Exception e) {
				// TODO Auto-generated catch block
				LogUtil.error("LoadConfigThread.run.Exception: " + e.toString());
			}
		}
	}

	private void loadPointList2() {
		// TODO Auto-generated method stub
		for (Entry<String, String> bd : Constant.BuildingDB.entrySet()) {
			try {
				String project = bd.getKey();
				String db = bd.getValue();
				HBaseUtil.LoadDB2Cache_dy_pointlist(Constant.agent, db, "dy_pointlist", project);

			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				LogUtil.error("LoadConfigThread.loadPointList2: " + e.toString());
			}
		}
	}

	private void loadCJQ() {
		// TODO Auto-generated method stub
		// 文件夹命名格式：config+"-"+楼号+"-"+mac序号，示例：config-1101080002-1
		Constant.getConfigFile(LogUtil.GetPath(), "config-", "port");
		// 采集器点位位置信息写入数据库
		for (Entry<String, String> pi : Constant.pointInfo.entrySet()) {
			String building = pi.getKey().split("-")[0];
			String mac = pi.getKey().split("-")[1];
			String node = mac;
			String point = pi.getKey().split("-")[2];
			String busno = pi.getValue().split("-")[0];
			String site = pi.getValue().split("-")[1];
			try {
				LogUtil.info(building + " " + node + " " + point + " " + busno + " " + site);
				HBaseUtil.InsertPointInfo(Constant.agent, Constant.BuildingDB.get(building), building, node, point,
						busno, site);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			// System.out.println(building + node + point + busno +
			// site);
		}
		try {
			Thread.sleep(Constant.Millisecond.MINUTE_1);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void loadConfig() {
		// TODO Auto-generated method stub
		String resource = "/config.json";
		String path = null;
		JsonParser config = new JsonParser();

		try {
			LogUtil.info("System.get " + System.getProperty("user.dir") + resource);
			LogUtil.info("LogUtil.get " + LogUtil.GetPath() + resource);
			path = LogUtil.GetPath() + resource;

			JsonObject object = (JsonObject) config.parse(new FileReader(path));

			JsonArray db = object.get("DB").getAsJsonArray();
			for (int i = 0; i < db.size(); i++) {
				JsonObject subObject = db.get(i).getAsJsonObject();
				if (subObject.get("building") != null) {
					String building = subObject.get("building").getAsString();
					// if(Constant.BuildingDB.containsKey(building)){
					// continue;
					// }
					Constant.BuildingDB.put(building, subObject.get("database").getAsString());

					if (subObject.get("buildingadds") != null) {
						JsonArray buildingAdds = subObject.get("buildingadds").getAsJsonArray();
						for (int j = 0; j < buildingAdds.size(); j++) {
							JsonObject addObject = buildingAdds.get(j).getAsJsonObject();
							String builing_new = addObject.get("building").getAsString();
							if (!Constant.BuildingAdds.containsKey(building)) {
								Constant.BuildingAdds.put(building, new ArrayList<String>());
							}
							if (!exist(builing_new)) {
								Constant.BuildingAdds.get(building).add(builing_new);
								Constant.BuildingConv.put(builing_new, building);
							}
						}

					}
				} else if (subObject.get("insertthread") != null) {
					Constant.InsertThread = subObject.get("insertthread").getAsInt();
				}
			}
			JsonArray port = object.get("Port").getAsJsonArray();
			for (int i = 0; i < port.size(); i++) {
				JsonObject subObject = port.get(i).getAsJsonObject();
				Constant.TypePort.put(subObject.get("type").getAsString(), subObject.get("port").getAsString());
				Constant.PortCompress.put(Integer.valueOf(subObject.get("port").getAsString()),
						subObject.get("Compress").getAsString());
				Constant.PortHistory.put(Integer.valueOf(subObject.get("port").getAsString()),
						subObject.get("History").getAsString());
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private boolean exist(String builing_new) {
		// TODO Auto-generated method stub
		if (Constant.BuildingConv.containsKey(builing_new)) {
			return true;
		}
		return false;
	}
}
