package org.bench4q.web.service;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.bench4q.share.models.agent.ParameterModel;
import org.bench4q.share.models.agent.RunScenarioModel;
import org.bench4q.share.models.agent.scriptrecord.BatchModel;
import org.bench4q.share.models.agent.scriptrecord.BehaviorModel;
import org.bench4q.share.models.agent.scriptrecord.PageModel;
import org.bench4q.share.models.agent.scriptrecord.UsePluginModel;
import org.bench4q.share.models.master.plugin.BehaviorInfoModel;
import org.bench4q.share.models.master.plugin.ParamInfoModel;
import org.bench4q.share.models.master.plugin.PluginInfoModel;
import org.bench4q.web.masterMessager.PluginMessager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class ScriptService {

	private PluginMessager pluginMessager;

	public PluginMessager getPluginMessager() {
		return pluginMessager;
	}

	@Autowired
	public void setPluginMessager(PluginMessager pluginMessager) {
		this.pluginMessager = pluginMessager;
	}

	public RunScenarioModel createRunScenarioModel(
			List<UsePluginModel> usePlugins, List<BehaviorModel> behaviorModels) {
		RunScenarioModel runScenarioModel = new RunScenarioModel();
		runScenarioModel.setUsePlugins(usePlugins);
		List<PageModel> pageModels = new LinkedList<PageModel>();
		int count = 0;
		int idGenerator =1;
		for (BehaviorModel behaviorModel : behaviorModels) {
			List<BehaviorModel> behaviorList = new LinkedList<BehaviorModel>();
			behaviorModel.setId(count);
			behaviorList.add(behaviorModel);
			BatchModel batchModel = new BatchModel(idGenerator++);
			batchModel.setBehaviors(behaviorList);
			batchModel.setChildId(-1);
			batchModel.setId(count);
			batchModel.setParentId(-1);
			List<BatchModel> batchModels = new LinkedList<BatchModel>();
			batchModels.add(batchModel);
			PageModel pageModel = new PageModel(idGenerator++);
			pageModel.setBatches(batchModels);
			pageModels.add(pageModel);
			count++;
		}
		runScenarioModel.setPages(pageModels);
		return runScenarioModel;
	}

	public Map<UsePluginModel, PluginInfoModel> createUsePluginInfoModelsWithValue(
			List<UsePluginModel> usePluginModels, String accessToken) {
		Map<UsePluginModel, PluginInfoModel> map = new HashMap<UsePluginModel, PluginInfoModel>();
		for (UsePluginModel usePluginModel : usePluginModels) {
			map.put(usePluginModel,
					createPluginInfoModelWithValue(usePluginModel, accessToken));
		}
		return map;
	}

	public Map<BehaviorModel, BehaviorInfoModel> createBehaviorInfoModelsWithValue(
			RunScenarioModel runScenarioModel, String accessToken) {
		Map<String, String> behaviorPluginMap = getBehaviorUsePluginName(runScenarioModel);
		Map<BehaviorModel, BehaviorInfoModel> map = new HashMap<BehaviorModel, BehaviorInfoModel>();
		for (PageModel pageModel : runScenarioModel.getPages()) {
			BehaviorModel behaviorModel = pageModel.getBatches().get(0)
					.getBehaviors().get(0);
			System.out.println(behaviorPluginMap.get(behaviorModel.getUse()));
			map.put(behaviorModel,
					createBehaviorModelWithValue(accessToken, behaviorModel,
							behaviorPluginMap.get(behaviorModel.getUse())));
		}
		return map;

	}

	private PluginInfoModel createPluginInfoModelWithValue(
			UsePluginModel usePluginModel, String accessToken) {
		PluginInfoModel pluginInfoModel = this.getPluginMessager()
				.getPluginInfoModel(accessToken, usePluginModel.getName())
				.getPluginInfoModel();
		
		pluginInfoModel.setParamInfoModels(createParameterModelsWithValue(
				usePluginModel.getParameters(),
				pluginInfoModel.getParamInfoModels()));
		return pluginInfoModel;

	}

	private BehaviorInfoModel createBehaviorModelWithValue(String accessToken,
			BehaviorModel behaviorModel, String pluginName) {

		List<BehaviorInfoModel> behaviorInfoModels = this.getPluginMessager()
				.loadBehaviors(accessToken, pluginName)
				.getBehaviorInfoModels();
		BehaviorInfoModel behaviorInfoModel = null;
		for (BehaviorInfoModel behaviorInfoModelTemp : behaviorInfoModels) {
			if (behaviorInfoModelTemp.getName().equals(behaviorModel.getName())) {
				behaviorInfoModel = behaviorInfoModelTemp;
			}
		}
		behaviorInfoModel.setParamInfoModels(createParameterModelsWithValue(
				behaviorModel.getParameters(),
				behaviorInfoModel.getParamInfoModels()));
		return behaviorInfoModel;
	}

	private Map<String, String> getBehaviorUsePluginName(
			RunScenarioModel runScenarioModel) {
		Map<String, String> map = new HashMap<String, String>();
		List<UsePluginModel> usePluginModels = runScenarioModel.getUsePlugins();
		for (UsePluginModel usePluginModel : usePluginModels) {
			map.put(usePluginModel.getId(), usePluginModel.getName());
		}
		return map;

	}

	private List<ParamInfoModel> createParameterModelsWithValue(
			List<ParameterModel> parameters,
			List<ParamInfoModel> paramInfoModels) {
		if (parameters == null) {
			return paramInfoModels;
		}
		for (ParameterModel parameterModel : parameters) {
			for (ParamInfoModel paramInfoModel : paramInfoModels) {
				if (parameterModel.getKey() == paramInfoModel.getName()) {
					paramInfoModel.setValue(parameterModel.getValue());
				}
			}
		}
		return paramInfoModels;
	}
}
