package client;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.util.Vector;

import javax.media.CannotRealizeException;
import javax.media.CaptureDeviceInfo;
import javax.media.CaptureDeviceManager;
import javax.media.ControllerEvent;
import javax.media.DataSink;
import javax.media.Format;
import javax.media.Manager;
import javax.media.MediaLocator;
import javax.media.NoDataSinkException;
import javax.media.NoDataSourceException;
import javax.media.NoPlayerException;
import javax.media.NoProcessorException;
import javax.media.NotRealizedError;
import javax.media.Player;
import javax.media.Processor;
import javax.media.RealizeCompleteEvent;
import javax.media.control.FormatControl;
import javax.media.control.TrackControl;
import javax.media.format.VideoFormat;
import javax.media.protocol.ContentDescriptor;
import javax.media.protocol.DataSource;
import javax.media.protocol.FileTypeDescriptor;
import javax.media.protocol.SourceCloneable;
import javax.swing.*;

public class MovieSender extends JFrame
{
	Player player = null;
	Processor p = null;
	DataSource videoSource = null;
	MediaLocator videoLoc = null;
	VTransmit at = null;
	String ip;
	String port;
	String path;
	
	public MovieSender( String IP, String port )
	{
		super( "¼v¤ù" );
		ip = IP;
		this.port = port;
		
		addWindowListener(
				new WindowAdapter(){
						public void windowClosing( WindowEvent e ){
							if( p != null )
								p.close();
//							if( videoSource != null )
//								videoSource.disconnect();
							if( player != null )
								player.close();
							at.stoptransmit();
							setVisible( false );
						}
				}
		);
	}
	
	public MovieSender( String filepath )
	{
		super( "¼v¤ù" );
		path = filepath;
		
		addWindowListener(
				new WindowAdapter(){
						public void windowClosing( WindowEvent e ){
							if( p != null )
								p.close();
//							if( videoSource != null )
//								videoSource.disconnect();
							if( player != null )
								player.close();
							at.stoptransmit();
							setVisible( false );
						}
				}
		);
	}
	
	public void startPlayer()
	{
		Container c = getContentPane();
		c.setLayout( new BorderLayout() );
		Component comp;
		
		player.start();
//		p.realize();
//		p.start();
		
		if(( comp = player.getVisualComponent()) != null )
			c.add( comp, BorderLayout.CENTER );
		if(( comp = player.getControlPanelComponent()) != null )
			c.add( comp, BorderLayout.SOUTH );
//		if(( comp = p.getVisualComponent()) != null )
//			c.add( comp, BorderLayout.CENTER );
//		if(( comp = p.getControlPanelComponent()) != null )
//			c.add( comp, BorderLayout.SOUTH );
		validate();
		
		pack();
		setVisible( true );
	}
	
	public void stop()
	{
		player.stop();
		player.deallocate();
	}
	
	public void destroy()
	{
		player.close();
	}
	
	public synchronized void controllerUpdate( ControllerEvent event )
	{
		Container c = getContentPane();
		if( event instanceof RealizeCompleteEvent )
		{
			Component comp;
			if(( comp = player.getVisualComponent()) != null )
				c.add( "center", comp );
			if(( comp = player.getControlPanelComponent()) != null )
				c.add( "South", comp );
			validate();
		}	
	}
	
	public void set( String ip, String port )
	{
		this.ip = ip;
		this.port = port;
	}
//	public void captureVideo( String source )
//	public void captureVideo()
	public void run()
	{		
		try {
			videoLoc = new MediaLocator( "file:///" + path );
			DataSource ds = Manager.createDataSource( videoLoc );
			videoSource = Manager.createCloneableDataSource( ds );
			player = Manager.createRealizedPlayer( videoSource );
//			System.out.println( "datasource" );
//			p = Manager.createProcessor( videoSource );
//			p.configure();
//			p.setContentDescriptor( new ContentDescriptor(ContentDescriptor.RAW));
			System.out.println( "Successfully construct" );
			startPlayer();
			
			at = new VTransmit( videoSource, ip, port, null );
			at.start();
			
		} catch ( NoPlayerException e ){
			e.printStackTrace();
		} catch ( IOException e ){
			e.printStackTrace();
//		} catch ( NoDataSourceException e ){
//			e.printStackTrace();
		} catch ( CannotRealizeException e ){
			e.printStackTrace();
		} catch ( NoDataSourceException e ){
			e.printStackTrace();
		}
	}
}
