
package eu.dnetlib.dhp.ircdl_extention;

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

import org.apache.neethi.Assertion;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.jayway.jsonpath.WriteContext;

import eu.dnetlib.dhp.ircdl_extention.model.Orcid;
import eu.dnetlib.dhp.ircdl_extention.model.Result;
import eu.dnetlib.dhp.ircdl_extention.model.ShuffleInfo;
import scala.Tuple2;

public class WrongOrcidTest {

	@Test
	public void wrongOrcidFalse() throws Exception {
		Assertions
			.assertTrue(
				Utils
					.filterFunction(
						new Tuple2<>(Result.newInstance("veigas pires cristina"),
							Orcid
								.newInstance(
									"cristina", "veiga pires", "c veiga pires",
									Arrays.asList("c c veiga pires")))));

	}

	@Test
	public void wrongOrcidFalse2() throws Exception {
		Assertions
			.assertTrue(
				Utils
					.filterFunction(
						new Tuple2<>(Result.newInstance("yushkevich p"),
							Orcid
								.newInstance(
									"paul", "yushkevich", "paul a yushkevich",
									new ArrayList<>()))));

	}

	@Test
	public void wrongOrcidFalse3() throws Exception {
		Assertions
			.assertTrue(
				Utils
					.filterFunction(
						new Tuple2<>(Result.newInstance("ghosh ss"),
							Orcid
								.newInstance(
									"satrajit", "ghosh",
									"satra",
									Arrays.asList("satra", "satrajit s ghosh")))));

	}

	@Test
	public void wrongOrcidTrue() throws Exception {
		Assertions
			.assertFalse(
				Utils
					.filterFunction(
						new Tuple2<>(Result.newInstance("kluft lukas"),
							Orcid
								.newInstance(
									"satrajit", "ghosh",
									"satra",
									Arrays.asList("satra", "satrajit s ghosh")))));

	}

	@Test
	public void wrongOrcidFalse4() throws Exception {
		Assertions
			.assertTrue(
				Utils
					.filterFunction(
						new Tuple2<>(Result.newInstance("schulz s a"),
							Orcid
								.newInstance(
									"sebastian", "schulz",
									"sebastian a schulz",
									new ArrayList<>()))));

	}

	@Test
	public void wrongOrcidFalse5() throws Exception {
		Assertions
			.assertTrue(
				Utils
					.filterFunction(
						new Tuple2<>(Result.newInstance("domingues af"),
							Orcid
								.newInstance(
									"allysson", "domingues",
									"allysson f domingues",
									new ArrayList<>()))));

	}

	@Test
	public void wrongOrcidFalseConservative() throws Exception {
		Assertions
			.assertTrue(
				Utils
					.conservativeFilterFunction(
						new Tuple2<>(Result.newInstance("veigas pires cristina"),
							Orcid
								.newInstance(
									"cristina", "veiga pires", "c veiga pires",
									Arrays.asList("c c veiga pires")))));

	}

	@Test
	public void wrongOrcidFalseConservative2() throws Exception {
		Assertions
			.assertTrue(
				Utils
					.conservativeFilterFunction(
						new Tuple2<>(Result.newInstance("yushkevich p"),
							Orcid
								.newInstance(
									"paul", "yushkevich", "paul a yushkevich",
									new ArrayList<>()))));

	}

	@Test
	public void wrongOrcidFalseConservative3() throws Exception {
		Assertions
			.assertTrue(
				Utils
					.conservativeFilterFunction(
						new Tuple2<>(Result.newInstance("ghosh ss"),
							Orcid
								.newInstance(
									"satrajit", "ghosh",
									"satra",
									Arrays.asList("satra", "satrajit s ghosh")))));

	}

	@Test
	public void wrongOrcidTrueConservative() throws Exception {
		Assertions
			.assertFalse(
				Utils
					.conservativeFilterFunction(
						new Tuple2<>(Result.newInstance("kluft lukas"),
							Orcid
								.newInstance(
									"satrajit", "ghosh",
									"satra",
									Arrays.asList("satra", "satrajit s ghosh")))));

	}

	@Test
	public void wrongOrcidFalseConservative4() throws Exception {
		Assertions
			.assertTrue(
				Utils
					.conservativeFilterFunction(
						new Tuple2<>(Result.newInstance("schulz s a"),
							Orcid
								.newInstance(
									"sebastian", "schulz",
									"sebastian a schulz",
									new ArrayList<>()))));

	}

	@Test
	public void wrongOrcidFalseConservative5() throws Exception {
		Assertions
			.assertTrue(
				Utils
					.conservativeFilterFunction(
						new Tuple2<>(Result.newInstance("domingues af"),
							Orcid
								.newInstance(
									"allysson", "domingues",
									"allysson f domingues",
									new ArrayList<>()))));

	}

	@Test
	public void wrongOrcidTrueConservative2() throws Exception {
		Assertions
			.assertFalse(
				Utils
					.conservativeFilterFunction(
						new Tuple2<>(Result.newInstance("figueiredo pontes lorena lobo de"),
							Orcid
								.newInstance(
									"moyses", "soares",
									"moyses antonio porto soares",
									new ArrayList<>()))));

	}

	@Test
	public void wrongOrcidFalseConservative6() throws Exception {
		Assertions
			.assertTrue(
				Utils
					.conservativeFilterFunction(
						new Tuple2<>(Result.newInstance("da luz geraldo eduardo"),
							Orcid
								.newInstance(
									"geraldo", "luz jr",
									"luz jr g e",
									new ArrayList<>()))));

	}

	@Test
	public void testShuffle() throws Exception {

		List<ShuffleInfo> shuffleInfoList = new ArrayList<>();

		shuffleInfoList
			.add(
				ShuffleInfo
					.newInstance(
						"Miriam", "Baglioni", "Miriam Baglioni", "50|fake_1",
						"Alessia", "Bardi", "", new ArrayList<String>(), "orcid_alessia"));
		shuffleInfoList.add(ShuffleInfo.newInstance("Alessia", "Bardi", "Alessia Bardi", "50|fake_1"));
		shuffleInfoList.add(ShuffleInfo.newInstance("Miriam", "Baglioni", "Miriam Baglioni", "50|fake_1"));
		shuffleInfoList
			.add(
				ShuffleInfo
					.newInstance(
						"Alessia", "Bardi", "Alessia Bardi", "50|fake_1",
						"Miriam", "Baglioni", "", new ArrayList<String>(), "orcid_miriam"));
		shuffleInfoList.add(ShuffleInfo.newInstance("Claudio", "Atzori", "Claudio Atzori", "50|fake_1"));

		List<ShuffleInfo> tmp = shuffleInfoList
			.stream()
			.filter(e -> Optional.ofNullable(e.getOrcid()).isPresent())
			.collect(Collectors.toList());
		int count = 0;
		for (ShuffleInfo e : tmp) {
			if (verifyShuffle(e, shuffleInfoList))
				count++;

		}

		System.out.println(count);
	}

	private boolean verifyShuffle(ShuffleInfo e, List<ShuffleInfo> shuffleInfoList) {
		return shuffleInfoList.stream().anyMatch(si -> {
			try {
				final Orcid orcid = Orcid
					.newInstance(e.getOname(), e.getOsurname(), e.getOcreditName(), e.getoOtherNames());
				return Utils
					.filterFunction(
						new Tuple2<>(Result.newInstance(si.getAfullname()), orcid));
			} catch (JsonProcessingException ex) {
				ex.printStackTrace();
			}
			return false;
		});
	}
}
