package org.orienteer.core.web;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.wicket.model.IModel;
import org.apache.wicket.model.StringResourceModel;
import org.apache.wicket.request.http.flow.AbortWithHttpErrorCodeException;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.orienteer.core.MountPath;
import org.orienteer.core.component.property.DisplayMode;
import org.orienteer.core.module.OWidgetsModule;
import org.orienteer.core.web.schema.OClassPage;
import org.orienteer.core.widget.DashboardPanel;
import org.orienteer.core.widget.IWidgetFilter;

import ru.ydn.wicket.wicketorientdb.model.OClassModel;
import ru.ydn.wicket.wicketorientdb.model.OClassNamingModel;
import ru.ydn.wicket.wicketorientdb.security.ISecuredComponent;
import ru.ydn.wicket.wicketorientdb.security.OSecurityHelper;
import ru.ydn.wicket.wicketorientdb.security.OrientPermission;
import ru.ydn.wicket.wicketorientdb.security.RequiredOrientResource;

import com.orientechnologies.orient.core.metadata.schema.OClass;
import com.orientechnologies.orient.core.record.impl.ODocument;

/**
 * Page to browse all documents for a specific {@link OClass}
 */
@MountPath("/browse/${className}")
public class BrowseOClassPage extends AbstractWidgetPage<OClass> implements ISecuredComponent {

	public BrowseOClassPage(String className)
	{
		this(new OClassModel(className));
	}
	
	public BrowseOClassPage(IModel<OClass> model)
	{
		super(model);
	}

	public BrowseOClassPage(PageParameters parameters)
	{
		super(parameters);
	}

	@Override
	protected IModel<OClass> resolveByPageParameters(
			PageParameters pageParameters) {
		return new OClassModel(pageParameters.get("className").toOptionalString());
	}
	
	@Override
	public void initialize() {
		if(getModelObject()==null) throw new AbortWithHttpErrorCodeException(HttpServletResponse.SC_NOT_FOUND);
		super.initialize();
	}
	
	@Override
	protected DashboardPanel<OClass> newDashboard(String id, String domain,
			String tab, IModel<OClass> model, IWidgetFilter<OClass> filter) {
		
		return new DashboardPanel<OClass>(id, domain, tab, model, filter) {
			@Override
			protected ODocument lookupDashboardDocument(String domain,
					String tab, IModel<OClass> model) {
				return dashboardManager.getExistingDashboard(domain, tab, model, model.getObject());
			}
			
			@Override
			public ODocument storeDashboard() {
				ODocument doc = super.storeDashboard();
				doc.field(OWidgetsModule.OPROPERTY_CLASS, getModelObject().getName());
				doc.save();
				return doc;
			}
		};
	}
	
	@Override
	public String getDomain() {
		return "browse";
	}
	
	@Override
	public IModel<String> getTitleModel() {
		return new StringResourceModel("class.browse.title", new OClassNamingModel(getModel()));
	}

	@Override
	public RequiredOrientResource[] getRequiredResources() {
		return OSecurityHelper.requireOClass(getModelObject(), OrientPermission.READ);
	}
	
	public static PageParameters preparePageParameters(OClass oClass, DisplayMode mode) {
		return OClassPage.preparePageParameters(oClass, mode);
	}

}

@@ -356,16 +356,62 @@
 meters;%0A
+import org.apache.wicket.util.string.Strings;%0A
 import o
@@ -691,16 +691,62 @@
 tFilter;
+%0Aimport org.orienteer.core.widget.IWidgetType;
 %0A%0Aimport
@@ -2009,16 +2009,355 @@
 FOUND);%0A
+%09%09setWidgetsFilter(new IWidgetFilter%3COClass%3E() %7B%0A%09%09%09%0A%09%09%09@Override%0A%09%09%09public boolean apply(IWidgetType%3COClass%3E input) %7B%0A%09%09%09%09if(Strings.isEmpty(input.getSelector())) return true;%0A%09%09%09%09else %7B%0A%09%09%09%09%09OClass oClass = BrowseOClassPage.this.getModelObject();%0A%09%09%09%09%09return oClass!=null?oClass.isSubClassOf(input.getSelector()):false;%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%09%09%7D);%0A
 %09%09super.

Add ability for widgets to have selector on BrowseOClasPage