//EventCallback.java
// adapted from clipinfo.java found at http://demos.real.com/tools/realplayer/clipinfo.htm

import java.awt.*;
import java.applet.Applet;
import RMObserver;
import RAPlayer;
import netscape.javascript.JSException;
import netscape.javascript.JSObject;


//Class: EventCallback
//Purpose: catch prefetch events in order to display TAC and other metadata

public class EventCallback extends Applet implements RMObserver{

        RAPlayer ra;            // realplayer object
        JSObject window;        // Window the applet is embeded in
        JSObject document;      // document of that window (we don't use this here)
        JSObject embeded;       // plugin located inside that window
        //Button author,title,copyright;
        static boolean bye;

        public void init() 
        {
                // here we create a new player object 
                // and tell the RA plugin we would like to receive callbacks.

                JSObject window = JSObject.getWindow(this);
                JSObject embedded = (JSObject) window.getMember("document");

                //get handle to plugin
                ra = (RAPlayer)embedded.getMember("javaPlug1");
                
                //register observer class to fire asynchronous callback events
                ra.AdviseG2(this);      

                //handle URL events with applet instead of browser
                ra.SetAutoGoToURL(false);

                //handle mouse and keyboard events
                //ra.SetWantKeyboardEvents(true);
                //ra.SetWantMouseEvents(true);

                //handle error messages
                //ra.SetWantErrors(true);


                System.out.println("EventCallback applet initialized.");
        }
        public void start() 
        {
                System.out.println("EventCallback applet started.");
                bye = false;
        }

        public void stop() {
                bye = true;
        }

    public void onClipOpened(String shortClipname, String url)
        {
                System.out.println("OnClipOpened sent."+ shortClipname +" "+ url);
    }

    public void onClipClosed()
        {
                        System.out.println("OnClipClosed sent");
     }

     public void onShowStatus(String statustext){
                if(!bye) 
                {
                         System.out.println("OnShowStatus() sent,"+ statustext);
                }
     }

     public void onGoToURL(String url, String target){
            JSObject window = JSObject.getWindow(this);
            Object[] args = {url, target};
            window.call("OnGoToURL", args);
            System.out.println("OnGoToURL sent,"+ url +" "+ target);
     }

        public boolean OnPositionChange(long newpos){
               //System.out.println("OnPositionChange sent, the new position is "+ newpos +"ms");
                return true;
        }

        public boolean OnLengthChange(long newlength){
               //System.out.println("OnLengthChange sent, the new length is "+ newlength +"ms");
                return true;
        }

        public boolean OnVolumeChange(short volume){
                 System.out.println("OnVolumeChange sent, the new volume is "+ volume);
                return true;
        }

        public boolean OnTitleChange(String title){
                 System.out.println("OnTitleChange sent, the new title is "+ title);
                return true;
        }

        public boolean OnAuthorChange(String author){
                 System.out.println("OnAuthorChange sent, the new author is "+ author);
                return true;
        }

        public boolean OnCopyrightChange(String copyright){
                 System.out.println("OnCopyrightChange sent, the new copyright is "+ copyright);
                return true;
        }

        public boolean OnClipSizeChange(long width, long height){
                 System.out.println("OnClipSizeChange sent, height="+ height +" width="+ width);
                return true;
        }

        public boolean OnPlayStateChange(long newPlayState){
                 System.out.println("OnPlayStateChange sent, the new play state is "+ newPlayState);
                return true;
        }

        public boolean OnErrorMessage(short uSeverity, int uRMACode, int uUserCode, String pUserString, String pMoreInfoURL, String pError){
                 System.out.println("OnErrorMessage sent with error: "+ uSeverity+", "+uRMACode+", "+uUserCode+", "+pUserString+", "+pMoreInfoURL+", "+pError);
                return true;
        }

        public boolean OnStatsInfoChange(String text){
                 System.out.println("OnStatsInfoChange sent with info: "+ text);
                return true;
        }

        public boolean OnPosLength(int Position, int Length){
               //  System.out.println("OnPosLength called, Position= "+ Position +" Length= "+ Length);
                return true;
        }

        public boolean OnPresentationOpened(){
                 System.out.println("OnPresentationOpened called");
                return true;
        }

        public boolean OnPresentationClosed(){
                 System.out.println("OnPresentationClosed called");
                return true;
        }
        
        public boolean OnStatisticsChanged(){
                 System.out.println("OnStatisticsChanged called");
                return true;
        }

        public boolean OnPreSeek(int OldTime,   int NewTime){
                 System.out.println("OnPreSeek called, old time = "+ OldTime +", new time = "+ NewTime);
                return true;
        }

        public boolean OnPostSeek(int OldTime, int NewTime){
                 System.out.println("OnPostSeek called, old time = "+ OldTime +", new time = "+ NewTime);
                return true;    
        }
        
        public boolean OnStop(){
                 System.out.println("OnStop called");
                return true;
        }

        public boolean OnBuffering(int Flags, short PercentComplete){
                 System.out.println("OnBuffering called, flags= "+ Flags +" percent complete= "+PercentComplete);
                return true;
        }
                
        public boolean OnContacting(String HostName){
                 System.out.println("OnContacting called, host name= "+HostName);
                return true;
        }

        public boolean OnResized(long lwidth, long lheight){
                 System.out.println("OnResized sent, height= "+ lheight +" width= "+ lwidth);
                return true;
        }

        public boolean OnLButtonDown(int nFlags, int nX, int nY){
                 System.out.println("OnLButtonDown sent, flags= "+ nFlags +" x= "+ nX +" y= "+ nY);
                return true;
        }

        public boolean OnLButtonUp(int nFlags, int nX, int nY){
                 System.out.println("OnLButtonUp sent, flags= "+ nFlags +" x= "+ nX +" y= "+ nY);
                return true;
        }

        public boolean OnLButtonDblClk(int nFlags, int nX, int nY){
                 System.out.println("OnLButtonDblClk sent, flags= "+ nFlags +" x= "+ nX +" y= "+ nY);
                return true;
        }

        public boolean OnRButtonDown(int nFlags, int nX, int nY){
                 System.out.println("OnRButtonDown sent, flags= "+ nFlags +" x= "+ nX +" y= "+ nY);
                return true;
        }

        public boolean OnRButtonUp(int nFlags, int nX, int nY){
                 System.out.println("OnRButtonUp sent, flags= "+ nFlags +" x= "+ nX +" y= "+ nY);
                return true;
        }

        public boolean OnRButtonDblClk(int nFlags, int nX, int nY){
                 System.out.println("OnRButtonDblClk sent, flags= "+ nFlags +" x= "+ nX +" y= "+ nY);
                return true;
        }

        public boolean OnMouseMove(int nFlags, int nX, int nY){
                 System.out.println("OnMouseMove sent, flags= "+ nFlags +" x= "+ nX +" y= "+ nY);
                return true;
        }

        public boolean OnClipOpen(){
                 System.out.println("OnClipOpen sent");
                return true;
        }
        public boolean OnKeyDown(int keycode){
                 System.out.println("OnKeyDown sent with KeyCode equal "+ keycode);
                return true;
        }
        public boolean OnKeyUp(int keycode){
                 System.out.println("OnKeyUp sent with KeyCode equal "+ keycode);
                return true;
        }
        public boolean OnKeyPress(int keycode){
                 System.out.println("OnKeyPress sent with KeyCode equal "+ keycode);
                return true;
        }
        public boolean OnVolumeChange(int newvolume){
                 System.out.println("OnVolumeChange sent with the new volume being "+ newvolume);
                return true;
        }
        public boolean OnMuteChange(boolean muted){
                 System.out.println("OnMuteChange sent, Mute= "+ muted);
                return true;
        }
        public boolean OnPlayStateChange(int fromState, int toState){
                 System.out.println("OnPlayStateChange sent, old state= "+ fromState +" new state= "+ toState);
                return true;
        }

        public boolean OnPreFetchComplete(){
                if(!bye) 
                {
            System.out.println("OnPreFetchComplete sent");
                        JSObject window = JSObject.getWindow(this);
                        Object[] args = { };
                        window.call("OnPreFetchComplete", args);
                }
                return true;
        }

}
