package model;

import javax.swing.tree.TreePath;
import java.util.TreeMap;

/**
 * Created by joseph on 13/05/16.
 */
public class Route
{
	private String description = "";

	private String controller = "";
	private String view = "";
	private String viewTemplate = "";

	private TreeMap<String, Route> subRoutes;
	private TreeMap<String, HttpMethod> httpMethods;


	public Route()
	{
		subRoutes = new TreeMap<String, Route>();
		httpMethods = new TreeMap<String, HttpMethod>();
	}

	public String getDescription()
	{
		return description;
	}

	public void setDescription(String description)
	{
		this.description = description;
	}

	public String getController()
	{
		return controller;
	}

	public void setController(String controller)
	{
		this.controller = controller;
	}

	public String getView()
	{
		return view;
	}

	public void setView(String view)
	{
		this.view = view;
	}

	public String getViewTemplate()
	{
		return viewTemplate;
	}

	public void setViewTemplate(String viewTemplate)
	{
		this.viewTemplate = viewTemplate;
	}

	public TreeMap<String, Route> getSubRoutes()
	{
		return subRoutes;
	}

	private String extractRouteFirstPart(String name)
	{
		if(name.startsWith("/"))
		{
			name = name.substring(1);
		}

		if(name.indexOf('/') != -1)
		{
			name = name.substring(0, name.indexOf('/'));
		}
		return name;
	}

	private String extractRouteLastPart(String name)
	{
		String lastPart = "";

		if(name.startsWith("/"))
		{
			name = name.substring(1);
		}

		if(name.indexOf('/') != -1)
		{
			lastPart = name.substring(name.indexOf('/') + 1);
		}
		return lastPart;
	}

	public boolean addRoute(String name)
	{
		String firstPart = extractRouteFirstPart(name);
		String lastPart = extractRouteLastPart(name);

		if(firstPart.isEmpty())
		{
			return false;
		}

		if(subRoutes.containsKey(firstPart))
		{
			if(lastPart.isEmpty())
			{
				return false;
			}
			return subRoutes.get(firstPart).addRoute(lastPart);
		}
		else
		{
			Route newRoute = new Route();
			subRoutes.put(firstPart, newRoute);

			if(lastPart.isEmpty())
			{
				return true;
			}

			return newRoute.addRoute(lastPart);
		}
	}

	public boolean deleteRoute(String name)
	{
		String firstPart = extractRouteFirstPart(name);
		String lastPart = extractRouteLastPart(name);

		if(subRoutes.containsKey(firstPart))
		{
			if(lastPart.isEmpty())
			{
				Route r = subRoutes.remove(firstPart);
				if(r != null)
				{
					return true;
				}
				return false;
			}
			return subRoutes.get(firstPart).deleteRoute(lastPart);
		}
		return false;
	}
}
@@ -13,42 +13,8 @@
 l;%0A%0A
-import javax.swing.tree.TreePath;%0A
 impo
@@ -2352,19 +2352,23 @@
 t);%0A%09%09%09%09
-if(
+return 
 r != nul
@@ -2372,55 +2372,476 @@
 null
-)%0A%09%09%09%09%7B%0A%09%09%09%09%09return true;%0A%09%09%09%09%7D%0A%09%09%09%09return fals
+;%0A%09%09%09%7D%0A%09%09%09return subRoutes.get(firstPart).deleteRoute(lastPart);%0A%09%09%7D%0A%09%09return false;%0A%09%7D%0A%0A%09public boolean renameRoute(String oldName, String newName)%0A%09%7B%0A%09%09String firstPart = extractRouteFirstPart(newName);%0A%09%09String lastPart = extractRouteLastPart(newName);%0A%0A%09%09if(subRoutes.containsKey(firstPart))%0A%09%09%7B%0A%09%09%09if(lastPart.isEmpty())%0A%09%09%09%7B%0A%09%09%09%09Route r = subRoutes.remove(firstPart);%0A%09%09%09%09if(r == null)%0A%09%09%09%09%7B%0A%09%09%09%09%09return false;%0A%09%09%09%09%7D%0A%09%09%09%09subRoutes.put(newName, r);%0A%09%09%09%09return tru
 e;%0A%09

rename route (model only)