
package eu.dnetlib.dhp.ircdl_extention;

import static eu.dnetlib.dhp.common.SparkSessionSupport.runWithSparkSession;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import org.apache.commons.io.IOUtils;
import org.apache.spark.SparkConf;
import org.apache.spark.api.java.function.FilterFunction;
import org.apache.spark.api.java.function.FlatMapFunction;
import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Encoders;
import org.apache.spark.sql.SaveMode;
import org.apache.spark.sql.SparkSession;

import eu.dnetlib.dhp.application.ArgumentApplicationParser;
import eu.dnetlib.dhp.ircdl_extention.model.Author;
import eu.dnetlib.dhp.ircdl_extention.model.KeyValue;
import eu.dnetlib.dhp.ircdl_extention.model.Result;
import eu.dnetlib.dhp.schema.common.ModelConstants;

public class PrepareResultSpark {

	public static void main(String[] args) throws Exception {

		String jsonConfiguration = IOUtils
			.toString(
				PrepareResultSpark.class
					.getResourceAsStream(
						"/eu/dnetlib/dhp/ircdl_extention/prepare_result_parameters.json"));

		final ArgumentApplicationParser parser = new ArgumentApplicationParser(jsonConfiguration);

		parser.parseArgument(args);

		final String resultClassName = parser.get("resultClass");

		Class<? extends eu.dnetlib.dhp.schema.oaf.Result> resultClazz = (Class<? extends eu.dnetlib.dhp.schema.oaf.Result>) Class
			.forName(resultClassName);

		Boolean isSparkSessionManaged = Optional
			.ofNullable(parser.get("isSparkSessionManaged"))
			.map(Boolean::valueOf)
			.orElse(Boolean.TRUE);

		final String inputPath = parser.get("inputPath");

		final String outputPath = parser.get("outputPath");

		SparkConf conf = new SparkConf();

		runWithSparkSession(
			conf,
			isSparkSessionManaged,
			spark -> {
				Utils.removeOutputDir(spark, outputPath);
				mapToResult(spark, inputPath, resultClazz, outputPath);
			});
	}

	private static <R extends eu.dnetlib.dhp.schema.oaf.Result> void mapToResult(SparkSession spark,
		String input_path,
		Class<R> resultClazz, String output_path) {
		Dataset<R> publicationDataset = Utils.readPath(spark, input_path, resultClazz);
		Dataset<R> result = publicationDataset.filter((FilterFunction<R>) p -> {
			if (p.getAuthor() == null)
				return false;
			if (p.getAuthor().size() == 0)
				return false;
			return true;
		});

		result.flatMap((FlatMapFunction<R, Author>) p -> {
			List<Author> reslist = new ArrayList<>();
			p.getAuthor().forEach(a -> {
				Author r = new Author();
				r.setDeletedbyinference(p.getDataInfo().getDeletedbyinference());
				r.setId(p.getId());

				r
					.setCf(
						p
							.getCollectedfrom()
							.stream()
							.map(cf -> KeyValue.newInstance(cf.getKey(), cf.getValue()))
							.collect(Collectors.toList()));

				r.setName(a.getName());
				r.setSurname(a.getSurname());
				r.setFullname(a.getFullname());
				r
					.setPid(
						p
							.getPid()
							.stream()
							.map(
								pid -> KeyValue
									.newInstance(pid.getQualifier().getClassid(), pid.getValue()))
							.collect(Collectors.toList()));
				r
					.setApid(
						Optional
							.ofNullable(a.getPid())
							.map(
								pids -> pids
									.stream()
									.map(pd -> KeyValue.newInstance(pd.getQualifier().getClassid(), pd.getValue()))
									.collect(Collectors.toList()))
							.orElse(new ArrayList<>()));
				reslist.add(r);

			});
			return reslist.iterator();
		}, Encoders.bean(Author.class))
			.write()
			.option("compression", "gzip")
			.mode(SaveMode.Overwrite)
			.json(output_path);

	}

}
