// (c) MIT 2003.  All rights reserved.

////////////////////////////////////////////////////////////////////////////////
//
// FILE:	Console.java
//
// AUTHOR:	Manoj D. Muzumdar
//		archon@ai.mit.edu
//
// DATE:	July, 1998
//
// COPYRIGHT:	Massachusetts Institute of Technology
//		All rights reserved.
//
// PURPOSE:     Class for printing information
//
////////////////////////////////////////////////////////////////////////////////

package rec.util;

import java.io.*;

public final class Console
{
  public static void enableOutput()
  {
    s_ShouldOutput = true;
  }

  public static void disableOutput()
  {
    s_ShouldOutput = false;
  }

  public static boolean isOutputEnabled()
  {
    return( s_ShouldOutput );
  }

  /////////////////////////////////////////////////////////////////////////////

  public static void setIndent( int i )
  {
    s_Indent = i;
  }

  public static void indent( int i )
  {
    s_Indent += i;
  }

  public static void indent()
  {
    indent( 4 );
  }

  public static void outdent()
  {
    outdent( 4 );
  }

  public static void outdent( int i )
  {
    s_Indent -= i;
    if ( s_Indent < 0 )
      s_Indent = 0;
  }

  public static void resetIndent( )
  {
    s_Indent = 0;
  }

  /////////////////////////////////////////////////////////////////////////////

  public static void enablePrettyPrinting()
  {
    s_IsPretty = true;
  }

  public static void disablePrettyPrinting()
  {
    s_IsPretty = false;
  }

  public static boolean isPrettyPrintingEnabled()
  {
    return( s_IsPretty );
  }

  //////////////////////////////////////////////////////////////////////////////

  public static void moWriteln( String msg ) 
  {
    System.err.println( "****MOMSG: " + msg );
  }
    
  /**
   * Writes a message.
   */
  public static void write( String msg )
  {
    if( !s_ShouldOutput )
      return;

    if( s_IsPretty ) {
      printText( makePretty( msg ) );
      return;
    }

    printText( msg );
  }

  public static void writeln()
  {
    if( !s_ShouldOutput )
      return;

    printlnText("");
  }

  /**
   * Writes a message followed by a carriage return.
   */
  public static void writeln( String msg )
  {
    if( !s_ShouldOutput )
      return;

    String h = "";
    for ( int i = 0; i < s_Indent; i++ )
      h += " ";

    if( s_IsPretty ) {
      printlnText( h + makePretty( msg ) );
      return;
    }

    printlnText( msg );
  }

  //////////////////////////////////////////////////////////////////////////////

  /**
   * Makes a message pretty.
   */
  public static String makePretty( String msg )
  {
    String ret = new String( msg );
    ret = ret.trim();

    int len = ret.length();

    if( len == 0 )
      return( "" );

    char f = ret.charAt( 0 );
    if( Character.isLetter( f ) && Character.isLowerCase( f ) )
      f = Character.toUpperCase( f );

    if( ret.charAt( ret.length() - 1 ) == '.' ) {

      if( len == 1 )
	return( (new Character( f )).toString() );

      return( f + ret.substring( 1 ) );
    }

    return( f + ret.substring( 1 ) + "." );
  }

  private static void printText( String text ) 
  {
    s_Output.print( text );
  }

  private static void printlnText( String text )
  {
    s_Output.println( text );
    //    s_Output.flush();
  }

  

  /** Strip off all the package.names.before.the.Classname. **/
  public static String stripPackaging( Object obj ) 
  {
    return stripPackaging( obj.toString() );
  }
  
  /** Strip off all the package.names.before.the.Classname. **/
  public static String stripPackaging( String classname ) 
  {
    int index = classname.lastIndexOf( "." );
    if( index > 0 )
      index++;
    return classname.substring( index );
  }
  


  private static PrintStream s_Output = System.err;
//    private static PrintWriter s_Output;
//    static{
//      try {
//        FileWriter fw = new FileWriter( "z:\\hal\\source\\rec\\transcript.txt" );
//        s_Output = new PrintWriter( fw );
//      }
//      catch ( IOException e ) {
//        e.printStackTrace();
//        System.err.println( "Error opening log file..." );
//      }
//    }
  

  //////////////////////////////////////////////////////////////////////////////

  private static boolean s_ShouldOutput = false;
  private static boolean s_IsPretty = true;
  private static int s_Indent = 0;
}

////////////////////////////////////////////////////////////////////////////////
