package uk.co.patrickhaston.mars;

public class MarsEnables extends MarsArray 
{
  public MarsEnables(MarsModel model)
  {
    super(model);
    this.CollectionName = "[Enables]";
  }

  public void readData(String line)
  {
    int idx = readIndex(line);
    if (idx>0)
    {
      MarsEnable e = new MarsEnable(line);

      this.setElementAt(e, idx);
    }
  }
  
  public boolean isOldTechnology(int techId)
  {
    MarsEnable e = null;
    for(int i=0; i<size(); i++)
    {
      e = (MarsEnable) get(i);
      if(e != null)
      {
        if(e.getNewTechnology() == techId)
        {
          return false;
        }
      }
    }
    // no enable required => it is an existing technology
    return true;
  }
  
  public boolean isOldConstruction(int conId)
  {
    MarsEnable e = null;
    for(int i=0; i<size(); i++)
    {
      e = (MarsEnable) get(i);
      if(e != null)
      {
        if(e.getNewConstruction() == conId)
        {
          return false;
        }
      }
    }
    // no enable required => it is an existing construction type
    return true;
  }
  
  public boolean isTechnologyResearchable(int techId)
  {
    MarsDiscoveries discovered = theModel.Discoveries;
    boolean enabled = true;
    MarsEnable e = null;
    
    if(isOldTechnology(techId))
    {
      // cannot research something we already know...
      return false;
    }
    
    for(int i=1; i<size(); i++)
    {
      e = (MarsEnable) get(i);
      if(e != null)
      {
        if(e.getNewTechnology() == techId)
        {
          // We've got a match - we've found a technology that requires
          // a discovery.  Now we need to find out if we've discovered
          // the required technology...
          if(!discovered.isDiscovered(e.getTechnology()))
          {
            // The required technology has not been discovered so this
            // technology is not available to research.
            return false;
          }
        }
      }
    }
    // The default position is that this technology is available
    return true;
  }

  public boolean isConstructionResearchable(int conId)
  {
    MarsDiscoveries discovered = theModel.Discoveries;
    boolean enabled = true;
    MarsEnable e = null;
    
    if(isOldConstruction(conId))
    {
      // cannot research something we already know...
      return false;
    }
    
    for(int i=1; i<size(); i++)
    {
      e = (MarsEnable) get(i);
      if(e != null)
      {
        if(e.getNewConstruction() == conId)
        {
          // We've got a match - we've found a construction that requires
          // a discovery.  Now we need to find out if we've discovered
          // the required technology...
          if(!discovered.isDiscovered(e.getTechnology()))
          {
            // The required technology has not been discovered so this
            // construction is not available to research.
            return false;
          }
        }
      }
    }
    // The default position is that this construction is available
    return true;
  }
}
