/* BlinkenSim
 * version 0.1 date 2006-07-28
 * Copyright (C) 2004-2006: Stefan Schuermans <stefan@blinkenarea.org>
 * Copyleft: GNU public license - http://www.gnu.org/copyleft/gpl.html
 * a blinkenarea.org project
 */

package org.blinkenarea.BlinkenSim;

import java.lang.*;
import java.applet.*;
import java.awt.*;
import java.net.*;
import org.blinkenarea.BlinkenLib.*;
import org.blinkenarea.BlinkenSim.*;

public class BlinkenSim extends Applet implements FrameReceiver
{ 
  private int pixelX = 0;
  private int pixelY = 0;
  private int colors = 0;
  private int backgroundX = 0;
  private int backgroundY = 0;
  private int windowX = 0;
  private int windowY = 0;
  private int startX = 0;
  private int startY = 0;
  private int distanceX = 0;
  private int distanceY = 0;
  private Image backgroundImg;
  private Image overlayImg;
  private BlinkenFrame frame;
  private DataSource dataSrc;

  public void init( )
  {
    String param;
    URL url;
    String host;
    int port = 0;
	int movieCnt = 0;
	String [] movies;

    param = getParameter( "pixelX" );
    if( param != null )
      pixelX = Integer.parseInt( param );
    if( pixelX < 1 || pixelX > 100 )
      pixelX = 26;
    param = getParameter( "pixelY" );
    if( param != null )
      pixelY = Integer.parseInt( param );
    if( pixelY < 1 || pixelY > 100 )
      pixelY = 20;
    param = getParameter( "colors" );
    if( param != null )
      colors = Integer.parseInt( param );
    if( colors < 1 || colors > 256 )
      colors = 8;
    param = getParameter( "backgroundX" );
    if( param != null )
      backgroundX = Integer.parseInt( param );
    if( backgroundX < 1 || backgroundX > 2048 )
      backgroundX = 144;
    param = getParameter( "backgroundY" );
    if( param != null )
      backgroundY = Integer.parseInt( param );
    if( backgroundY < 1 || backgroundY > 2048 )
      backgroundY = 108;
    param = getParameter( "windowX" );
    if( param != null )
      windowX = Integer.parseInt( param );
    if( windowX < 1 || windowX > 256 )
      windowX = 2;
    param = getParameter( "windowY" );
    if( param != null )
      windowY = Integer.parseInt( param );
    if( windowY < 1 || windowY > 256 )
      windowY = 4;
    param = getParameter( "startX" );
    if( param != null )
      startX = Integer.parseInt( param );
    if( startX < 1 || startX > 2048 )
      startX = 21;
    param = getParameter( "startY" );
    if( param != null )
      startY = Integer.parseInt( param );
    if( startY < 1 || startY > 2048 )
      startY = 5;
    param = getParameter( "distanceX" );
    if( param != null )
      distanceX = Integer.parseInt( param );
    if( distanceX < 1 || distanceX > 512 )
      distanceX = 2;
    param = getParameter( "distanceY" );
    if( param != null )
      distanceY = Integer.parseInt( param );
    if( distanceY < 1 || distanceY > 512 )
      distanceY = 24;

    url = getDocumentBase( );
    if( url != null )
    {
      backgroundImg = getImage( url, getParameter( "background" ) );
      overlayImg = getImage( url, getParameter( "overlay" ) );
    }
    else
    {
      try
      {
        url = new URL( "file:" + getParameter( "background" ) );
        backgroundImg = getImage( url );
      }
      catch( MalformedURLException e )
      {
        backgroundImg = createImage( -1, -1 );
      }
      try
      {
        url = new URL( "file:" + getParameter( "overlay" ) );
        overlayImg = getImage( url );
      }
      catch( MalformedURLException e )
      {
        overlayImg = createImage( -1, -1 );
      }
    }

	String mode = getParameter( "mode" );

	if( mode.equals( "stream" ) )
		dataSrc = new DynMcufClient( this );
	else if( mode.equals( "movie" ) )
		dataSrc = new MoviePlayer( this );
	else if( mode.equals( "playlist" ) )
		dataSrc = new PlaylistPlayer( this );
	else
	    dataSrc = null;

    frame = new BlinkenFrame( pixelY, pixelX, 1, 1, 1 );

    if( dataSrc != null )
      dataSrc.start( this );
  }

  public void newFrame( BlinkenFrame frame )
  {
    this.frame = frame;
    repaint( );
  }

  public void paint( Graphics g )
  {
    frame.resize( pixelY, pixelX, 1, colors - 1 );
    
    Image img = createImage( backgroundX, backgroundY );
    Graphics gBuf = img.getGraphics( );

    gBuf.drawImage( backgroundImg, 0, 0, this );

    for( int y = 0, yy = startY; y < pixelY; y++, yy += distanceY )
    {
      for( int x = 0, xx = startX; x < pixelX; x++, xx += distanceX )
      {
        int value = frame.getPixel( y, x, 0 );
        gBuf.drawImage( overlayImg, xx, yy, xx + windowX, yy + windowY,
                        value * windowX, 0, value * windowX + windowX, windowY, this );
      }
    }

    g.drawImage( img, 0, 0, this );
  }

  public void update( Graphics g )
  {
    paint( g );
  }
}
