package com.mason.server.controller.json;

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.UmlRelationshipDAO;
import com.mason.server.dao.jdo.ViewDAO;
import com.mason.server.model.Log;
import com.mason.server.model.UmlModelData;
import com.mason.server.model.View;

@Controller
@RequestMapping("/json/")
public class ViewControllerJson {

	private static ViewDAO viewDAO = new ViewDAO();
	private static LogDAO logDAO = new LogDAO();

	private static UmlRelationshipDAO relationshipDAO = new UmlRelationshipDAO();
	private static UmlElementContainerDAO elementContainerDAO = new UmlElementContainerDAO();

	@ResponseBody
	@RequestMapping(value = "model/{slug}/view", method = RequestMethod.POST)
	public View newView(@PathVariable String slug, View v) {

		View result = new View();
		
		v.setModelIdGenerated(slug);

		v = this.processView(v);
		
		if (v.getId() == null ) {
			result = viewDAO.add(v);
			logDAO.add(new Log(result.getId(), View.class.getSimpleName(),
					Log.Action.DELETE));
		} else {
			result = viewDAO.update(v);
			logDAO.add(new Log(result.getId(), View.class.getSimpleName(),
					Log.Action.DELETE));
		}

		return result;
	}

	private View processView(View view) {
		
		view.setDescription(view.getDescription().trim());

		int i = 1;

		String description = view.getDescription().toLowerCase()
				.replace(" ", "_");
		String slug = description;

		View vw = viewDAO.getBySlug(slug, view.getModelId());

		while (vw != null && vw.getId() != view.getId()) {
			slug = description + i;
			i++;
			vw = viewDAO.getBySlug(slug, view.getModelId());
		}
		view.setSlug(slug);
		return view;
	}

	@RequestMapping(value = "view/{slug}/data/all", method = RequestMethod.GET)
	public @ResponseBody UmlModelData getAllData(@PathVariable String slug) {
		
		UmlModelData data = new UmlModelData();
		
		View v = viewDAO.get(KeyFactory.stringToKey(slug).getId());
		
		if (v != null ) {		
			data.elementContainers = elementContainerDAO.getAllByViewId(v.getId());
			data.relationships = relationshipDAO.getAllByViewId(v.getId());
		}

		return data;
	}

	@RequestMapping(value = "view/{viewSlug}", method = RequestMethod.DELETE)
	public @ResponseBody int delete(@PathVariable String viewSlug) {

		elementContainerDAO.deleteAllByViewId(KeyFactory.stringToKey(viewSlug).getId());
		relationshipDAO.deleteAllByViewId(KeyFactory.stringToKey(viewSlug).getId());
		
		boolean result = viewDAO.deleteById(KeyFactory.stringToKey(viewSlug).getId());
		logDAO.add(new Log(KeyFactory.stringToKey(viewSlug).getId(), View.class.getSimpleName(),
				Log.Action.DELETE));
		
		if (result ) {
			return 1;
		} else {
			return 0;
		}
		
	}

}
