package org.labraaten.screensaver.earthquake;

import java.io.InputStream;
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import javax.microedition.io.Connector;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;

/**
 *
 */
public class EarthQuakeCanvas extends GameCanvas
{
    public Date imageTime;
    public Image image; 
    public final long INTERVAL = 1000 * 60 * 60 * 4;  // 4 hours
    public Timer timer;
    public TimerTask timerTask;
    private int loadCounter;
            
    
    public EarthQuakeCanvas()
    {
        super(false);
        timer = new Timer();
        loadCounter = 0;
    }
  
    public void start()
    {
        timerTask = new TimerTask() {

            public void run() {
                try
                {
                    loadImage();
                } catch (Exception e)
                {
                }
                
                drawImage();
            }
        };
        
        timer.schedule(timerTask, 0, INTERVAL);
        
    }
    
    public void stop()
    {
        if (timerTask != null)
            timerTask.cancel();
    }
    
    private void loadImage() throws java.io.IOException
    {
        // get current time
        Date currentTime = Calendar.getInstance().getTime();
        if (imageTime == null || 
            currentTime.getTime() > imageTime.getTime() + INTERVAL - 60000) // add some slack to avoid 'race condition' with our timer
        {
            loadCounter++;
            
            // reload image
            InputStream imageStream = Connector.openInputStream(
                    "http://www.norsar.no/NDC/bulletins/lastweek_fp.jpg");
            
            imageTime = currentTime;
            image  = Image.createImage(imageStream);
            
            // scale image
            double scale = getScale(image);
            int newWidth = (int) (image.getWidth() * scale);
            int newHeight = (int) (image.getWidth() * scale);
            image = scaleImage(image, newWidth, newHeight);
        }
    }
    
    private double getScale(Image image)
    {
            return Math.max((double) getWidth() / image.getWidth(), (double) getHeight() / image.getHeight());
    }
    
    private void drawImage()
    {
        if (image != null)
        {
            Graphics g = this.getGraphics();
            g.setColor(0xff, 0xff, 0xff);
            g.fillRect(0, 0, getWidth(), getHeight());
            int x = (getWidth() - image.getWidth()) / 2;
            int y = (getHeight() - image.getHeight()) / 2;
            g.drawImage(image, x, y, Graphics.TOP | Graphics.LEFT);
            
            g.setColor(0x00, 0x00, 0x00);
            g.drawString("" + loadCounter, 0, 0, Graphics.TOP | Graphics.LEFT);
            
            flushGraphics();
        }
    }
    
    public static Image scaleImage(Image src, int dstW, int dstH) 
    {
        int srcW = src.getWidth();
        int srcH = src.getHeight();

        Image tmp = Image.createImage(dstW, srcH);
        Graphics g = tmp.getGraphics();

        int delta = (srcW << 16) / dstW;
        int pos = delta / 2;

        for (int x = 0; x < dstW; x++)
        {
            g.setClip(x, 0, 1, srcH);
            g.drawImage(src, x - (pos >> 16), 0, Graphics.LEFT | Graphics.TOP);
            pos += delta;
        }

        Image dst = Image.createImage(dstW, dstH);
        g = dst.getGraphics();

        delta = (srcH << 16) / dstH;
        pos = delta / 2;

        for (int y = 0; y < dstH; y++)
        {
            g.setClip(0, y, dstW, 1);
            g.drawImage(tmp, 0, y - (pos >> 16), Graphics.LEFT | Graphics.TOP);
            pos += delta;
        }

        return dst;
}    

    

    
    
    


}
