/* ======================================================================
   Parts Copyright 2006 University of Leeds, Oxford University, University of the Highlands and Islands.

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

====================================================================== */

package org.bodington.applet.components;

import org.bodington.applet.components.*;
import org.bodington.applet.data.*;

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


public class TransferLocalFileDialog extends org.bodington.applet.components.TransferDataDialog
implements Runnable
{

    private TransferLocalFileItem[] items;
    private int current;
    
    Thread worker=null;
    
    public TransferLocalFileDialog( JDesktopPane desktop )
	{
	super( desktop, "Publishing File", "Preparing to Publish File", true );
	}
    
    public synchronized boolean isInProgress()
	{
	return worker != null;
	}
	
    public synchronized void transfer( TransferLocalFileItem[] items )
	throws IOException
	{
	this.items = items;
	current=0;
	
	if ( isInProgress() )
	    throw new IOException( "Transfer already in progress." );
	
	worker = new Thread( this );
	setBusy( true );
	worker.start();
	
	setVisible( true );
	}
    
    public synchronized void transfer( File file, String name, URL url )
	throws IOException
	{
	TransferLocalFileItem[] items = new TransferLocalFileItem[1];
        items[0] = new TransferLocalFileItem();
	items[0].file = file;
	items[0].name = name;
	items[0].url  = url;
	transfer( items );
	}
    
public void run()
    {
    try
	{
	for ( current=0; current<items.length; current++ )
	    {
	    if ( cancelled )
		{
		System.err.println( "Error message." );
		panel.setErrorMessage( "File transfer stopped." );
		setMessage( "Transfer Stopped." );
		break;
		}
	    
	    BufferedInputStream fin = new BufferedInputStream( new FileInputStream( items[current].file ) );

	    startMultiPartPost( items[current].url );
	    OutputStream out = getPartOutputStream( items[current].getName(), items[current].file.length() );

	    int b;
	    while ( (b=fin.read()) >= 0 )
		{
		out.write( b );
		}

	    fin.close();
	    out.close();

	    InputStreamReader inr = new InputStreamReader( getResponse() );
	    BufferedReader reader = new BufferedReader( inr );

	    String confirm = reader.readLine();

	    System.err.println( "Confirmation: " + confirm );

	    String line = null;
	    StringBuffer message = null;
	    if ( !confirm.equalsIgnoreCase( "OK" ) )
		{
		message = new StringBuffer();
		line = reader.readLine();
		message.append( line );
		/*
		while ( (line = reader.readLine()) != null )
		    {
		    message.append( line );
		    message.append( "\n" );
		    }
		 */
		}


	    if ( message != null )
		{
		System.err.println( "Error message." );
		panel.setErrorMessage( message.toString() );
		setMessage( "Transfer Failed" );
		break;
		}
	    else
		{
		setMessage( "Transfer Complete" );
		setProgress( 100 );
		}

	    inr.close();
	    }
        //if ( listener != null )
	//    listener.progressCompleted( this, false );
	}
    catch ( Exception ex )
	{
        ex.printStackTrace();
        JOptionPane.showMessageDialog( null, ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE );
	//if ( listener != null )
	//    listener.progressCompleted( this, true );
	}
    finally
        {
	worker = null;
	setBusy( false );
	panel.setCloseEnabled( true );
	panel.setCancelEnabled( false );
        }
    }
    
}
