package com.athena.meerkat.controller.web.tomcat;

import java.util.Date;
import java.util.List;

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;

import com.athena.meerkat.controller.MeerkatConstants;
import com.athena.meerkat.controller.common.State;
import com.athena.meerkat.controller.web.common.model.SimpleJsonResponse;
import com.athena.meerkat.controller.web.entities.TomcatApplication;
import com.athena.meerkat.controller.web.entities.TomcatDomain;
import com.athena.meerkat.controller.web.entities.TomcatInstance;
import com.athena.meerkat.controller.web.tomcat.services.ApplicationService;
import com.athena.meerkat.controller.web.tomcat.services.TomcatDomainService;

@Controller
@RequestMapping("application")
public class ApplicationController {

	@Autowired
	private ApplicationService appService;
	@Autowired
	private TomcatDomainService domainService;

	@RequestMapping(value = "/deploy", method = RequestMethod.POST)
	public @ResponseBody SimpleJsonResponse deploy(SimpleJsonResponse json,
			TomcatApplication app, int domainId) {
		TomcatDomain domain = domainService.getDomain(domainId);
		if (domain == null) {
			json.setMsg("Domain does not exist.");
			json.setSuccess(false);
			return json;
		}
		app.setState(State.APP_STATE_STOPPED);
		app.setLastModifiedTime(new Date());
		app.setDeployedDate(new Date());
		app.setTomcatDomain(domain);
		app.setVersion("1.233"); // get by war fileF
		// provisioning before save
		appService.save(app);
		return json;
	}

	@RequestMapping("/undeploy")
	public @ResponseBody SimpleJsonResponse undeploy(SimpleJsonResponse json,
			int Id) {
		TomcatApplication app = appService.getApplication(Id);

		if (app == null) {
			json.setSuccess(false);
			json.setMsg("Application does not exist.");

		} else {
			appService.delete(app);

		}
		return json;
	}

	@RequestMapping("/start")
	public @ResponseBody SimpleJsonResponse start(SimpleJsonResponse json,
			int id) {
		TomcatApplication app = appService.getApplication(id);
		if (app == null) {
			json.setSuccess(false);
			json.setMsg("Application does not exist.");
			return json;
		}

		if (app.getState() == State.APP_STATE_STARTED) {
			json.setSuccess(false);
			json.setMsg("Application has been already started.");
			return json;
		}

		if (appService.start(app)) {
			json.setSuccess(true);
			json.setData(State.APP_STATE_STARTED);
		}
		return json;
	}

	@RequestMapping("/stop")
	public @ResponseBody SimpleJsonResponse stop(SimpleJsonResponse json, int id) {
		TomcatApplication app = appService.getApplication(id);
		if (app == null) {
			json.setSuccess(false);
			json.setMsg("Application does not exist.");
			return json;
		}

		if (app.getState() == State.APP_STATE_STOPPED) {
			json.setSuccess(false);
			json.setMsg("Application has been already stopped.");
			return json;
		}

		if (appService.stop(app)) {
			json.setSuccess(true);
			json.setData(State.APP_STATE_STOPPED);
		}
		return json;
	}

	@RequestMapping("/restart")
	public @ResponseBody SimpleJsonResponse restart(SimpleJsonResponse json,
			int id) {
		TomcatApplication app = appService.getApplication(id);
		if (app == null) {
			json.setSuccess(false);
			json.setMsg("Application does not exist.");
			return json;
		}

		if (app.getState() == State.APP_STATE_STOPPED) {
			json.setSuccess(false);
			json.setMsg("Application has been already stopped.");
			return json;
		} else {
			if (appService.stop(app)) {
				if (appService.start(app)) {
					json.setSuccess(true);
					json.setData(State.APP_STATE_STARTED);
				}
			}
		}

		return json;
	}

	@RequestMapping("/list")
	public @ResponseBody SimpleJsonResponse getAppListByDomain(
			SimpleJsonResponse json, int domainId) {
		TomcatDomain domain = domainService.getDomain(domainId);
		if (domain == null) {
			json.setSuccess(false);
			json.setMsg("Domain does not exist");
		}
		// List<TomcatInstance> tomcats = domain.getTomcats();
		// if (tomcats.size() > 0) {
		// json.setSuccess(true);
		// // json.setData(tomcats.get(0).getApplications());
		// }
		return json;
	}
}

@@ -387,26 +387,145 @@
 ation.Re
-sponseBody
+questParam;%0D%0Aimport org.springframework.web.bind.annotation.ResponseBody;%0D%0Aimport org.springframework.web.multipart.MultipartFile
 ;%0D%0A%0D%0Aimp
@@ -1411,42 +1411,208 @@
 %0A%09%09%09
-TomcatApplication app, int domainI
+@RequestParam String contextPathTextField,%0D%0A%09%09%09@RequestParam String installationRemotePathTextField,%0D%0A%09%09%09@RequestParam Integer domainIdHiddenField,%0D%0A%09%09%09@RequestParam MultipartFile warLocalPathFileFiel
 d) %7B
@@ -1673,128 +1673,135 @@
 inId
-);%0D%0A%09%09if (domain
+HiddenField);%0D%0A%09%09TomcatApplication app
  =
-=
  n
-ull) %7B%0D%0A%09%09%09json.setMsg(%22Domain does not exist.%22);%0D%0A%09%09%09json.setSuccess(false);%0D%0A%09%09%09return json;%0D%0A%09%09%7D
+ew TomcatApplication(contextPathTextField,%0D%0A%09%09%09%09installationRemotePathTextField, %22%22);
 %0D%0A%09%09
@@ -1944,24 +1944,76 @@
 n(domain);%0D%0A
+%09%09// TODO: idkjwon provisioning deploy application%0D%0A
 %09%09app.setVer
@@ -2048,16 +2048,18 @@
  fileF%0D%0A
+%0D%0A
 %09%09// pro

deploy app