package eu.dnetlib.x3m;

import java.io.IOException;
import java.io.InputStream;

import eu.dnetlib.msro.workflows.nodes.transform.ApplyX3Mapping;
import org.apache.commons.io.IOUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;
import org.junit.Test;
import org.springframework.core.io.ClassPathResource;

/**
 * Created by alessia on 13/03/17.
 */
public class ApplyX3MappingTest {

	private static final Log log = LogFactory.getLog(ApplyX3MappingTest.class);

	private static String basePathProfiles = "/eu/dnetlib/bootstrap/profiles/TransformationRuleDSResources/TransformationRuleDSResourceType/";


	//Parthenos mappings
	final String parthenosPolicyPath = "parthenos_policy1.5.xml";
	final String mappingAriadnePath = "ariadne_dataset_444_mapping.xml";
	final String mappingEhriPath = "ehri_328_mapping.xml";
	final String mappingCulturaItaliaPath = "culturaitalia_musei_312_mapping.xml";

	//Parthenos records
	final String ariadnePath = "/eu/dnetlib/x3m/new-10304736.xml";
	final String ehriRecord = "/eu/dnetlib/x3m/ehri_test.xml";
	final String ehriRecord2 = "/eu/dnetlib/x3m/EHRI_sample_record_328.xml";
	final String[] ariadneFiles =
			new String[] { "/eu/dnetlib/x3m/new-10304737.xml", "/eu/dnetlib/x3m/new-10304738.xml", "/eu/dnetlib/x3m/new-10304739.xml",
					"/eu/dnetlib/x3m/new-10304740.xml", "/eu/dnetlib/x3m/new-10304741.xml", "/eu/dnetlib/x3m/new-10304742.xml" };

	final String culturaItaliaPath = "/eu/dnetlib/x3m/culturaItalia_record.xml";


	@Test
	public void testAriadne() {
		doBasicTest(this.mappingAriadnePath, this.ariadnePath);
	}

	@Test
	public void testAriadneAll() throws IOException {
		String m = loadFromTransformationProfile(mappingAriadnePath);
		String g = getString(parthenosPolicyPath);

		ApplyX3Mapping x3m = new ApplyX3Mapping(new String[]{m}, g, false);
		for(String path : ariadneFiles){
			String res = x3m.apply(getString(path));
			log.debug(res);
		}
	}


	@Test
	public void testEhri() throws IOException {
		doBasicTest(this.mappingEhriPath, this.ehriRecord);
	}
	@Test
	public void testEhri2(){
		doBasicTest(this.mappingEhriPath, this.ehriRecord2);
	}

	@Test
	public void testCulturaItalia() throws IOException {
		doBasicTest(this.mappingCulturaItaliaPath, this.culturaItaliaPath);
	}

	@Test
	public void testMetashare(){
		doBasicTest("metashare_439_mapping.xml", "/eu/dnetlib/x3m/metashare.xml");
	}

	@Test
	public void testCendariAuthors() {
		doBasicTest("parthenos_policy1.5.xml", "cendari_authors_486.xml", "/eu/dnetlib/x3m/cendari_author_ex1.xml");
	}
	@Test
	public void testCendariAuthors2() {
		doBasicTest("parthenos_policy1.5.xml", "cendari_authors_486.xml", "/eu/dnetlib/x3m/cendari_author_ex2.xml");
	}

	@Test
	public void testCendariTesti1() {
		doBasicTest("parthenos_policy1.5.xml", "cendari_texts_487.xml", "/eu/dnetlib/x3m/cendari_testi1.xml");
	}

	@Test
	public void testCendariManoscritto(){
		doBasicTest("cendari_manuscripts_485.xml", "/eu/dnetlib/x3m/cendari_manoscritto.xml");
	}


	@Test
	public void testWp8_ok() {
		doBasicTest("parthenos_policy1.5.xml", "parthenos_wp8.xml", "/eu/dnetlib/x3m/wp8.xml");
	}

	@Test
	public void testTopLevel() {
		doBasicTest("parthenos_policy1.5.xml", "parthenos_toplevel_mapping.xml", "/eu/dnetlib/x3m/topLevel.xml");
	}


	public void doBasicTest(String mappingPath, String recordPath){
		doBasicTest(parthenosPolicyPath, mappingPath, recordPath);
	}
	public void doBasicTest(String policyPath, String mappingPath, String recordPath){
		String m = loadFromTransformationProfile(mappingPath);
		String g = loadFromTransformationProfile(policyPath);
		String r = getString(recordPath);
		ApplyX3Mapping x3m = new ApplyX3Mapping(new String[]{m}, g, true);
		String res = x3m.apply(r);
		System.out.println("res is \n"+res);
	}


	private String getString(final String classpath) {
		try {
			final ClassPathResource resource = new ClassPathResource(classpath);
			return IOUtils.toString(resource.getInputStream(), "UTF-8");
		}catch(IOException e){
			return null;
		}
	}


	private String loadFromTransformationProfile(String profilePath){
		log.info("Loading xslt from: " + basePathProfiles + profilePath);
		InputStream profile = getClass().getResourceAsStream(basePathProfiles + profilePath);
		SAXReader saxReader = new SAXReader();
		Document doc = null;
		try {
			doc = saxReader.read(profile);
		} catch (DocumentException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
		String s = doc.selectSingleNode("//SCRIPT/CODE").getText();
		//log.debug(profilePath+": "+s);
		return s;
	}
}