package tcra.exercises.q2_mystery_private;

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

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

   @SuppressWarnings("unchecked")
   public boolean check(List<MathType> t)
   {
      MathString<MathType> lb_self   = (MathString<MathType>)t.get(0);
      MathString<MathType> self      = (MathString<MathType>)t.get(1);
      
      if(!self.equals(lb_self.reverse())) return false;
      
      return true;
   } // end check(List)
   
   public String toString()
   {
      return("there exists int N = |#self|; A1, A2, ..., AN: Object such that<br>"+
             "#self = &lt;A1&gt; * &lt;A2&gt; * ... * &lt;AN&gt; and<br>"+
             "self = &lt;AN&gt; * &lt;AN-1&gt; * ... * &lt;A2&gt; * &lt;A1&gt;");
   } // end toString()

   @SuppressWarnings("unchecked")
   public String toInstanceString(List<MathType> t)
   {
      MathString<MathType> lb_self   = (MathString<MathType>)t.get(0);
      MathString<MathType> self      = (MathString<MathType>)t.get(1);
      
      String pre, inorder, reverse;
      if(lb_self.length() > 3)
      {
    	  pre = lb_self.elementAt(0).toHTMLString() + ", " + lb_self.elementAt(1).toHTMLString() + ", ..., " + lb_self.elementAt(lb_self.length()-1);
    	  inorder = "&lt;"+lb_self.elementAt(0).toHTMLString() + "&gt; * &lt;" +
    	            lb_self.elementAt(1).toHTMLString() + "&gt; * ... * &lt;" +
    	            lb_self.elementAt(lb_self.length()-1) + "&gt;";
    	  reverse = "&lt;"+lb_self.elementAt(lb_self.length()-1).toHTMLString() + "&gt; * &lt;" +
    	             lb_self.elementAt(lb_self.length()-2).toHTMLString() + "&gt; * ... * &lt;" +
    	             lb_self.elementAt(1) + "&gt; * &lt;" +
    	             lb_self.elementAt(0).toHTMLString() + "&gt;";
      }
      else if(lb_self.length() == 3)
      {
    	  pre = lb_self.elementAt(0).toHTMLString() + ", " + lb_self.elementAt(1).toHTMLString() + ", " + lb_self.elementAt(2);
    	  inorder = "&lt;"+lb_self.elementAt(0).toHTMLString() + "&gt; * &lt;" +
    	            lb_self.elementAt(1).toHTMLString() + "&gt; * &lt;" +
    	            lb_self.elementAt(2) + "&gt;";
    	  reverse = "&lt;"+lb_self.elementAt(2).toHTMLString() + "&gt; * &lt;" +
    	             lb_self.elementAt(1) + "&gt; * &lt;" +
    	             lb_self.elementAt(0).toHTMLString() + "&gt;";
      }
      else if(lb_self.length() == 2)
      {
    	  pre = lb_self.elementAt(0).toHTMLString() + ", " + lb_self.elementAt(1).toHTMLString();
    	  inorder = "&lt;"+lb_self.elementAt(0).toHTMLString() + "&gt; * &lt;" +
    	            lb_self.elementAt(1).toHTMLString() + "&gt;";
    	  reverse = "&lt;"+lb_self.elementAt(1) + "&gt; * &lt;" +
    	             lb_self.elementAt(0).toHTMLString() + "&gt;";
      }
      else if(lb_self.length() == 1)
      {
    	  pre = lb_self.elementAt(0).toHTMLString();
    	  inorder = "&lt;"+lb_self.elementAt(0).toHTMLString() + "&gt;";
    	  reverse = inorder;
      }
      else
      {
    	  pre = "NULL";
    	  inorder = "&lt;&gt;";
    	  reverse = inorder;
      }
      
      return("there exists int N = |"+lb_self.toHTMLString()+"|; "+pre+": Object such that<br>"+
              lb_self.toHTMLString()+" = "+inorder+" and<br>"+
              self.toHTMLString()+" = "+reverse);
   } // end toInstanceString(List)
} // end class
