package org.bench4q.web.masterMessager;

import java.util.LinkedList;
import java.util.Map;
import javax.xml.bind.JAXBException;
import org.bench4q.share.communication.HttpRequester.HttpResponse;
import org.bench4q.share.helper.MarshalHelper;
import org.bench4q.share.models.master.plugin.BehaviorInfoModel;
import org.bench4q.share.models.master.plugin.PluginResponseModel;
import org.bench4q.share.models.master.plugin.PluginUIModel;
import org.springframework.stereotype.Component;

@Component
public class PluginMessager extends MasterMessager {
	public PluginMessager() {
		super(MasterAddressManamger.getMasterAddress() + "/plugin");
	}

	public PluginResponseModel loadPluginUIModels(String accessToken) {
		String url = this.getBaseUrl() + "/loadPluginList";
		PluginResponseModel pluginResponseModel = getPluginResponseModelByGet(
				url, null, accessToken);
		if (pluginResponseModel.getPluginUIModels() == null) {
			pluginResponseModel
					.setPluginUIModels(new LinkedList<PluginUIModel>());
		}
		return pluginResponseModel;
	}

	public PluginResponseModel loadPluginNameList(String accessToken) {

		String url = this.getBaseUrl() + "/loadPluginNameList";
		PluginResponseModel pluginResponseModel = getPluginResponseModelByGet(
				url, null, accessToken);
		if (pluginResponseModel.getPluginList() == null) {
			pluginResponseModel.setPluginList(new LinkedList<String>());
		}
		return pluginResponseModel;
	}

	public PluginResponseModel getPluginInfoModel(String accessToken,
			String pluginName) {
		String url = this.getBaseUrl() + "/getPlugin" + "/" + pluginName;
		return getPluginResponseModelByGet(url, null, accessToken);

	}

	public PluginResponseModel loadBehaviors(String accessToken,
			String pluginName) {
		String url = this.getBaseUrl() + "/loadBehaviors" + "/" + pluginName;
		PluginResponseModel pluginResponseModel = this
				.getPluginResponseModelByGet(url, null, accessToken);

		if (pluginResponseModel.getBehaviorInfoModels() == null) {
			pluginResponseModel
					.setBehaviorInfoModels(new LinkedList<BehaviorInfoModel>());
		}
		return pluginResponseModel;
	}

	public PluginResponseModel addPlugin(String accessToken, String plugin) {
		String url = this.getBaseUrl() + "/addPlugin";
		HttpResponse httpResponse = null;
		try {

			httpResponse = this.getHttpRequester().sendPutXml(url, plugin,
					makeAccessTockenMap(accessToken));
			if (!validateHttpResponse(httpResponse))
				return null;
			return extractPluginResponseModel(httpResponse);
		} catch (Exception e) {
			this.handleException(httpResponse, e);
			return createFailPluginResponseModel();
		}
	}

	public PluginResponseModel deletePlugin(String accessToken,
			String pluginName) {
		String url = this.getBaseUrl() + "/deletePlugin" + "/" + pluginName;
		return getPluginResponseModelByGet(url, null, accessToken);
	}

	private PluginResponseModel getPluginResponseModelByGet(String url,
			Map<String, String> params, String accessToken) {

		HttpResponse httpResponse = null;
		try {
			httpResponse = this.getHttpRequester().sendGet(url, null,
					makeAccessTockenMap(accessToken));

			if (!validateHttpResponse(httpResponse)) {
				logger.info("response is null");
				return createFailPluginResponseModel();
			}

			return extractPluginResponseModel(httpResponse);

		} catch (Exception e) {
			this.handleException(httpResponse, e);

			return createFailPluginResponseModel();
		}

	}

	private PluginResponseModel createFailPluginResponseModel() {
		PluginResponseModel pluginResponseModel = new PluginResponseModel();
		pluginResponseModel.setSuccess(false);
		pluginResponseModel.setFailMessage("");
		return pluginResponseModel;
	}

	private PluginResponseModel extractPluginResponseModel(
			HttpResponse httpResponse) throws JAXBException {
		PluginResponseModel pluginResponseModel = (PluginResponseModel) MarshalHelper
				.unmarshal(PluginResponseModel.class, httpResponse.getContent());
		if (!pluginResponseModel.isSuccess()) {
			logger.info(pluginResponseModel.getFailMessage());
		}
		return pluginResponseModel;

	}

}
