package org.verapdf.model.impl.pb.pd;

import org.apache.log4j.Logger;
import org.apache.pdfbox.cos.COSBase;
import org.apache.pdfbox.cos.COSDictionary;
import org.apache.pdfbox.cos.COSName;
import org.verapdf.model.baselayer.Object;
import org.verapdf.model.pdlayer.PDStructElem;
import org.verapdf.model.tools.TaggedPDFHelper;

import java.util.List;

/**
 * Current class is representation of node of structure tree root.
 * Implemented by Apache PDFBox.
 *
 * @author Evgeniy Muravitskiy
 */
public class PBoxPDStructElem extends PBoxPDObject implements PDStructElem {

	private static final Logger LOGGER = Logger.getLogger(PBoxPDStructElem.class);

	/** Type name for {@code PBoxPDStructElem} */
	public static final String STRUCTURE_ELEMENT_TYPE = "PDStructElem";

	/** Link name for {@code K} key */
	public static final String CHILDREN = "K";

	/**
	 * Default constructor
	 *
	 * @param structElemDictionary dictionary of structure element
	 */
	public PBoxPDStructElem(COSDictionary structElemDictionary) {
		super(structElemDictionary, STRUCTURE_ELEMENT_TYPE);
	}

	/**
	 * @return Type entry of current structure element
	 */
	public String getType() {
		COSBase value = ((COSDictionary) this.simplePDObject).getDictionaryObject(COSName.TYPE);
		if (value instanceof COSName) {
			return ((COSName) value).getName();
		} else {
			LOGGER.warn("In struct element type expected 'COSName' but got: "
					+ value.getClass().getSimpleName());
			return null;
		}
	}

	@Override
	public List<? extends Object> getLinkedObjects(String link) {
		if (CHILDREN.equals(link)) {
			return this.getChildren();
		}
		return super.getLinkedObjects(link);
	}

	private List<PDStructElem> getChildren() {
		return TaggedPDFHelper.getChildren(((COSDictionary) this.simplePDObject), LOGGER);
	}
}

@@ -227,16 +227,121 @@
 Object;%0A
+import org.verapdf.model.coslayer.CosUnicodeName;%0Aimport org.verapdf.model.impl.pb.cos.PBCosUnicodeName;%0A
 import o
@@ -428,16 +428,74 @@
 elper;%0A%0A
+import java.util.ArrayList;%0Aimport java.util.Collections;%0A
 import j
@@ -1007,16 +1007,102 @@
 N = %22K%22;
+%0A%09/** Link name for %7B@code S%7D key */%0A%09public static final String STRUCTURE_TYPE = %22S%22;
 %0A%0A%09/**%0A%09
@@ -1373,32 +1373,43 @@
 re element%0A%09 */%0A
+%09@Override%0A
 %09public String g
@@ -1809,37 +1809,43 @@
 %7B%0A%09%09
-if (CHILDREN.equals(link)) %7B%0A
+switch (link) %7B%0A%09%09%09case CHILDREN:%0A%09
 %09%09%09r
@@ -1872,18 +1872,40 @@
 en();%0A%09%09
-%7D%0A
+%09case STRUCTURE_TYPE:%0A%09%09
 %09%09return
@@ -1905,16 +1905,57 @@
 %09return 
+this.getStructureType();%0A%09%09%09default:%0A%09%09%09%09
 super.ge
@@ -1976,16 +1976,20 @@
 (link);%0A
+%09%09%7D%0A
 %09%7D%0A%0A%09pri
@@ -2115,11 +2115,398 @@
 GER);%0A%09%7D
+%0A%0A%09private List%3CCosUnicodeName%3E getStructureType() %7B%0A%09%09COSBase type = ((COSDictionary) this.simplePDObject).getDictionaryObject(COSName.S);%0A%09%09if (type instanceof COSName) %7B%0A%09%09%09ArrayList%3CCosUnicodeName%3E list = new ArrayList%3C%3E(MAX_NUMBER_OF_ELEMENTS);%0A%09%09%09list.add(new PBCosUnicodeName((COSName) type));%0A%09%09%09return Collections.unmodifiableList(list);%0A%09%09%7D%0A%09%09return Collections.emptyList();%0A%09%7D
 %0A%7D%0A

Add new link to StructureElement