package com.gentics.mesh.changelog.highlevel.change;

import static com.gentics.mesh.core.data.ContainerType.DRAFT;
import static com.gentics.mesh.core.data.ContainerType.PUBLISHED;
import static com.gentics.mesh.core.data.relationship.GraphRelationships.HAS_FIELD_CONTAINER;

import javax.inject.Inject;
import javax.inject.Singleton;

import com.gentics.mesh.changelog.highlevel.AbstractHighLevelChange;
import com.gentics.mesh.core.data.ContainerType;
import com.gentics.mesh.core.data.NodeGraphFieldContainer;
import com.gentics.mesh.core.data.container.impl.NodeGraphFieldContainerImpl;
import com.gentics.mesh.core.data.impl.GraphFieldContainerEdgeImpl;
import com.gentics.mesh.core.data.node.Node;
import com.gentics.mesh.graphdb.spi.Database;
import com.syncleus.ferma.FramedTransactionalGraph;
import com.syncleus.ferma.tx.Tx;

import io.vertx.core.logging.Logger;
import io.vertx.core.logging.LoggerFactory;

/**
 * Change which will get rid of the old {@link NodeGraphFieldContainer} webroot properties and instead add those props to the HAS_FIELD_CONTAINER edge.
 */
@Singleton
public class RestructureWebrootIndex extends AbstractHighLevelChange {

	private static final Logger log = LoggerFactory.getLogger(RestructureWebrootIndex.class);

	private final Database db;

	@Inject
	public RestructureWebrootIndex(Database db) {
		this.db = db;
	}

	@Override
	public String getUuid() {
		return "7E94C51E763C46D394C51E763C86D3F5";
	}

	@Override
	public String getName() {
		return "Restructure Webroot Index";
	}

	@Override
	public String getDescription() {
		return "Restructures the webroot index by iterating over all publish and draft edges.";
	}

	@Override
	public void apply() {
		FramedTransactionalGraph graph = Tx.getActive().getGraph();
		Iterable<? extends GraphFieldContainerEdgeImpl> edges = graph.getFramedEdgesExplicit("@class", HAS_FIELD_CONTAINER,
			GraphFieldContainerEdgeImpl.class);
		long count = 0;
		for (GraphFieldContainerEdgeImpl edge : edges) {
			ContainerType type = edge.getType();
			if (DRAFT.equals(type) || PUBLISHED.equals(type)) {
				String branchUuid = edge.getBranchUuid();
				NodeGraphFieldContainer container = edge.getNodeContainer();
				Node node = container.getParentNode();
				if (node != null) {
					node = node.getParentNode(branchUuid);
				}
				edge.setUrlFieldInfo(container.getUrlFieldValues());
				String segment = container.getSegmentFieldValue();
				if (segment != null) {
					String newInfo = GraphFieldContainerEdgeImpl.composeSegmentInfo(node, segment);
					edge.setSegmentInfo(newInfo);
				} else {
					edge.setSegmentInfo(null);
				}
				if (count % 100 == 0) {
					log.info("Updating edge {" + count + "}");
				}
				count++;
			}
		}
		log.info("Done updating all edges. Total: {" + count + "}");

		Iterable<? extends NodeGraphFieldContainerImpl> containers = graph.getFramedVertices("@class",
			NodeGraphFieldContainerImpl.class.getSimpleName(), NodeGraphFieldContainerImpl.class);
		for (NodeGraphFieldContainer container : containers) {
			container.getElement().removeProperty("publishedWebrootUrlInfo");
			container.getElement().removeProperty("webrootUrlInfo");
			container.getElement().removeProperty("publishedWebrootPathInfo");
			container.getElement().removeProperty("webrootPathInfo");
		}

	}

	@Override
	public void applyNoTx() {
		db.removeVertexIndex("webrootPathInfoIndex", NodeGraphFieldContainerImpl.class);
		db.removeVertexIndex("publishedWebrootPathInfoIndex", NodeGraphFieldContainerImpl.class);
		db.removeVertexIndex("webrootUrlInfoIndex", NodeGraphFieldContainerImpl.class);
		db.removeVertexIndex("publishedWebrootInfoIndex", NodeGraphFieldContainerImpl.class);
	}

}

@@ -1692,16 +1692,61 @@
 ply() %7B%0A
+%09%09log.info(%22Applying change: %22 + getName());%0A
 %09%09Framed
@@ -2475,32 +2475,61 @@
 (segment != null
+ && !segment.trim().isEmpty()
 ) %7B%0A%09%09%09%09%09String 
@@ -2787,16 +2787,151 @@
 +;%0A%09%09%09%7D%0A
+%0A%09%09%09String segment = edge.getSegmentInfo();%0A%09%09%09if (segment == null %7C%7C segment.trim().isEmpty()) %7B%0A%09%09%09%09edge.setSegmentInfo(null);%0A%09%09%09%7D%0A%0A
 %09%09%7D%0A%09%09lo

Fix regression in changelog entry