/*
 * Copyright (c) 2016 IBM Corporation and others.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * You may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.brunel.build.info;

import org.brunel.build.d3.D3Util;
import org.brunel.build.d3.element.ElementDetails;
import org.brunel.data.Dataset;
import org.brunel.data.Field;
import org.brunel.maps.GeoMapping;
import org.brunel.model.VisSingle;
import org.brunel.model.VisTypes.Diagram;
import org.brunel.model.VisTypes.Element;

import java.util.ArrayList;
import java.util.List;

/**
 * Defines how to display an element
 */
public class ElementStructure {
	public final ChartStructure chart;
	public final int index;
	public final VisSingle vis;
	public final Dataset original;
	public final Dataset data;
	public final GeoMapping geo;

	public ElementDetails details;
	public List<Dependency> dependencies;

	public ElementStructure(ChartStructure chartStructure, int elementIndex, VisSingle vis, Dataset data, GeoMapping geo) {
		this.chart = chartStructure;
		this.index = elementIndex;
		this.vis = vis;
		this.data = data;
		this.geo = geo;
		this.original = vis.getDataset();
		this.dependencies = new ArrayList<>();
	}

	// Find a dependent structure on us -- edges for our nodes
	public ElementStructure findDependentEdges() {
		for (Dependency dependency : dependencies) {
			// Needs two keys
			if (dependency.base == this && dependency.dependent.vis.fKeys.size() > 1) return dependency.dependent;
		}
		return null;
	}

	public int getBaseDatasetIndex() {
		return chart.getBaseDatasetIndex(original);
	}

	public String elementID() {
		return "" + (index + 1);
	}

	public boolean hasHierarchicalData() {
		return chart.diagram != null && chart.diagram.isHierarchical;
	}

	public boolean isClustered() {
		// We are clustered when we are a coordinate chart with 2+ X dimensions
		return chart.diagram == null && vis.fX.size() > 1;
	}


	// returns true of we depend on another element
	public boolean isDependent() {
		return getDependencyBase() != null;
	}

	// rReturns the element we depend on
	public ElementStructure getDependencyBase() {
		for (Dependency dependency : dependencies) {
			if (dependency.dependent == this) return dependency.base;
		}
		return null;
	}

	public boolean isNetworkEdge() {
		return chart.diagram == Diagram.network && isDependentEdge();
	}

	public boolean isDependentEdge() {
		return vis.tElement == Element.edge && isDependent();
	}

	public String[] makeReferences(Field[] keys) {
		String idToPointName = "elements[" + getDependencyBase().index + "].internal()._idToPoint(";
		String[] references = new String[keys.length];
		for (int i = 0; i < references.length; i++)
			references[i] = idToPointName + D3Util.writeCall(keys[i]) + ")";
		return references;
	}
}

@@ -863,50 +863,8 @@
 le;%0A
-import org.brunel.model.VisTypes.Diagram;%0A
 impo
@@ -2668,110 +2668,8 @@
 %09%7D%0A%0A
-%09public boolean isNetworkEdge() %7B%0A%09%09return chart.diagram == Diagram.network && isDependentEdge();%0A%09%7D%0A%0A
 %09pub

Remove unneeded code