package edu.vt.marian.Document;

import java.io.*;
import java.net.*;
import java.util.*;

import edu.vt.marian.common.*;


/**
    A "variable field" in a US MARC record that contains a personal name:
	that is, a 100, 400, 600, 700, or 800 field.
    @author	Robert France

*/

public class MarcPersNameField extends PresentableMarcVarField
{
    /** Labels for subfields reported in a long description.
    */
    private final static String person_name_labels = "abcdeknpqt";


    /**
        Create a MarcPersNameField object with no subfields.
        @param	id -- the variable field id.
        @param	xMap -- used to convert ANSEL to (e.g.) XML.
        @param	debug -- used for debugging
    */      
    public MarcPersNameField(int id, EntityMap xMap, Debug debug)
    {
        super(id, xMap, debug);
        isPersName = true;
     }


    /**
        "Short" presentation for a personal name variable field:  use 'a' and 'b'
              subfields.
        @param	markupType    see edu.vt.marian.common.DigInfObj
        @param	out	A BufferedWriter (presumably String or OutputStream) to present on.
        @return    OK -- everything jake.
        <BR>    IO_ERROR or PARSE_ERROR -- problems.
    */      
    public int presentShort(int markupType, BufferedWriter out) throws IOException
    {
		int Err;
        switch ( markupType )
        {
         default:
            debug.dumpTrace("MarcPersNameField.presentShort(): Unexpected markup type " +
                            markupType + ":  treating as ASCII.");
            // Fall through:

         case DigInfObj.XML:
         case DigInfObj.SGML:
         case DigInfObj.HTML:
         case DigInfObj.ASCII:
         case DigInfObj.ANSEL:
            Enumeration subfld = subfields.elements();
            try { while ( true )
            {
                MarcSubField sf = (MarcSubField) subfld.nextElement();
                if ( ( sf.getLabel() == 'a' ) || ( sf.getLabel() == 'b' ) )
                   if ( (Err = sf.present(markupType, out)) != ReturnCodes.OK )
                        return( Err );
                if ( subfld.hasMoreElements() )
                   out.write(' ');
            } } catch( NoSuchElementException e) {};
        }
     return( ReturnCodes.OK );
    }


    /**
        "Long" presentation for a personal name variable field.
        @param	markupType    see edu.vt.marian.common.DigInfObj
        @param	out	A BufferedWriter (presumably String or OutputStream) to present on.
        @return    OK -- everything jake.
        <BR>    IO_ERROR or PARSE_ERROR -- problems.
    */      
    public int presentLong(int markupType, BufferedWriter out) throws IOException 
    {
        Enumeration subfld = subfields.elements();
		int Err;
        switch ( markupType )
        {
         case DigInfObj.XML:	// Use default.
         case DigInfObj.SGML:
	        return( super.presentLong(markupType, out) );

        case DigInfObj.HTML:
            // Presentation:  make the (unique) 'a' subfield a hot link
            //  using the full indexed form of the name as exact query text.

            // Build URL.
            StringWriter sw = new StringWriter();
            BufferedWriter bsw = new BufferedWriter( sw );
            if ( (Err = presentLong(DigInfObj.ANSEL, bsw)) != ReturnCodes.OK )
                return( Err );
			bsw.flush();
            String queryString = "text1_type=Exact Personal Authors/text1=" +
                                    sw.toString();

            try { while ( true )
            {
                MarcSubField sf = (MarcSubField) subfld.nextElement();
                if (sf.getLabel() == 'a')
                {
                    presentUrl(queryString, out);
                    if ( (Err = sf.present(markupType, out)) != ReturnCodes.OK )
                        return( Err );
                    out.write("</A>");
                }
                else if (sf.getLabel() == '4')
                {
                    out.write("[");
                    if ( (Err = sf.present(markupType, out)) != ReturnCodes.OK )
                        return( Err );
                    out.write("]");
                }
                else if (person_name_labels.indexOf(sf.getLabel()) != -1)
                {
                    if ( (Err = sf.present(markupType, out)) != ReturnCodes.OK )
                        return( Err );
                }
                if ( subfld.hasMoreElements() )
                   out.write(' ');
            } } catch( NoSuchElementException e) {};
            return( ReturnCodes.OK );

        default:
            debug.dumpTrace("MarcPersNameField.presentLong(): Unexpected markup type " +
                            markupType + ":  treating as ASCII.");
            // Fall through:

         case DigInfObj.ASCII:
         case DigInfObj.ANSEL:
            try { while ( true )
            {
                MarcSubField sf = (MarcSubField) subfld.nextElement();
                if (sf.getLabel() == '4')
                {
                    out.write("[");
                    if ( (Err = sf.present(markupType, out)) != ReturnCodes.OK )
                        return( Err );
                    out.write("]");
                }
                else if (person_name_labels.indexOf(sf.getLabel()) != -1)
                {
                    if ( (Err = sf.present(markupType, out)) != ReturnCodes.OK )
                        return( Err );
                }
                if ( subfld.hasMoreElements() )
                   out.write(' ');
            } } catch( NoSuchElementException e) {};
            return( ReturnCodes.OK );
        }
    }

}

