package org.bench4q.agent.api;

import java.util.ArrayList;
import java.util.List;

import org.bench4q.agent.plugin.BehaviorInfo;
import org.bench4q.agent.plugin.ParameterInfo;
import org.bench4q.agent.plugin.PluginInfo;
import org.bench4q.agent.plugin.PluginManager;
import org.bench4q.share.models.agent.BehaviorInfoModel;
import org.bench4q.share.models.agent.ParameterInfoModel;
import org.bench4q.share.models.agent.PluginInfoListModel;
import org.bench4q.share.models.agent.PluginInfoModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/plugin")
public class PluginController {
	private PluginManager pluginManager;

	public PluginManager getPluginManager() {
		return pluginManager;
	}

	@Autowired
	public void setPluginManager(PluginManager pluginManager) {
		this.pluginManager = pluginManager;
	}

	@RequestMapping(method = RequestMethod.GET)
	@ResponseBody
	public PluginInfoListModel list() {
		List<PluginInfo> pluginInfos = this.getPluginManager().getPluginInfo();
		PluginInfoListModel pluginInfoListModel = new PluginInfoListModel();
		pluginInfoListModel.setPlugins(new ArrayList<PluginInfoModel>());
		for (PluginInfo pluginInfo : pluginInfos) {
			PluginInfoModel pluginInfoModel = buildPluginInfoModel(pluginInfo);
			pluginInfoListModel.getPlugins().add(pluginInfoModel);
		}
		return pluginInfoListModel;
	}

	private PluginInfoModel buildPluginInfoModel(PluginInfo pluginInfo) {
		PluginInfoModel pluginInfoModel = new PluginInfoModel();
		pluginInfoModel.setName(pluginInfo.getName());
		pluginInfoModel.setParameters(new ArrayList<ParameterInfoModel>());
		for (ParameterInfo param : pluginInfo.getParameters()) {
			ParameterInfoModel model = buildParameterInfoModel(param);
			pluginInfoModel.getParameters().add(model);
		}
		pluginInfoModel.setBehaviors(new ArrayList<BehaviorInfoModel>());
		for (BehaviorInfo behaviorInfo : pluginInfo.getBehaviors()) {
			BehaviorInfoModel behaviorInfoModel = buildBehaviorInfoModel(behaviorInfo);
			pluginInfoModel.getBehaviors().add(behaviorInfoModel);
		}
		return pluginInfoModel;
	}

	private BehaviorInfoModel buildBehaviorInfoModel(BehaviorInfo behaviorInfo) {
		BehaviorInfoModel behaviorInfoModel = new BehaviorInfoModel();
		behaviorInfoModel.setName(behaviorInfo.getName());
		behaviorInfoModel.setParameters(new ArrayList<ParameterInfoModel>());
		for (ParameterInfo param : behaviorInfo.getParameters()) {
			ParameterInfoModel model = buildParameterInfoModel(param);
			behaviorInfoModel.getParameters().add(model);
		}
		return behaviorInfoModel;
	}

	private ParameterInfoModel buildParameterInfoModel(ParameterInfo param) {
		ParameterInfoModel model = new ParameterInfoModel();
		model.setName(param.getName());
		model.setType(param.getType());
		return model;
	}

}
