package tcra.exercises.q2_mystery_private;

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

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

   @SuppressWarnings("unchecked")
   public boolean check(List<MathType> t)
   {
	  MathString<MathType> lb_self  = (MathString<MathType>)t.get(0);
      MathInteger          lb_index = (MathInteger)t.get(1);
      MathInteger          lb_count = (MathInteger)t.get(2);
      MathString<MathType>    self  = (MathString<MathType>)t.get(3);
      MathInteger             index = (MathInteger)t.get(4);
      MathInteger             count = (MathInteger)t.get(5);
      
      MathString<MathType> a, b, c, temp;
      MathInteger          d;
      
      a = lb_self.split(lb_index.getValue()-lb_count.getValue()-1).get(0);
      temp = lb_self.split(lb_index.getValue()-lb_count.getValue()-1).get(1);
      b = temp.split(lb_count.getValue()-1).get(0);
      temp = temp.split(lb_count.getValue()-1).get(1);
      d = (MathInteger)temp.elementAt(0);
      c = temp.split(0).get(1);
      
      if(!lb_self.equals(a.concat(b.concat(c.prepend(d))))) return false;
      if(a.length() != lb_index.getValue() - lb_count.getValue()) return false;
      if(b.length() != lb_count.getValue()) return false;
      if(!self.equals(a.concat(c.prepend(b.prepend(d))))) return false;
      
      return true;
   } // end check(List)
   
   public String toString()
   {
      return("there exists A, B, C: String of Object; D: Object such that<br>"+
             "#self = A * B * &lt;D&gt; * C and<br>"+
             "|A| = index - count and<br>"+
             "|B| = count and<br>"+
             "self = A * &lt;D&gt; * B * C;");
   } // end toString()

   @SuppressWarnings("unchecked")
   public String toInstanceString(List<MathType> t)
   {
      MathString<MathType> lb_self  = (MathString<MathType>)t.get(0);
      MathInteger          lb_index = (MathInteger)t.get(1);
      MathInteger          lb_count = (MathInteger)t.get(2);
      MathString<MathType>    self  = (MathString<MathType>)t.get(3);
      MathInteger             index = (MathInteger)t.get(4);
      MathInteger             count = (MathInteger)t.get(5);
	      
      MathString<MathType> a, b, c, temp;
      MathInteger          d;
	      
      a = lb_self.split(lb_index.getValue()-lb_count.getValue()-1).get(0);
      temp = lb_self.split(lb_index.getValue()-lb_count.getValue()-1).get(1);
      b = temp.split(lb_count.getValue()-1).get(0);
      temp = temp.split(lb_count.getValue()-1).get(1);
      d = (MathInteger)temp.elementAt(0);
      c = temp.split(0).get(1);
      
      return("there exists A, B, C: String of Object; D: Object such that<br>"+
             lb_self.toHTMLString()+" = "+a.toHTMLString()+" * "+b.toHTMLString()+
             " * &lt;"+d.toHTMLString()+"&gt; * "+c.toHTMLString()+" and<br>"+
             "|"+a.toHTMLString()+"| = "+lb_index.toHTMLString()+
             " - "+lb_count.toHTMLString()+" and<br>"+
             "|"+b.toHTMLString()+"| = "+lb_count.toHTMLString()+" and<br>"+
             self.toHTMLString()+" = "+a.toHTMLString()+" * &lt;"+d.toHTMLString()+
             "&gt; * "+b.toHTMLString()+" * "+c.toHTMLString()+";");
   } // end toInstanceString(List)
} // end class
