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 imprint data:
	that is, a 260 field.
    @author	Robert France

*/

public class MarcImprintField extends PresentableMarcVarField
{
    /** Labels for subfields reported in a long presentation (NOT USED: 
	instead, all subfields are currently reported in a long presentation).
    */
    private final static String title_labels = "abcdfklnpst";


    /**
        Create a MarcImprintField 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 MarcImprintField(int id, EntityMap xMap, Debug debug)
    {
        super(id, xMap, debug);
        isImprint = true;
    }


    /**
        "Short" presentation for a corporate 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 presentShort(int markupType, BufferedWriter out) throws IOException
    {
		int Err;
        switch ( markupType )
        {
         default:
            debug.dumpTrace("MarcImprintField.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:
            out.write('(');
            boolean seen_place = false;
            boolean seen_publisher = false;

            Enumeration subfld = subfields.elements();
            try { while ( true )
            {
                MarcSubField sf = (MarcSubField) subfld.nextElement();
                switch ( sf.getLabel() )
                {
                 case 'a':
                    if (seen_publisher)
                    {
                        out.write("; ");
                        seen_publisher = false;
                    }
                    else if (seen_place)
                    {
                        out.write(", ");
                    }
                    seen_place = true;
                    if ( (Err = sf.present(markupType, out)) != ReturnCodes.OK )
                        return( Err );
                    break;

                 case 'b':
                    if (seen_place)
                    {
                        out.write(": ");
                    }
                    else if (seen_publisher)
                    {
                        out.write(", ");
                    }
                    seen_place = false;
                    seen_publisher = true;
                    if ( (Err = sf.present(markupType, out)) != ReturnCodes.OK )
                        return( Err );
                    break;

                 case 'c':
                    if (seen_place || seen_publisher)
                    {
                        out.write(", ");
                    }
                    if ( (Err = sf.present(markupType, out)) != ReturnCodes.OK )
                        return( Err );
                    break;

                 default:
                } // end -- switch
            } } catch( NoSuchElementException e) {};
        }
     return( ReturnCodes.OK );
    }


    /**
        "Long" presentation for a conference 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 
    {
		int Err;
        switch ( markupType )
        {
         case DigInfObj.XML:	// Use default.
         case DigInfObj.SGML:
	        return( super.presentLong(markupType, out) );

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

        case DigInfObj.HTML:
		case DigInfObj.ASCII:	//**DEVEL: This is not right.  We should be at least
		case DigInfObj.ANSEL:	//**  as careful here as in presentShort().
            Enumeration subfld = subfields.elements();
            try { while ( true )
            {
                MarcSubField sf = (MarcSubField) subfld.nextElement();
                if ( (Err = sf.present(markupType, out)) != ReturnCodes.OK )
                    return( Err );
                if ( subfld.hasMoreElements() )
                   out.write(" / ");
            } } catch( NoSuchElementException e) {};
            return( ReturnCodes.OK );
        }
    }

}
