package org.bench4q.master.unitTest.testHelper;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.bench4q.master.domain.entity.plugin.PluginUI;
import org.bench4q.master.domain.factory.PluginEntityFactory;
import org.bench4q.master.domain.repository.PluginRepository;
import org.bench4q.master.domain.service.PluginService;
import org.springframework.stereotype.Component;

@Component
public class Test_PluginHelper {

	public static String filePath(String fileName) {

		String fileSeparator = System.getProperty("file.separator");
		String filePath = System.getProperty("user.dir") + fileSeparator
				+ "pluginUI" + fileSeparator + fileName;
		return filePath;
	}

	public static PluginUI createPluginUI(String fileName) throws IOException {
		PluginEntityFactory pluginEntityFactory = new PluginEntityFactory();
		String filePath = filePath(fileName);
		File file = new File(filePath);
		String uiContent = FileUtils.readFileToString(file);
		PluginUI pluginUI = pluginEntityFactory
				.createPluginUIWithOutId(uiContent);
		return pluginUI;
	}

	public String addPlugin(String fileName, PluginRepository pluginRepository) {
		try {
			PluginUI pluginUI = createPluginUI(fileName);
			pluginRepository.attatch(pluginUI);

			return pluginUI.getPluginInfo().getName();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}

	}

	public String addPlugin(String fileName, PluginService pluginService,
			String pluginName) {
		try {
			String pluginUI = FileUtils.readFileToString(new File(
					filePath(fileName)));
			pluginService.addPlugin(pluginUI);
			return pluginName;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}

	}

	public void clearPlugin(String pluginName, PluginRepository pluginRepository) {
		pluginRepository.detach(pluginName);

	}
}
