package com.ht.web.hessian;

import java.net.MalformedURLException;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;

import com.caucho.hessian.client.HessianProxyFactory;
import com.ht.ui.MainApplication;
import com.ht.util.UserSession;
import com.ht.web.remoting.AlarmService;
import com.ht.web.remoting.ReportService;
import com.ht.web.remoting.SVGService;
import com.ht.web.remoting.TagService;
import com.ht.web.remoting.TagVarService;
import com.ht.web.remoting.UserService;

public enum HessianFactoryManger {
	INSTANCE;

	private static final String USER_SERVICE = "/remoting/UserService";
	private static final String TAG_SERVICE = "/remoting/TagService";
	private static final String TAG_VAR_SERVICE = "/remoting/TagVarService";
	private static final String ALARM_SERVICE = "/remoting/AlarmService";
	private static final String REPORT_SERVICE = "/remoting/ReportService";
	private static final String SVG_SERVICE = "/remoting/SVGService";

	private MyHessianProxyFactory factory = new MyHessianProxyFactory();

	private UserService userService;
	private TagService tagService;
	private TagVarService tagVarService;
	private AlarmService alarmService;
	private ReportService reportService;
	private SVGService svgService;

	private HessianFactoryManger() {
		factory.setUser("htck");
		factory.setPassword("htck2000");
		factory.setConnectTimeout(10);
		
		init(MainApplication.getMainApp().hostProperty().getValue());
		MainApplication.getMainApp().hostProperty().addListener(new ChangeListener<String>() {
			@Override
			public void changed(ObservableValue<? extends String> observable,
					String oldValue, String newValue) {
				if (newValue != null) {
					init(newValue);
				}
			}
		});
	}

	private void init(String host) {
		final String URL_BASE = "http://" + host;
		System.out.println(URL_BASE);
		try {
			userService = (UserService) factory.create(UserService.class, URL_BASE + USER_SERVICE);
			tagService = (TagService) factory.create(TagService.class, URL_BASE + TAG_SERVICE);
			tagVarService = (TagVarService) factory.create(TagVarService.class, URL_BASE + TAG_VAR_SERVICE);
			alarmService = (AlarmService) factory.create(AlarmService.class, URL_BASE + ALARM_SERVICE);
			reportService = (ReportService) factory.create(ReportService.class, URL_BASE + REPORT_SERVICE);
			svgService = (SVGService) factory.create(SVGService.class, URL_BASE + SVG_SERVICE);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
	}
	
	public SVGService getSvgService() {
		return svgService;
	}
	
	public UserService getUserService() {
		UserSession.INSTANCE.updateUserActivity();
		return userService;
	}
	
	public TagService getTagService() {
		UserSession.INSTANCE.updateUserActivity();
		return tagService;
	}
	
	public TagVarService getTagVarService() {
		UserSession.INSTANCE.updateUserActivity();
		return tagVarService;
	}
	
	public AlarmService getAlarmService() {
		return alarmService;
	}
	
	public ReportService getReportService() {
		return reportService;
	}
	
}
