package com.mason.server.controller.json;

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

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.google.appengine.api.datastore.KeyFactory;
import com.mason.server.dao.jdo.LogDAO;
import com.mason.server.dao.jdo.UmlElementContainerDAO;
import com.mason.server.dao.jdo.UmlElementDAO;
import com.mason.server.dao.jdo.UmlRelationshipDAO;
import com.mason.server.model.Log;
import com.mason.server.model.UmlElement;
import com.mason.server.model.UmlElementContainer;

@Controller
@RequestMapping("/json/view/{slug}/")
public class UmlContainerControllerJson {
	private static UmlElementDAO elementDAO = new UmlElementDAO();
	private static UmlElementContainerDAO elementContainerDAO = new UmlElementContainerDAO();
	private static UmlRelationshipDAO relationshipDAO = new UmlRelationshipDAO();
	private static LogDAO logDAO = new LogDAO();

	@RequestMapping(value = "elementContainers", method = RequestMethod.GET)
	public @ResponseBody List<Object> getViewContainers(@PathVariable String slug) {
		ArrayList <Object> result = new ArrayList<Object>();
		result.addAll(elementContainerDAO.getAllByViewId(KeyFactory.stringToKey(slug).getId()));
		result.addAll(relationshipDAO.getAllByViewId(KeyFactory.stringToKey(slug).getId()));
		return result;
	}

	@RequestMapping(value = "elementContainer", method = RequestMethod.POST)
	public @ResponseBody UmlElementContainer updateElementContainer(@PathVariable String slug, UmlElementContainer ec) {

		ec.setViewIdGenerated(slug);
		
		UmlElementContainer result = new UmlElementContainer();
		if (ec.getId() == null ) {
			result = elementContainerDAO.add(ec);
			logDAO.add(new Log(result.getId(), UmlElementContainer.class.getSimpleName(),
					Log.Action.CREATE));
		} else {
			result = elementContainerDAO.update(ec);
			logDAO.add(new Log(result.getId(), UmlElementContainer.class.getSimpleName(),
					Log.Action.UPDATE));
		}
		
		return result;
	}

	@RequestMapping(value = "elementContainer/{elementSlug}", method = RequestMethod.DELETE)
	public void deleteElementContainer(@PathVariable String slug, @PathVariable String elementSlug) {

		UmlElementContainer container = elementContainerDAO.get(KeyFactory.stringToKey(elementSlug).getId());

		Long elementId = container.getElementId();
		
		elementContainerDAO.deleteById(KeyFactory.stringToKey(elementSlug).getId());
		logDAO.add(new Log(KeyFactory.stringToKey(elementSlug).getId(), UmlElementContainer.class.getSimpleName(),
				Log.Action.DELETE));
		
		if (elementContainerDAO.getAllByElementId(elementId).size() == 0 ) {
			elementDAO.deleteById(elementId);
			logDAO.add(new Log(elementId, UmlElement.class.getSimpleName(),
					Log.Action.DELETE));
		}
		
		relationshipDAO.deleteByUmlElementContainerId(KeyFactory.stringToKey(elementSlug).getId());
	}

}
