package client;

import java.awt.*;
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.net.*;
import java.util.Vector;
import javax.media.*;
import javax.media.control.*;
import javax.media.format.*;
import javax.media.protocol.*;
import javax.swing.*;

public class AudioSender extends JFrame implements ControllerListener, Runnable
{
	Player player = null;
	Processor p = null;
	DataSource audioSource = null;
	MediaLocator audioLoc = null;
	DataSink filewriter = null;
	ATransmit at = null;
	JButton stopRec;
	String ip;
	String port;
	boolean isFile = false;
	
	public AudioSender( String locator )
	{
		audioLoc = new MediaLocator( "file:///" + locator );
		isFile = true;
		
//		captureVideo();
//		playFile( "file:///C:/XD.avi" );
//		sendVideo();
		
		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 AudioSender()
	{
		isFile = false;
//		captureVideo();
//		playFile( "file:///C:/XD.avi" );
//		sendVideo();
		
		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 boolean initialize()
	{
		AudioFormat format;
		Vector devices;
		format = new AudioFormat( "linear", 44100, 8, 2 );
		devices = CaptureDeviceManager.getDeviceList( format );
		CaptureDeviceInfo di = null;
		if( devices.size()>0 )
		{
			di = ( CaptureDeviceInfo )devices.elementAt(0);
		}
		else
		{
			System.exit(-1);
		}
		audioLoc = di.getLocator();
		
//		AVTransmit3 at = new AVTransmit3(m, ip, port, fmt);
//		at.start();
//		audioLoc = new MediaLocator( "file:///C:/aaa1.wav");
		
		if( audioLoc == null )
		{
			JOptionPane.showMessageDialog( null, "找不到麥克風！");
			return false;
		}
		return true;
		

//		try {
//			DataSource ds = Manager.createDataSource( audioLoc );
//			audioSource = Manager.createCloneableDataSource( ds );
//		} catch (NoDataSourceException e) {
////			e.printStackTrace();
//		} catch (IOException e) {
////			e.printStackTrace();
//		}
//		if( audioSource != null )
//			return true;
//		else
//		{
//			JOptionPane.showMessageDialog( null, "找不到麥克風！");
//			return 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 );

		validate();
		stopRec = new JButton( "stopRec" );
		stopRec.addActionListener(
				new ActionListener(){
					public void actionPerformed( ActionEvent e ){
						if( p != null )
						{
							p.stop();
							p.close();
						}
						if( filewriter != null )
						{
							try {
								filewriter.stop();
							} catch (IOException e1) {
								e1.printStackTrace();
							}
							filewriter.close();
						}
					}
		});
//		c.add( stopRec, BorderLayout.EAST );
		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 {
//			videoSource = Manager.createCloneableDataSource(
//					Manager.createDataSource( videoLoc ) );
//			videoSource = Manager.createDataSource( videoLoc );
//			player = Manager.createRealizedPlayer( audioSource );

			System.out.println( "Successfully construct" );
//			player.start();
			
			at = new ATransmit( audioLoc, ip, port, null, isFile );
			at.start();
//			sendVideo();
//			recordVideo();
			
//		} catch (NoPlayerException e) {
//			e.printStackTrace();
//		} catch (IOException e) {
//			e.printStackTrace();
//		} catch (NoDataSourceException e) {
//			e.printStackTrace();
//		} catch (CannotRealizeException e) {
//			e.printStackTrace();
//		}
	}
}

