package org.bench4q.master.domain.service;

import java.io.IOException;
import java.io.InputStream;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.Properties;
import java.util.UUID;

import org.apache.log4j.Logger;
import org.bench4q.master.domain.entity.Monitor;
import org.bench4q.master.domain.entity.TestPlan;
import org.bench4q.master.domain.entity.TestPlanScript;
import org.bench4q.master.domain.entity.User;
import org.bench4q.master.domain.factory.BusinessModelMapFactory;
import org.bench4q.master.domain.repository.TestPlanRepository;
import org.bench4q.share.enums.master.TestPlanStatus;
import org.bench4q.share.helper.MarshalHelper;
import org.bench4q.share.models.master.MonitorModel;
import org.bench4q.share.models.master.ScriptHandleModel;
import org.bench4q.share.models.master.TestPlanDBModel;
import org.bench4q.share.models.master.TestPlanResultModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class TestPlanService {
	private BusinessModelMapFactory businessMapFactory;
	private TestPlanRepository testPlanRepository;
	
	private Properties properties = null;

	private TestPlanRepository getTestPlanRepository() {
		return testPlanRepository;
	}

	@Autowired
	private void setTestPlanRepository(TestPlanRepository testPlanRepository) {
		this.testPlanRepository = testPlanRepository;
	}

	private BusinessModelMapFactory getBusinessMapFactory() {
		return businessMapFactory;
	}

	@Autowired
	private void setBusinessMapFactory(
			BusinessModelMapFactory businessMapFactory) {
		this.businessMapFactory = businessMapFactory;
	}

	public List<TestPlan> loadTestPlans(User user) {
		return this.getTestPlanRepository().loadEntities(user);
	}

	public boolean removeTestPlanInDB(int testPlanId) {
		return this.getTestPlanRepository().detach(testPlanId);
	}

	public TestPlanStatus queryTestPlanStatus(UUID testPlanRunID) {
		return TestPlanStatus.valueOf(this.getTestPlanRepository()
				.getTestPlanInDomainBy(testPlanRunID).getCurrentStatus());
	}

	public TestPlanDBModel getTestPlanDBModel(UUID runId) {
		return this.getBusinessMapFactory().toModel(
				this.getTestPlanRepository().getTestPlanInDomainBy(runId));
	}

	public TestPlanResultModel buildResultModel(UUID runId) {
		TestPlanResultModel resultModel = new TestPlanResultModel();
		TestPlan testPlan = this.getTestPlanRepository().getTestPlanInDomainBy(
				runId);
		resultModel.setTestPlanName(testPlan.getName());
		resultModel.setCurrentStatus(TestPlanStatus.valueOf(testPlan
				.getCurrentStatus()));
		resultModel.setTestPlanId(runId);
		resultModel.setCreateDateTime(testPlan.getCreateDateTime());

		List<ScriptHandleModel> scripts = new LinkedList<ScriptHandleModel>();
		for (TestPlanScript testPlanScript : testPlan.getTestPlanScripts()) {
			ScriptHandleModel scriptHandleModel = new ScriptHandleModel(
					testPlanScript.getScript().getId(), testPlanScript
							.getScript().getName());
			scripts.add(scriptHandleModel);
		}
		resultModel.setScripts(scripts);
		List<MonitorModel> monitorModels = new LinkedList<MonitorModel>();
		for (Monitor monitor : testPlan.getMonitors()) {
			monitorModels.add(BusinessModelMapFactory.toModel(monitor));
		}
		resultModel.setMonitorModels(monitorModels);
		resultModel.setExecuteTime(testPlan.getTestPlanScripts().iterator()
				.next().getPlanedConfig().getExecuteRange());
		Logger.getLogger(this.getClass()).info(
				"runningInfo : " + MarshalHelper.tryMarshal(resultModel));
		return resultModel;
	}

	public Collection<TestPlan> loadAllRunningTestPlans() {
		return this.getTestPlanRepository().loadRunningTestPlans();
	}
	public String[] loadFilterTypeList() {
		return getFilterTypeList();
	}
	
	private String[] getFilterTypeList(){
		if(properties == null){
			try {
				properties = new Properties();
				InputStream inputStream = ScriptService.class.getClassLoader()
						.getResourceAsStream(
								"org/bench4q/master/config/FilterType.properties");
				properties.load(inputStream);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return properties.getProperty("filter.type.list","").split(",");
	}
}
