package org.bench4q.master.domain.service;

import java.util.LinkedList;
import java.util.List;
import java.util.Set;

import org.bench4q.master.domain.repository.PluginRepository;
import org.bench4q.master.domain.entity.plugin.BehaviorInfo;
import org.bench4q.master.domain.entity.plugin.PluginUI;
import org.bench4q.master.domain.factory.PluginDomainFactory;
import org.bench4q.master.domain.factory.PluginEntityFactory;
import org.bench4q.share.models.master.plugin.BehaviorInfoModel;
import org.bench4q.share.models.master.plugin.PluginUIModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class PluginService {
	private PluginRepository pluginRepository;
	private PluginEntityFactory pluginEntityFactory;
	private PluginDomainFactory pluginDomainFactory;

	public PluginRepository getPluginRepository() {
		return pluginRepository;
	}

	@Autowired
	private void setPluginRepository(PluginRepository pluginRepository) {
		this.pluginRepository = pluginRepository;
	}

	public PluginEntityFactory getPluginEntityFactory() {
		return pluginEntityFactory;
	}

	@Autowired
	private void setPluginEntityFactory(PluginEntityFactory pluginEntityFactory) {
		this.pluginEntityFactory = pluginEntityFactory;
	}

	public PluginDomainFactory getPluginDomainFactory() {
		return pluginDomainFactory;
	}

	@Autowired
	public void setPluginDomainFactory(PluginDomainFactory pluginDomainFactory) {
		this.pluginDomainFactory = pluginDomainFactory;
	}

	public boolean addPlugin(String pluginUIContent) {
		return this.getPluginRepository().attatch(
				this.getPluginEntityFactory().createPluginUIWithOutId(
						pluginUIContent));
	}

	public boolean deletePlugin(String pluginName) {
		return this.getPluginRepository().detach(pluginName);
	}

	public List<String> getPluginNameList() {
		List<PluginUIModel> pluginUIModels = this.loadPluginUIs();
		if (pluginUIModels == null)
			return null;
		List<String> pluginNameList = new LinkedList<String>();
		for (PluginUIModel pluginUIModel : this.loadPluginUIs()) {
			pluginNameList.add(pluginUIModel.getPluginInfoModel().getName());
		}
		return pluginNameList;

	}

	public List<PluginUIModel> loadPluginUIs() {
		List<PluginUI> pluginUIs = this.getPluginRepository().loadPlugins();
		if (pluginUIs == null)
			return null;
		List<PluginUIModel> pluginUIModels = new LinkedList<PluginUIModel>();
		for (PluginUI pluginUI : pluginUIs) {
			pluginUIModels.add(this.getPluginDomainFactory()
					.createPluginUIModelwithOutBehaviors(pluginUI));
		}

		return pluginUIModels;
	}

	public List<BehaviorInfoModel> loadBehaviorInfoModels(String pluginName) {

		Set<BehaviorInfo> behaviorInfos = this.getPluginRepository()
				.loadBehaviorInfos(pluginName);
		if (behaviorInfos == null)
			return null;
		return this.getPluginDomainFactory().createBehaviorInfoModels(
				this.getPluginRepository().loadBehaviorInfos(pluginName));

	}

	public boolean isPluginExist(String pluginName) {
		return this.getPluginRepository().isExist(pluginName);
	}

	public PluginUIModel getPluginUIModel(String pluginName) {
		PluginUI pluginUI = this.getPluginRepository().getPluginUI(pluginName);
		if (pluginUI == null) {
			return null;
		}
		return this.getPluginDomainFactory()
				.createPluginUIModelwithOutBehaviors(pluginUI);

	}
}
