package tcra.exercises.q2_mystery_private;

import java.util.*;
import tcra.math.*;

public class Q2MysteryExercise_PostMystery5Assertion implements
      Assertion
{
   public Q2MysteryExercise_PostMystery5Assertion() {}

   @SuppressWarnings("unchecked")
   public boolean check(List<MathType> t)
   {
      MathString<MathType> lb_self = (MathString<MathType>)t.get(0);
      MathInteger          lb_a1   = (MathInteger)t.get(1);
      MathInteger          lb_a2   = (MathInteger)t.get(2);
      MathInteger          lb_b1   = (MathInteger)t.get(3);
      MathInteger          lb_b2   = (MathInteger)t.get(4);
      MathString<MathType> self    = (MathString<MathType>)t.get(5);
      MathInteger          a1      = (MathInteger)t.get(6);
      MathInteger          a2      = (MathInteger)t.get(7);
      MathInteger          b1      = (MathInteger)t.get(8);
      MathInteger          b2      = (MathInteger)t.get(9);
      
      MathString<MathType> A, B, C, D, E, temp;
      
      A = lb_self.split(lb_a1.getValue()-1).get(0);
      temp = lb_self.split(lb_a1.getValue()-1).get(1);
      B = temp.split(lb_a2.getValue() - lb_a1.getValue() - 1).get(0);
      temp = temp.split(lb_a2.getValue() - lb_a1.getValue() - 1).get(1);
      C = temp.split(lb_b1.getValue() - a2.getValue() - 1).get(0);
      temp = temp.split(lb_b1.getValue() - a2.getValue() - 1).get(1);
      D = temp.split(lb_b2.getValue() - b1.getValue() - 1).get(0);
      E = temp.split(lb_b2.getValue() - b1.getValue() - 1).get(1);
      
      if(!lb_self.equals(A.concat(B.concat(C.concat(D.concat(E)))))) return false;
      if(A.length() != lb_a1.getValue()) return false;
      if(A.concat(B.concat(C)).length() != lb_b1.getValue()) return false;
      if(B.length() != lb_a2.getValue() - lb_a1.getValue()) return false;
      if(D.length() != lb_b2.getValue() - lb_b1.getValue()) return false;
      if(!self.equals(A.concat(D.concat(C.concat(B.concat(E)))))) return false;
      
      return true;
   } // end check(List)
   
   public String toString()
   {
      return("there exists A, B, C, D, E: String of Object such that<br>"+
             "#self = A * B * C * D * E and<br>"+
             "|A| = a1 and<br>"+
             "|A * B * C| = b1 and<br>"+
             "|B| = a2 - a1 and<br>"+
             "|D| = b2 - b1 and <br>"+
             "self = A * D * C * B * E;");
   } // end toString()

   @SuppressWarnings("unchecked")
   public String toInstanceString(List<MathType> t)
   {
      MathString<MathType> lb_self = (MathString<MathType>)t.get(0);
      MathInteger          lb_a1   = (MathInteger)t.get(1);
      MathInteger          lb_a2   = (MathInteger)t.get(2);
      MathInteger          lb_b1   = (MathInteger)t.get(3);
      MathInteger          lb_b2   = (MathInteger)t.get(4);
      MathString<MathType> self    = (MathString<MathType>)t.get(5);
      MathInteger          a1      = (MathInteger)t.get(6);
      MathInteger          a2      = (MathInteger)t.get(7);
      MathInteger          b1      = (MathInteger)t.get(8);
      MathInteger          b2      = (MathInteger)t.get(9);
	      
      MathString<MathType> A, B, C, D, E, temp;
	  
      A = lb_self.split(lb_a1.getValue()-1).get(0);
      temp = lb_self.split(lb_a1.getValue()-1).get(1);
      B = temp.split(lb_a2.getValue() - lb_a1.getValue() - 1).get(0);
      temp = temp.split(lb_a2.getValue() - lb_a1.getValue() - 1).get(1);
      C = temp.split(lb_b1.getValue() - a2.getValue() - 1).get(0);
      temp = temp.split(lb_b1.getValue() - a2.getValue() - 1).get(1);
      D = temp.split(lb_b2.getValue() - b1.getValue() - 1).get(0);
      E = temp.split(lb_b2.getValue() - b1.getValue() - 1).get(1);
      
      return("there exists A, B, C, D, E: String of Object such that<br>"+
             lb_self.toHTMLString()+" = "+A.toHTMLString()+" * "+
             B.toHTMLString()+" * "+C.toHTMLString()+" * "+
             D.toHTMLString()+" * "+E.toHTMLString()+" and<br>"+
             "|"+A.toHTMLString()+"| = "+lb_a1.toHTMLString()+" and<br>"+
             "|"+A.toHTMLString()+" * "+B.toHTMLString()+" * "+
             C.toHTMLString()+"| = "+b1.toHTMLString()+" and<br>"+
             "|"+B.toHTMLString()+"| = "+lb_a2.toHTMLString()+" - "+
             lb_a1.toHTMLString()+" and<br>"+
             "|"+D.toHTMLString()+"| = "+lb_b2.toHTMLString()+" - "+
             lb_b1.toHTMLString()+" and <br>"+
             self.toHTMLString()+" = "+A.toHTMLString()+" * "+D.toHTMLString()+
             " * "+C.toHTMLString()+" * "+B.toHTMLString()+" * "+E.toHTMLString()+
             ";");
   } // end toInstanceString(List)
} // end class
