package com.ht.web.cometd.service;

import static com.ht.web.cometd.util.ServiceConst.VAR_TAG;
import static com.ht.web.cometd.util.ServiceConst.WARN;

import java.util.Collections;
import java.util.HashSet;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import javafx.application.Platform;

import org.cometd.bayeux.Message;
import org.cometd.bayeux.client.ClientSession;
import org.cometd.java.annotation.Session;
import org.cometd.java.annotation.Subscription;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ht.data.RealTimeDataManager;
import com.ht.ui.MainApplication;
import com.ht.web.mongo.domain.Alarm;
import com.ht.web.mongo.domain.Fault;
import com.ht.web.mongo.domain.OnOffShift;
import com.ht.web.mongo.domain.SOE;

@org.cometd.java.annotation.Service
public class TagVarCometdService {

	private static final Logger log = LoggerFactory
			.getLogger(TagVarCometdService.class);

	private Set<String> dataNames = Collections
			.synchronizedSet(new HashSet<String>());
	@Session
	private ClientSession client;

	@Subscription(VAR_TAG)
	public void tagVarChanged(Message message) {
		log.debug("收到标签变量通知");

		Map<String, Object> data = message.getDataAsMap();
		for (Entry<String, Object> entry : data.entrySet()) {
			// entry.getKey()
			if (log.isDebugEnabled()) {
				log.debug("{} : {}", entry.getKey(), entry.getValue().toString());
			}
			RealTimeDataManager.INSTANCE.updateVarValue(entry.getKey(),
					entry.getValue());
		}
	}

	@Subscription(WARN)
	public void warnCatch(Message message) {

		final Map<String, Object> input = message.getDataAsMap();
		final String name = (String) input.get("warnType");
		log.debug("收到警告信息: {}", name);
		if (name == null) {
			log.error("警告类型为null.");
			return;
		}
		Platform.runLater(new Runnable() {

			@Override
			public void run() {
				if (name.equals("alarm")) {

					Alarm alarm = (Alarm) input.get("data");
					if (alarm != null) {
						//MainApplication.getMainApp().getAlarmInfoWnd().fireAlarmEvent(alarm);
					}

				} else if (name.equals("fault")) {

					Fault fault = (Fault) input.get("data");
					if (fault != null) {
					}

				} else if (name.equals("soe")) {
					SOE soe = (SOE) input.get("data");
					if (soe != null) {
					}
				} else if (name.equals("onOffShift")) {
					OnOffShift onOffShift = (OnOffShift) input.get("data");
					if (onOffShift != null) {
					}
				}
			}
		});

	}

	public void addDataToListen(final String name) {
		dataNames.add(name);
	}

	public void removeDataToListen(final String name) {
		client.batch(new Runnable() {

			@Override
			public void run() {
				dataNames.remove(name);
			}
		});
	}

//	/**
//	 * 获取遥测变量(一般为模拟量)从起始时间(starttime)到结束时间(endtime)内的历史数据
//	 * @param tagVarName 全局变量名, 注意这里不是变量路径
//	 * @param starttime  起始时间
//	 * @param endtime	  结束时间	
//	 * @param listener	  回调函数
//	 */
//	public void getTagVarHistoryData(String tagVarName, Date starttime, Date endtime, UserActionResponseListener listener) {
//		log.debug("获取历史数据: {}", tagVarName);
//		
//		messageListener.setUserActionResonseListener(listener);
//
//		Map<String, Object> data = new HashMap<String, Object>();
//		data.put("tagVarName", tagVarName);
//		data.put("starttime", starttime.getTime());
//		data.put("endtime", endtime.getTime());
//		client.getChannel(SERVICE_TAG_GET_HISTORY_VALUE).publish(data);
//	}
}
