package tcra.exercises.resolve_random_access_capability_private;

import java.util.*;

import tcra.math.*;

public class ResolveRandomAccessCapability_PostInsertAssertion
       implements Assertion
{
    public ResolveRandomAccessCapability_PostInsertAssertion() {}
   
   @SuppressWarnings("unchecked")
   public boolean check(List<MathType> t)
   {
      MathString<MathType> lb_self   = (MathString<MathType>)t.get(0);
      MathInteger          lb_insert = (MathInteger)t.get(1);
      MathInteger          lb_index  = (MathInteger)t.get(2);
      MathString<MathType> self   = (MathString<MathType>)t.get(3);
      MathInteger          index  = (MathInteger)t.get(5);
      
      Vector<MathString<MathType>> split =
                                 lb_self.split(lb_index.getValue()-2);
      MathString<MathType> alpha = split.get(0);
      MathString<MathType> beta  = split.get(1);
                  
      if(!self.equals(alpha.concat(lb_insert).concat(beta)))
         return false;
      if(index.getValue() != alpha.length()+1)
         return false;
      if(!lb_index.equals(index))
         return false;
       
      return true;
    }

   public String toString()
   {
      return("∃ α, β: Str(Integer) ∋ "+
             "#S = α * β and<br>"+
            "|α| + 1 = k and S = α * &lt;#I&gt; * β;");
   }
   
   @SuppressWarnings("unchecked")
   public String toInstanceString(List<MathType> t)
   {
      MathString<MathType> lb_self   = (MathString<MathType>)t.get(0);
      MathInteger          lb_insert = (MathInteger)t.get(1);
      MathInteger          lb_index  = (MathInteger)t.get(2);
      MathString<MathType> self      = (MathString<MathType>)t.get(3);
      
      Vector<MathString<MathType>> split =
                                 lb_self.split(lb_index.getValue()-2);
      MathString<MathType> alpha = split.get(0);
      MathString<MathType> beta  = split.get(1);
      
      return("∃ α, β: Str(Integer) ∋"+
             lb_self.toHTMLString()+ " = " +alpha.toHTMLString()+
             " * " +beta.toHTMLString()+ " and<br>"+
             "|" +alpha.toHTMLString()+ "| + 1 = " +
             lb_index.toHTMLString() +
             " and " +self.toHTMLString()+ " = " +
             alpha.toHTMLString()+
             "* &lt;" +lb_insert.toHTMLString()+ "&gt; * " +
             beta.toHTMLString()+ ";");
   }
}
