package inferencerule;
import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.Test;

import java.util.List;
import java.util.Set;

import inference.ProofSystem;
import inference.axioms.RightSubstitution;
import models.formulas.DependencyFormula;
import models.formulas.EquationFormula;
import models.formulas.Formula;
import models.terms.DependencyTerm;
import models.terms.Resource;
import models.terms.ResourceConstant;

public class EqualityAxiomTest {
	
	Resource a = new Resource("a", 1);
	Resource b = new Resource("b", 1);
	Resource c = new Resource("c", 1);
	Resource d = new Resource("d", 1);
	Resource e = new Resource("e", 1);
	Resource f = new Resource("f", 1);
	Resource g = new Resource("g", 1);
	Resource h = new Resource("h", 1);
	Resource i = new Resource("i", 1);
	Resource j = new Resource("j", 1);
	Resource k = new Resource("k", 1);
	Resource l = new Resource("l", 1);
	
	@Test
	void ReflexivityTest() {
	}
	
	@Test
	void SymmetryTest() {
		EquationFormula eq1 = new EquationFormula(a, b);
		Set<Formula> result = ProofSystem.symmetry.apply(eq1);
		assertTrue(result.contains(new EquationFormula(b, a)));
	}
	
	@Test
	void TransitivityTest() {
		EquationFormula eq1 = new EquationFormula(a, b);
		EquationFormula eq2 = new EquationFormula(b, c);
		EquationFormula eq3 = new EquationFormula(a, c);
		Set<Formula> result = ProofSystem.transitivity.apply(eq1, eq2);
		assertTrue(result.contains(eq3));
		
	}
	
	@Test
	void RightSubTest() {
		RightSubstitution rs = new RightSubstitution();
		EquationFormula eq = new EquationFormula(a, b);
		DependencyFormula dep = new DependencyFormula(c, d);
		DependencyTerm t1 = new DependencyTerm(c, d, a);
		DependencyTerm t2 = new DependencyTerm(c, d, b);
		EquationFormula eq2 = new EquationFormula(new DependencyTerm(d, e, f), a);
		Set<Formula> result = rs.apply(Set.of(eq, dep, eq2)); 
		assertTrue(result.contains(new EquationFormula(t1, t2)));
		
		assertEquals(rs.generateRightSideHand(List.of(eq, dep, eq2), t1), t2);
		DependencyFormula dep2 = new DependencyFormula(c, g);
		EquationFormula eq3 = new EquationFormula(new DependencyTerm(g, h, i), j);
		DependencyTerm t3 = new DependencyTerm(c, d, a, g, j);
		DependencyTerm t4 = new DependencyTerm(c, d, b, g, j);
		result = rs.apply(Set.of(eq, eq2, eq3, dep, dep2));
		assertTrue(result.contains(new EquationFormula(t3, t4)));
	}
	
	@Test
	void LeftSubTest() {
		EquationFormula eq1 = new EquationFormula(a, b);
		DependencyFormula d1 = new DependencyFormula(a, c);
		EquationFormula eq2 = new EquationFormula(new DependencyTerm(c, d, e), f);
		Set<Formula> result = ProofSystem.leftSubstitution.apply(eq1, d1, eq2);
		assertTrue(result.contains(new EquationFormula(new DependencyTerm(a, c, f), new DependencyTerm(b, c, f))));
	}
	
	@Test
	void IdentityTest() {
		EquationFormula eq1 = new EquationFormula(new DependencyTerm(a, b, c), d);
		Set<Formula> result = ProofSystem.identity.apply(eq1);
		assertTrue(result.contains(new EquationFormula(new DependencyTerm(a, a, d), d)));
		
		EquationFormula eq2 = new EquationFormula(new DependencyTerm(a, b, c), d);
		EquationFormula eq3 = new EquationFormula(new DependencyTerm(e, f, g), h);
		result = ProofSystem.identity.apply(eq2, eq3);
		assertTrue(result.contains(new EquationFormula(new DependencyTerm(a, a, d, e, h), d)));
	}
	
	@Test
	void PseudoConstantnessTest() {
		Set<Formula> result = ProofSystem.pseudoConstantness.apply(new DependencyFormula(a, b));
		assertTrue(result.contains(new EquationFormula(new DependencyTerm(a, b, b), a)));
		
		result =  ProofSystem.pseudoConstantness.apply(new DependencyFormula(a, b, c, d));
		assertTrue(result.contains(new EquationFormula(new DependencyTerm(a, b, b, c, c, d, d), a)));
	}
	
	@Test
	void ArgumentDependencyExtractionTest() {
		Set<Formula> result = ProofSystem.argumentDependencyExtraction.apply(
				new DependencyFormula(a, b, c), 
				new DependencyFormula(b, c), 
				new EquationFormula(new DependencyTerm(a, b, d, c, e), new ResourceConstant("ccc")));
		assertTrue(result.contains(new EquationFormula(new DependencyTerm(b, c, e), d)));
	}
	
}
