/* 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.applet.*;
import java.io.*;
import java.net.*;
import java.util.*;
import org.blinkenarea.BlinkenLib.*;

public class MoviePlayer extends DataSource {

  BlinkenMovie [] movies = null;
  int movieNo = 0, frameNo = 0;
  FrameReceiver receiver = null;
  Timer timer = null;
  MoviePlayerTimerTask task = null;

  protected MoviePlayer( ) {
    // e.g. used by PlaylistPlayer
  }

  public MoviePlayer( Applet applet ) {

    String movieName = applet.getParameter( "movie" );
    if( movieName == null || movieName.length( ) <= 0 )
      movieName = "movie.bml";

    String [] movieNames = new String[1];
    movieNames[0] = movieName;
    setUp( applet, movieNames );

  }

  protected void setUp( Applet applet, String [] movieNames ) {

    movies = new BlinkenMovie [movieNames.length];

    for( int idx = 0; idx < movieNames.length; idx++ ) {

      try {
        URL base = applet.getDocumentBase( );
        URL url;
        if( base != null ) {
          url = new URL( base, movieNames[idx] );
        } else {
          url = new URL( "file:" + movieNames[idx] );
        }
        movies[idx] = new BlinkenMovie( 1, 1, 1, 1 );
        BufferedReader reader = new BufferedReader( new InputStreamReader( url.openStream( ) ) );
        if( movieNames[idx].toLowerCase( ).endsWith( ".blm" ) ) {
          movies[idx].loadBlm( reader );
        } else if( movieNames[idx].toLowerCase( ).endsWith( ".bmm" ) ) {
          movies[idx].loadBmm( reader );
        } else if( movieNames[idx].toLowerCase( ).endsWith( ".bml" ) ) {
          movies[idx].loadBml( reader );
        }
      } catch( MalformedURLException e ) {
        movies[idx] = new BlinkenMovie( 1, 1, 1, 1 );
      } catch( IOException e ) {
        movies[idx] = new BlinkenMovie( 1, 1, 1, 1 );
      }

    }

    movieNo = 0;
    frameNo = 0;

  }

  public void start( FrameReceiver recv ) {
    stop( );
    movieNo = 0;
    frameNo = 0;
    receiver = recv;
    nextFrame( );
  }

  public void stop( ) {
    receiver = null;
    timer = null;
    task = null;
  }

  public void nextFrame( ) {

    if( movies == null || movies.length <= 0 || receiver == null )
      return;

    if( movieNo >= movies.length )
      movieNo = 0;
    if( movies[movieNo] == null )
      return;
    BlinkenMovie movie = movies[movieNo];

    if( frameNo >= movie.getFrameCnt( ) )
      frameNo = 0;

    BlinkenFrame frame = movie.getFrame( frameNo );
    receiver.newFrame( frame );

    timer = new Timer( );
    task = new MoviePlayerTimerTask( this );
    timer.schedule( task, frame.getDuration( ) );

    frameNo++;
    if( frameNo >= movie.getFrameCnt( ) ) {
      frameNo = 0;
      movieNo++;
      if( movieNo >= movies.length )
        movieNo++;
    }
  }

} // class MoviePlayer
