/*
 *  WMAConsole
 *  Copyright (C) 2005 John Montgomery <john@sensatus.com>
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

import javax.microedition.io.*;
import javax.wireless.messaging.*;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;

public class WMAConsole implements ActionListener {
	private JTextField fromNumber = new JTextField( "+5550000" );
	private JTextField toNumber   = new JTextField( "+555555" );
	private JTextField toPort     = new JTextField( "12345" );
	
	private JTextField datagramPort = new JTextField( "54321" );
	private JTextField datagramHost = new JTextField( "localhost" );
	
	private JTextArea messageArea = new JTextArea( 10, 16 );
	private JCheckBox binaryCheck = new JCheckBox( "Binary (use hex values)" );
	
	private JButton sendButton = new JButton( "Send" );
	
	private JFrame frame = null;
	
	public WMAConsole() {
		frame = new JFrame( "WMAConsole" );
		frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
		
		JPanel sidePanel = new JPanel( new BorderLayout() );
		
		JPanel paramsPanel = new JPanel( new GridLayout( 3, 2, 2, 2 ) );
		paramsPanel.setBorder( BorderFactory.createTitledBorder( "SMS Parameters" ) );
		String[] labels = { "From Number", "To Number", "To Port" };
		JTextField[] params = { fromNumber, toNumber, toPort };
		for ( int i = 0; i < labels.length; i++ ) {
			paramsPanel.add( new JLabel( labels[ i ] ) );
			paramsPanel.add( params[ i ] );
		}
		
		sidePanel.add( paramsPanel, BorderLayout.NORTH );
		
		paramsPanel = new JPanel( new GridLayout( 2, 2 ) );
		paramsPanel.setBorder( BorderFactory.createTitledBorder( "Emulation Parameters" ) );
		paramsPanel.add( new JLabel( "Host" ) );
		paramsPanel.add( datagramHost );
		paramsPanel.add( new JLabel( "Port" ) );
		paramsPanel.add( datagramPort );
		
		sidePanel.add( paramsPanel, BorderLayout.SOUTH );
		
		frame.getContentPane().add( sidePanel, BorderLayout.WEST );
		
		JPanel messagePanel = new JPanel( new BorderLayout() );
		messagePanel.setBorder( BorderFactory.createTitledBorder( "Message" ) );
		messagePanel.add( binaryCheck, BorderLayout.NORTH );
		messagePanel.add( messageArea, BorderLayout.CENTER );
		frame.getContentPane().add( messagePanel, BorderLayout.CENTER );
		
		JPanel buttonPanel = new JPanel( new BorderLayout() );
		buttonPanel.add( sendButton, BorderLayout.EAST );
		frame.getContentPane().add( buttonPanel, BorderLayout.SOUTH );
		frame.getRootPane().setDefaultButton( sendButton );
		
		sendButton.addActionListener( this );
	}
	
	public void start() {
		frame.pack();
		frame.setVisible( true );
	}
	
	public void actionPerformed( ActionEvent ae ) {
		if ( ae.getSource() == sendButton ) {
			try {
				doSend();
			}
			catch( Exception e ) {
				throw new RuntimeException( e );
			}
		}
	}
	
	private byte[] dehex( String hex ) throws IOException {
		ByteArrayOutputStream bytesOut = new ByteArrayOutputStream( hex.length()/2 );
		for ( int i = 0; i < hex.length(); i+=2 ) {
			String hexPair = hex.substring( i, i+2 );
			int value = Integer.parseInt( hexPair, 16 );
			bytesOut.write( value );
		}
		return bytesOut.toByteArray();
	}
	
	private BinaryMessage createBinaryMessage( MessageConnection connection ) throws Exception {
		BinaryMessage msg = (BinaryMessage)connection.newMessage( MessageConnection.BINARY_MESSAGE );
		String text = messageArea.getText();
		byte[] bytes = dehex( text );
		msg.setPayloadData( bytes );
		return msg;
	}
	
	private TextMessage createTextMessage( MessageConnection connection ) throws Exception {
		TextMessage msg = (TextMessage)connection.newMessage( MessageConnection.TEXT_MESSAGE );
		msg.setPayloadText( messageArea.getText() );
		return msg;
	}
	
	private void doSend() throws Exception {
		System.setProperty( "com.barteio.cldc.sms.SMSNumber", fromNumber.getText() );
		System.setProperty( "com.barteo.cldc.sms.DatagramPortOut", datagramPort.getText() );
		System.setProperty( "com.barteo.cldc.sms.DatagramHost",    datagramHost.getText() );
		String number = toNumber.getText();
		String port   = toPort.getText();
		String combined = number;
		if ( !port.trim().equals("") )
			combined += (":" + port);
		MessageConnection connection = (MessageConnection)Connector.open( "sms://" + combined );
		Message msg = null;
		if ( binaryCheck.isSelected() )
			msg = createBinaryMessage(connection);
		else
			msg = createTextMessage(connection);
		//System.out.println( msg.getAddress() );
		connection.send( msg );
	}
	
	public static void main( String[] args ) {
		new WMAConsole().start();
	}
}