/*
 * DownloadForm.java
 *
 * Created on October 16, 2003, 11:02 PM
 */

package org.tienshiao.BookCheck;

import org.tienshiao.XMLLite.*;
import java.io.*;
import java.util.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;

/**
 *
 * @author  tma
 * @version
 */
public class DownloadForm extends Canvas implements Runnable {
    private BookCheck bookCheck;
    private String isbn;
    private Thread runner;
    private Timer timer;
    private XMLElement response;
    private Image busy;

    private int fullWidth, fullHeight, halfWidth, halfHeight, counter = 0;
    private String status;
    
    public DownloadForm(String isbn, BookCheck bc) {
        this.bookCheck = bc;
        this.isbn = isbn;
        
        try {
            busy = Image.createImage("/org/tienshiao/BookCheck/BusyCursor.png");
        } catch (IOException ioe) {
            // TODO something with the exception
        }
        runner = new Thread(this);
        runner.start();
                    
        fullWidth = getWidth();
        fullHeight = getHeight();
        
        halfWidth = fullWidth / 2;
        halfHeight = fullHeight / 2;
        
    }
    
    protected void paint(Graphics g) {
        //TODO: Replace animation
    
        // Clear the whole screen.
        g.setColor(255, 255, 255);
        g.fillRect(0, 0, fullWidth, fullHeight);
    
        // Now draw the pinwheel.
        g.drawImage(busy, halfWidth, halfHeight, Graphics.HCENTER | Graphics.VCENTER);
        g.setColor(0, 0, 0);
        switch (counter % 8) {
            case 0:
                g.drawLine(halfWidth, halfHeight, halfWidth,  halfHeight - 4);
                break;
            case 1:
                g.drawLine(halfWidth, halfHeight, halfWidth + 2,  halfHeight - 3);                
                break;
            case 2:
                g.drawLine(halfWidth, halfHeight, halfWidth + 4,  halfHeight);
                break;
            case 3:
                g.drawLine(halfWidth, halfHeight, halfWidth + 2,  halfHeight + 3);
                break;
            case 4:
                g.drawLine(halfWidth, halfHeight, halfWidth,  halfHeight + 4);
                break;
            case 5:
                g.drawLine(halfWidth, halfHeight, halfWidth - 2,  halfHeight + 3);
                break;
            case 6:
                g.drawLine(halfWidth, halfHeight, halfWidth - 4,  halfHeight);
                break;
            case 7:
                g.drawLine(halfWidth, halfHeight, halfWidth - 2,  halfHeight - 3);
                break;
        }
        switch ((counter / 8) % 8) {
            case 0:
                g.drawLine(halfWidth, halfHeight, halfWidth,  halfHeight - 3);
                break;
            case 1:
                g.drawLine(halfWidth, halfHeight, halfWidth + 1,  halfHeight - 2);                
                break;
            case 2:
                g.drawLine(halfWidth, halfHeight, halfWidth + 3,  halfHeight);
                break;
            case 3:
                g.drawLine(halfWidth, halfHeight, halfWidth + 1,  halfHeight + 2);
                break;
            case 4:
                g.drawLine(halfWidth, halfHeight, halfWidth,  halfHeight + 3);
                break;
            case 5:
                g.drawLine(halfWidth, halfHeight, halfWidth - 1,  halfHeight + 3);
                break;
            case 6:
                g.drawLine(halfWidth, halfHeight, halfWidth - 3,  halfHeight);
                break;
            case 7:
                g.drawLine(halfWidth, halfHeight, halfWidth - 1,  halfHeight - 2);
                break;
        }        
    
        // Draw the message, if there is a message.
        if (status != null) {
            g.drawString(status, halfWidth, fullHeight, Graphics.BOTTOM | Graphics.HCENTER);
        }
    }
    
    protected void showNotify() {
        // Create a Timer to update the display.
        TimerTask task = new TimerTask() {
            public void run() {
                counter++;
                repaint();
            }
        };
        timer = new Timer();
        timer.schedule(task, 0, 150);        
    }
    
    protected void hideNotify() {
        timer.cancel();
    }
    
    public void run() {
        HttpConnection http = null;
        InputStream is = null;
        try {
            String url = "http://xml.amazon.com/onca/xml3?t=webservices-20&dev-t=D4OX7EHXJT1BT&AsinSearch=" +
                         isbn + "&type=heavy&f=xml";
            status = "Connecting...";
            http = (HttpConnection)Connector.open(url);
            XMLElement test = new XMLElement();
            InputStreamReader isr = new InputStreamReader(((InputConnection)http).openInputStream());
            status = "Reading...";            
            test.parseFromReader(isr);
            bookCheck.displayResponse(test);
        } catch (XMLParserException xpe) {
        } catch (IOException ioe) {
        } finally {
            try {
                if (is != null)
                    is.close();
                if (http != null)
                    ((Connection)http).close();
            } catch (IOException ioe) {
            }
        }
    }
}
