package org.minimalj.frontend.page;

import java.util.logging.Level;
import java.util.logging.Logger;

import org.minimalj.application.MjApplication;
import org.minimalj.frontend.toolkit.ClientToolkit;
import org.minimalj.frontend.toolkit.IAction;
import org.minimalj.util.StringUtils;
import org.minimalj.util.resources.Resources;


public class PageLink implements IAction {
	private static final Logger logger = Logger.getLogger(PageLink.class.getName());

	private final String name;
	private final String link;
	private final boolean enabled;

	PageLink() {
		this(null);
	}
	
	PageLink(String name) {
		this(name, null);
	}

	public PageLink(String name, String link) {
		this(name, link, true);
	}

	public PageLink(String name, String link, boolean enabled) {
		this.name = name;
		this.link = link;
		this.enabled = enabled;
	}
	
	public PageLink(Class<? extends Page> pageClass, String... args) {
		this(Resources.getString(pageClass), link(pageClass, args));
	}
	
	@Override
	public String getName() {
		return name;
	}
	
	public String getLink() {
		return link;
	}
	
	@Override
	public boolean isEnabled() {
		return enabled;
	}
	
	@Override
	public void action() {
		ClientToolkit.getToolkit().show(link);
	}
	
	public static Page createPage(String pageLink) {
		if (StringUtils.isEmpty(pageLink)) {
			return MjApplication.getApplication().createDefaultPage();
		}
		try {
			int pos = pageLink.indexOf('/');
			String className = pos > 0 ? pageLink.substring(0, pos) : pageLink;
			String fullClassName;
			if (EmptyPage.class.getSimpleName().equals(className)) {
				fullClassName = EmptyPage.class.getName();
			} else {
				fullClassName = MjApplication.getCompletePackageName("frontend.page") + "." + className;
			}
			Class<?> clazz = Class.forName(fullClassName);
			if (pos > 0) {
				String[] fragmentParts = pageLink.substring(pos+1).split("/");
				if (fragmentParts.length > 1) {
					Class<?>[] argumentClasses = new Class[1];
					argumentClasses[0] = new String[0].getClass();
					return (Page) clazz.getConstructor(argumentClasses).newInstance(new Object[]{fragmentParts});
				} else {
					Class<?>[] argumentClasses = new Class[1];
					argumentClasses[0] = String.class;
					return (Page) clazz.getConstructor(argumentClasses).newInstance(fragmentParts[0]);
				}
			} else {
				Class<?>[] argumentClasses = new Class[0];
				return (Page) clazz.getConstructor(argumentClasses).newInstance();
			}
		} catch (Exception x) {
			x.printStackTrace();
			logger.log(Level.SEVERE, "UriFragment Auflösung fehlgeschlagen: " + pageLink, x);
			// TODO It would be nice to have here an error page instead of an empty page
			return new EmptyPage();
		}
	}

	public static String link(Class<? extends Page> pageClass, long id) {
		return link(pageClass, String.valueOf(id));
	}
	
	public static String link(Class<? extends Page> pageClass, String... args) {
		StringBuilder s = new StringBuilder();
		s.append(pageClass.getSimpleName());
		for (int i = 0; i<args.length; i++) {
			s.append("/"); s.append(args[i]);
		}
		return s.toString();
	}

	@Override
	public String getDescription() {
		return null;
	}

	@Override
	public void setChangeListener(ActionChangeListener changeListener) {
		// n/a
	}
	
//	public static String link(String... args) {
//		if (args.length == 0) {
//			return "";
//		} else if (args.length == 1) {
//			return args[0];
//		} else {
//			StringBuilder s = new StringBuilder();
//			s.append(args[0]);
//			for (int i = 1; i<args.length; i++) {
//				s.append("/"); s.append(args[i]);
//			}
//			return s.toString();
//		}
//	}
	
}

@@ -245,24 +245,66 @@
 t.IAction;%0D%0A
+import org.minimalj.util.ExceptionUtils;%0D%0A
 import org.m
@@ -2595,30 +2595,114 @@
 %0A%09%09%09
-x.printStackTrace();
+String message = %22Page could not be created: %22 + pageLink;%0D%0A%09%09%09if (x.getCause() instanceof Exception) %7B
 %0D%0A
+%09
 %09%09%09l
@@ -2729,310 +2729,275 @@
 RE, 
-%22UriFragment Aufl%C3%B6sung fehlgeschlagen: %22 + pageLink, x);%0D%0A%09%09%09// TODO It would be nice to have here an error page instead of an empty page%0D%0A%09%09%09return new EmptyPage();%0D%0A%09%09%7D%0D%0A%09%7D%0D%0A%0D%0A%09public static String link(Class%3C? extends Page%3E pageClass, long id) %7B%0D%0A%09%09return link(pageClass, String.valueOf(id));%0D%0A%09%7D%0D%0A%09
+message);%0D%0A%09%09%09%09ExceptionUtils.logReducedStackTrace(logger, (Exception) x.getCause());%0D%0A%09%09%09%7D else %7B%0D%0A%09%09%09%09logger.log(Level.SEVERE, message, x);%0D%0A%09%09%09%7D%0D%0A%09%09%09// TODO It would be nice to have here an error page instead of an empty page%0D%0A%09%09%09return new EmptyPage();%0D%0A%09%09%7D%0D%0A%09%7D%0D%0A
 %0D%0A%09p

Use of logReducedStackTrace to better log if a page cannot be created