package org.orienteer.core.component.widget.browse;

import com.google.inject.Inject;
import com.orientechnologies.orient.core.metadata.schema.OClass;
import com.orientechnologies.orient.core.record.impl.ODocument;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.model.StringResourceModel;
import org.orienteer.core.behavior.UpdateOnActionPerformedEventBehavior;
import org.orienteer.core.component.FAIcon;
import org.orienteer.core.component.FAIconType;
import org.orienteer.core.component.command.*;
import org.orienteer.core.component.property.DisplayMode;
import org.orienteer.core.component.table.OrienteerDataTable;
import org.orienteer.core.component.table.component.GenericTablePanel;
import org.orienteer.core.service.IOClassIntrospector;
import org.orienteer.core.widget.AbstractWidget;
import org.orienteer.core.widget.Widget;
import ru.ydn.wicket.wicketorientdb.model.OClassNamingModel;
import ru.ydn.wicket.wicketorientdb.model.OQueryDataProvider;

/**
 * Widget to list all documents of a class
 */
@Widget(id="list-all", domain="browse", tab="list", autoEnable=true)
public class ListAllODocumentsWidget extends AbstractWidget<OClass> {
	
	@Inject
	private IOClassIntrospector oClassIntrospector;

	public ListAllODocumentsWidget(String id, IModel<OClass> model,
			IModel<ODocument> widgetDocumentModel) {
		super(id, model, widgetDocumentModel);
		IModel<DisplayMode> modeModel = DisplayMode.VIEW.asModel();
		String className = getModelObject().getName();
		String sql = "select from " + className;
		OQueryDataProvider<ODocument> provider = new OQueryDataProvider<>(sql);
		oClassIntrospector.defineDefaultSorting(provider, getModelObject());
		GenericTablePanel<ODocument> tablePanel =
				new GenericTablePanel<>("tablePanel", oClassIntrospector.getColumnsFor(getModelObject(), true, modeModel), provider, 20);

		final OrienteerDataTable<ODocument, String> table = tablePanel.getDataTable();
		table.getCommandsToolbar().setDefaultModel(getModel());
		table.addCommand(new CreateODocumentCommand(table, getModel()));
		table.addCommand(new EditODocumentsCommand(table, modeModel, getModel()));
		table.addCommand(new SaveODocumentsCommand(table, modeModel));
		table.addCommand(new CopyODocumentCommand(table, getModel()));
		table.addCommand(new DeleteODocumentCommand(table, getModel()));
		table.addCommand(new ExportCommand<>(table, new PropertyModel<String>(model, "name")));

		add(tablePanel);
		add(UpdateOnActionPerformedEventBehavior.INSTANCE_ALL_CONTINUE);
	}

	@Override
	protected FAIcon newIcon(String id) {
		return new FAIcon(id, FAIconType.list_alt);
	}

	@Override
	protected IModel<String> getDefaultTitleModel() {
		return new StringResourceModel("class.browse.title", new OClassNamingModel(getModel()));
	}
	
	@Override
	protected String getWidgetStyleClass() {
		return "strict";
	}

}

@@ -341,24 +341,90 @@
 ourceModel;%0A
+import org.apache.wicket.request.mapper.parameter.PageParameters;%0A
 import org.o
@@ -844,35 +844,163 @@
 ore.
-service.IOClassIntrospector
+component.widget.document.ODocumentPropertiesWidget;%0Aimport org.orienteer.core.service.IOClassIntrospector;%0Aimport org.orienteer.core.web.schema.OClassPage
 ;%0Aim
@@ -2739,16 +2739,310 @@
 TINUE);%0A
+%09%09%0A%09%09addCommand(new BookmarkablePageLinkCommand%3COClass%3E(newCommandId(), %22command.gotoClass%22, OClassPage.class) %7B%0A%09%09%09@Override%0A%09%09%09public PageParameters getPageParameters() %7B%0A%09%09%09%09return OClassPage.preparePageParameters(ListAllODocumentsWidget.this.getModelObject(), DisplayMode.VIEW);%0A%09%09%09%7D%0A%09%09%7D);%0A
 %09%7D%0A%0A%09@Ov

Add action to navigate to class from browse page