package com.uvigo.mhp.mediaPlayer;

import java.awt.*;
import java.io.*;
import java.net.*;
import javax.media.*;

class MediaPlayerControllerAdapter extends ControllerAdapter
{
    private final MediaPlayer mp;

    public MediaPlayerControllerAdapter(MediaPlayer mp)
    {
        this.mp = mp;
    }

    public void mediaTimeSet(MediaTimeSetEvent e)
    {
	mp.whenTimeIsSet();
    }

    public void transition(TransitionEvent e)
    {
	mp.setState(e.getCurrentState());
    }

    public void prefetchComplete(PrefetchCompleteEvent e)
    {
    }

    public void start(StartEvent e)
    {
    }

    public void endOfMedia(EndOfMediaEvent e)
    {
        if(mp.isPlayBackLoop())
            synchronized(this)
            {
                if(mp.player == null)
                {
                    System.out.println("Unexpected error");
                    System.exit(-1);
                }
                if(mp.player != null)
		    {
			mp.player.setMediaTime(new Time(0L));
			mp.player.start();
		    }
            }
    }

    public void controllerError(ControllerErrorEvent e)
    {
         mp.player = null;
         System.out.println(e.getMessage());
    }

    public void sizeChange(SizeChangeEvent e)
    {
        if(mp.visualComponent != null)
        {
            mp.visualComponent.setSize(e.getWidth(), e.getHeight());
	    mp.setPreferredSize(mp.visualComponent.getPreferredSize());
        }

        mp.invalidate();
        mp.validate();
    }
}
