package com.athena.meerkat.controller.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
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.module.ClientManager;
import com.athena.meerkat.controller.web.common.model.TreeJsonResponse;
import com.athena.meerkat.controller.web.common.model.TreeNode;
import com.athena.meerkat.controller.web.entities.Server;
import com.athena.meerkat.controller.web.entities.TomcatDomain;
import com.athena.meerkat.controller.web.entities.TomcatInstance;
import com.athena.meerkat.controller.web.resources.services.ServerService;
import com.athena.meerkat.controller.web.tomcat.services.TomcatDomainService;
import com.athena.meerkat.controller.web.tomcat.services.TomcatInstanceService;

@Controller
@RequestMapping("/menu")
public class MenuController {

	private static final Logger logger = LoggerFactory.getLogger(MenuController.class);
	@Autowired
	private TomcatDomainService domainService;
	@Autowired
	private TomcatInstanceService tomcatService;
	@Autowired
	private ServerService serverService;

	@RequestMapping(value = "/getNodes", method = RequestMethod.GET)
	@ResponseBody
	public List<HashMap<String, Object>> getNodes(String node) {
		boolean isLeaf = false;
		String prefixMeuId = "";
		List<HashMap<String, Object>> nodes = new ArrayList<>();

		if ((node.indexOf("mon_tomcats") >= 0 || node.indexOf("tomcatMng_domain") >= 0) && node.indexOf("_domain_") > 0) {
			Integer objId = Integer.parseInt(node.substring(node.indexOf("_domain_") + "_domain_".length()));
			List<TomcatInstance> tomcats = new ArrayList<>();
			isLeaf = true;
			prefixMeuId = node + "_tomcat_";
			tomcats = tomcatService.getTomcatListByDomainId(objId);
			for (TomcatInstance t : tomcats) {
				HashMap<String, Object> n = new HashMap<String, Object>();
				n.put("text", t.getName());
				n.put("id", prefixMeuId + t.getId());
				n.put("leaf", isLeaf);
				nodes.add(n);
			}
		} else if (node.equals("mon_servers")) {
			List<Server> servers = new ArrayList<>();
			isLeaf = true;
			prefixMeuId = node + "_server_";
			servers = serverService.getList();
			for (Server s : servers) {
				HashMap<String, Object> n = new HashMap<String, Object>();
				n.put("text", s.getName());
				n.put("id", prefixMeuId + s.getId());
				n.put("leaf", isLeaf);
				nodes.add(n);
			}
		} else {

		}
		//		json.setChildren(nodes);
		//		return json;
		return nodes;
	}

}

@@ -1871,20 +1871,441 @@
 t%3C%3E();%0D%0A
-%0D%0A
 %09%09
+%0D%0A%09    if (node.equals(%22tomcatMng%22) %7C%7C node.equals(%22mon_tomcats%22)) %7B%0D%0A%09%09%09List%3CTomcatDomain%3E domains = domainService.getAll();%0D%0A%09%09%09isLeaf = false;%0D%0A%09%09%09prefixMeuId = node + %22_domain_%22;%0D%0A%09%09%09for (TomcatDomain d : domains) %7B%0D%0A%09%09%09%09HashMap%3CString, Object%3E n = new HashMap%3CString, Object%3E();%0D%0A%09%09%09%09n.put(%22text%22, d.getName());%0D%0A%09%09%09%09n.put(%22id%22, prefixMeuId + d.getId());%0D%0A%09%09%09%09n.put(%22leaf%22, isLeaf);%0D%0A%09%09%09%09nodes.add(n);%0D%0A%09%09%09%7D%0D%0A%09%09%7D else 
 if ((nod
@@ -3350,56 +3350,8 @@
 %0D%0A%09%09
-//%09%09json.setChildren(nodes);%0D%0A%09%09//%09%09return json;
 %0D%0A%09%09
@@ -3369,13 +3369,14 @@
 s;%0D%0A%09%7D%0D%0A
+%09
 %0D%0A%7D%0D%0A

remove static menu