package org.bench4q.master.api;

import java.util.LinkedList;
import java.util.List;
import java.util.UUID;

import org.bench4q.master.domain.entity.TestPlan;
import org.bench4q.master.domain.service.TestPlanService;
import org.bench4q.share.models.master.ServerStatusModel;
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("/")
public class HomeController extends BaseController {
	private TestPlanService testPlanService;

	private TestPlanService getTestPlanService() {
		return testPlanService;
	}

	@Autowired
	private void setTestPlanService(TestPlanService testPlanService) {
		this.testPlanService = testPlanService;
	}

	@RequestMapping(value = { "/" }, method = RequestMethod.GET)
	@ResponseBody
	public ServerStatusModel currentTasks() {
		List<UUID> tasks = new LinkedList<UUID>();
		for (TestPlan testPlan : this.getTestPlanService()
				.loadAllRunningTestPlans()) {
			tasks.add(UUID.fromString(testPlan.getTestPlanRunId()));
		}
		ServerStatusModel result = new ServerStatusModel();
		result.setRunningTests(tasks);
		return result;
	}
}