
import java.net.*;
import java.io.*;
import java.util.*;

//import java.util.StringTokenizer;
//import java.text.BreakIterator;

//import Translation.*;
//import Transcription.*;

/** 
 * This class represents a data record of information about 
 * a language sound file. */

public class Record extends Object
{
  //--------------------------------------------
  /** The sound file which contains some human language recording */
  private String soundUrl;
  //--------------------------------------------
  /** A set of translations of the transcription into various languages */
  private TranslationSet translations;
  //--------------------------------------------
  /** A transcription of the language sound file into some writing system */
  private Transcription transcription;
  //--------------------------------------------
  /** information about cognates in other languages */
  private String cognates;
  //--------------------------------------------
  /** if the record contains some data set this to false; */
  private boolean isEmpty = true;
  //--------------------------------------------
  private boolean isGood;
  //--------------------------------------------
  private StringBuffer error;
  //--------------------------------------------
  private StringBuffer errorMessages;
  //--------------------------------------------
  /** a cross platform linebreak */
  public static String NEWLINE = System.getProperty("line.separator");

  //-- These can be changed to Translation.FIELD_NAME etc
  //--------------------------------------------
  String SOUNDURL_FIELD_NAME = "su";
  //--------------------------------------------
  String FIELD_VALUE_SEPARATOR = "=";


  //--------------------------------------------
  /** creates a record with no data. */
  public Record()
  {
    this.error = new StringBuffer("");
    this.errorMessages = new StringBuffer("");

    this.translations = new TranslationSet(); 
    this.transcription = new Transcription();
    this.soundUrl = new String("");
  } //-- constr: () 


  //--------------------------------------------
  /** Create a new Record object by parsing a string containing
   *  data. Internally Translation and Transcription objects
   *  will be created by handing string segments to their constructors */
  public Record(String sText)
  {
    this();
    this.loadData(sText);
  } 


  //--------------------------------------------
  public boolean isGood()
  {
    //if (this.isGood == null)
    // { return false; }

    return this.isGood;   
  }

  //--------------------------------------------
  /** populate internal data fields by parsing a string
   *  containing information about a sound file and its
   *  language content. 
   *  @param= sDataString string to parse 
   *  */
  public void loadData(String sDataString) 
  {
    BufferedReader brReader;
    brReader = new BufferedReader(new StringReader(sDataString));    

    String sCurrentLine = "";


    while (sCurrentLine != null)
    {
      // System.out.println(sbCurrentLine);
      
      boolean bIsTranslationData = false;
      boolean bIsTranscriptionData = false;
      boolean bIsSoundUrlData = false;

      bIsSoundUrlData = 
       sCurrentLine.toString().startsWith(
        SOUNDURL_FIELD_NAME + FIELD_VALUE_SEPARATOR);


      if (Translation.isGoodFormat(sCurrentLine))
      {
        this.translations.addTranslation(new Translation(sCurrentLine)); 
        this.isEmpty = false;
      }
      else if (Transcription.isGoodFormat(sCurrentLine))
      {
        this.transcription = new Transcription(sCurrentLine);
        this.isEmpty = false;
      }
      else if (bIsSoundUrlData)
      {
        this.soundUrl = sCurrentLine.substring(3, sCurrentLine.length());
        this.isEmpty = false;
      } //--if

      try 
      {
        sCurrentLine = brReader.readLine().trim();
      }
      catch (IOException e)
      {
        this.isGood = false;
        this.error.append("Problem reading from text data");
        this.errorMessages.append(e);
        return;
      } 
      catch (NullPointerException ee)
      {
        sCurrentLine = null;
      } //-- try

    } //-- while more lines  
    
    if ((this.translations.count() == 0) && (this.soundUrl == ""))  
    {
      this.isGood = false;
      this.error.append("Record contains no data");
      return;
    }

  } //-- method: loadData
  
  //--------------------------------------------
  /** checks whether the transcription of the sound resource
   *  consists of only a single word. This is considered useful since
   *  a language tutor program may have a vocabulary mode, in which
   *  only single words are given to the user for practice */

  public boolean isSingleWord()
  { 
    return false; 
  }

  //--------------------------------------------
  /** checks whether there is any reference to a sound resource. */
  public boolean hasSound()
  { 
    if (this.soundUrl.equals(""))
     { return false; }
    else 
     { return true; }
  }
 
  //--------------------------------------------
  /** set the pointer to a sound resource. */
  public void setSoundUrl(String sSoundUrl)
  {
    this.soundUrl = sSoundUrl;
  }
 
  //--------------------------------------------
  /** returns the pointer to a sound resource */

  public String getSoundUrl()
  {
    return this.soundUrl;
  }


  //--------------------------------------------
  /** print a report about the record */
  public String printReport()
  { 
    StringBuffer sbOutput = new StringBuffer("");
    sbOutput.append("Sound Resource        :" + this.soundUrl);
    sbOutput.append(NEWLINE);
    sbOutput.append("Record has data       :" + this.isEmpty);
    sbOutput.append(NEWLINE);
    sbOutput.append("Number of translations:" + this.translations.count());
    sbOutput.append(NEWLINE);
    return sbOutput.toString();
  }

  //--------------------------------------------
  /** create some kind of textual representation of the object. 
   *  suitable for displaying to the console or plain text element */
  public String print()
  { 
    StringBuffer sbOutput = new StringBuffer("");
    sbOutput.append("=");
    sbOutput.append(NEWLINE);
    sbOutput.append("su=" + this.soundUrl + NEWLINE);
    sbOutput.append(this.translations.print());
    sbOutput.append(this.transcription.print());
    sbOutput.append("=");
    sbOutput.append(NEWLINE);
    return sbOutput.toString();
  }

  //--------------------------------------------
  /** creates a textual representation of the object, probably
   *  in a format suitable for saving in a text data file. */ 
  public String toString()
  {
    StringBuffer sbOutput = new StringBuffer("");
    sbOutput.append(NEWLINE);
    sbOutput.append("=");
    sbOutput.append(NEWLINE);
    sbOutput.append(
     SOUNDURL_FIELD_NAME + FIELD_VALUE_SEPARATOR + this.soundUrl);
    sbOutput.append(NEWLINE);
    sbOutput.append(this.translations.toString());
    sbOutput.append(this.transcription.toString());
    sbOutput.append(NEWLINE);
    return sbOutput.toString();
  }

  //--------------------------------------------
  /** provides a string which is informative about the 
   *  internal state of the object */

  public String debug()
  {
    StringBuffer sbReturn = new StringBuffer("");
    sbReturn.append("Record Object -->> " + NEWLINE);
    sbReturn.append("Record-->>soundUrl=" + this.soundUrl + NEWLINE); 
    sbReturn.append(this.translations.debug() + NEWLINE);
    sbReturn.append(this.transcription.debug());
    return sbReturn.toString();
  } 

  //--------------------------------------------
  /** A main method to allow for testing of the class from the console.
   *  This may be moved to a separate file */

  public static void main(String[] args) throws Exception
  {
    String NEWLINE = System.getProperty("line.separator");

    StringBuffer sbTest = new StringBuffer("");
    sbTest.append("=" + NEWLINE);
    sbTest.append(" su");
    sbTest.append(NEWLINE);
    sbTest.append(NEWLINE);
    sbTest.append(NEWLINE);
    sbTest.append(" su=http://bumble.sf.net/maghreb/10.mp3");
    sbTest.append(NEWLINE);
    sbTest.append(" tr  ");
    sbTest.append(NEWLINE);
    sbTest.append(" tr =  ");
    sbTest.append(NEWLINE);
    sbTest.append(" tr =  ");
    sbTest.append(NEWLINE);
    sbTest.append(" tr =  ");
    sbTest.append(NEWLINE);
    sbTest.append(" tr = [en]I am speaking english very well");
    sbTest.append(NEWLINE);
    sbTest.append(" tr = [fr]Je parle francais tres bien ");
    sbTest.append(NEWLINE);
    sbTest.append(" tr = [es]Hablo espanyol");
    sbTest.append(NEWLINE);
    sbTest.append(" tr = [ca]jo puc parlar frances");
    sbTest.append(NEWLINE);
    sbTest.append(" tr = [de]Ich spreche deutsche");
    sbTest.append(NEWLINE);
    sbTest.append(" ts =ana kankool inglizi mzien bzef ");
    sbTest.append(NEWLINE);
    sbTest.append("=");
    sbTest.append(NEWLINE);

    System.out.println("Using data:");
    System.out.println(sbTest);
    Record rTestRecord = new Record(sbTest.toString());
    System.out.println(NEWLINE);
    System.out.println(rTestRecord.toString());
    System.out.println(rTestRecord.printReport());

  } //-- main()
} //-- Record class
