package PrintGame;

import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.Attributes;
import java.lang.Integer;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URI;
import java.io.File;
import java.net.URISyntaxException;

public class GameXMLContentHandler extends DefaultHandler
{
  private String wall_picture_;
  private String way_picture_;
  private String robot_picture_;
  private String diamond_picture_;
  private String start_picture_;
  private int num_levels_;
  private int number_;
  private String file_[];
  private String current_tag_;
  private URL document_base_;

  public GameXMLContentHandler()
  {
    document_base_=null;
    wall_picture_= new String();
    way_picture_= new String();
    robot_picture_= new String();
    diamond_picture_= new String();
    start_picture_= new String();
    num_levels_ = 0;
    current_tag_= new String();
  }

  public void setDocumentBase(URL document_base)
  {
    document_base_=document_base;
  }

/**-----------------------------------------------------------------------------
 *Receive notification of the beginning of the document.
 */
  public void startDocument()
  {
    //nothing to do here.
  }

/**-----------------------------------------------------------------------------
 *Receive notification of the start of an element.
 *@param attributes - The specified or defaulted attributes.
 *@param localName - The local name (without prefix),
 *or the empty string if Namespace processing is not being performed.
 *@param qualName - The qualified name (with prefix),
 *or the empty string if qualified names are not available.
 */

  public void startElement(String uri, String localName, String qualName, Attributes attribs)
  {
    current_tag_ = qualName;
  }

/**-----------------------------------------------------------------------------
 *Receive notification of character data inside an element.
 *@param charArray - The characters.
 *@param start - The start position in the character array.
 *@param length - The number of characters to use from the character array.
 */
  public void characters(char[] charArray, int start, int length)
  {
    if(current_tag_.length() > 0)
    {
      String temp_string = new String(charArray, start, length);
      temp_string = temp_string.trim();

      if (current_tag_.equals(new String(TagName.WALLPICTURE)))
      {
        wall_picture_=temp_string;
      }

      else if (current_tag_.equals(new String(TagName.WAYPICTURE)))
      {
        way_picture_=temp_string;
      }

      else if (current_tag_.equals(new String(TagName.ROBOTPICTURE)))
      {
        robot_picture_=temp_string;
      }

      else if (current_tag_.equals(new String(TagName.DIAMONDPICTURE)))
      {
        diamond_picture_=temp_string;
      }

      else if (current_tag_.equals(new String(TagName.STARTPICTURE)))
      {
        start_picture_=temp_string;
      }

      else if (current_tag_.equals(new String(TagName.NUMLEVELS)))
      {
        num_levels_=Integer.decode(temp_string).intValue();
        file_=new String[num_levels_];
      }

      else if (current_tag_.equals(new String(TagName.NUMBER)))
      {
        int number=Integer.decode(temp_string).intValue();
        if(number>0 && number<=num_levels_)
          number_=number;
      }

      else if (current_tag_.equals(new String(TagName.FILE)))
      {
        if(number_>0 && number_<=num_levels_)
        {
          try
          {
            URL new_url = new URL(document_base_,temp_string);
            //URI uri = new URI(new_url.toString());
            //File file = new File(uri);
            String file_name = new_url.toString();
            file_[number_-1]=file_name;
          }
          catch( MalformedURLException ex )
          {
            System.err.println(ex.getMessage());
          }
          /*catch( URISyntaxException ex )
          {
            System.err.println(ex.getMessage());
          }*/
        }
      }
    }
  }

/**-----------------------------------------------------------------------------
 *Receive notification of the end of an element.
 *@param localName - The local name (without prefix), or the empty string if
 *Namespace processing is not being performed.
 *@param qName - The qualified XML 1.0 name (with prefix), or the empty string if
 *qualified names are not available.
 */
  public void endElement(String uri, String localName, String qualName)
  {
    current_tag_="";
  }

/**-----------------------------------------------------------------------------
 *Receive notification of the end of the document.
 */
  public void endDocument()
  {

  }



  public String getWallPicture()
  {
    return wall_picture_;
  }

  public String getWayPicture()
  {
    return way_picture_;
  }

  public String getRobotPicture()
  {
    return robot_picture_;
  }

  public String getDiamondPicture()
  {
    return diamond_picture_;
  }

  public String getStartPicture()
  {
    return start_picture_;
  }

  public int getNumLevels()
  {
    return num_levels_;
  }

  public String getFile(int number)
  {
    if((number<=num_levels_) && (number>=0))
      return file_[number-1];
    else
    {
      System.out.println("getFile: " + number);
      return null;
    }
  }


}
