/*   $Id$
 *
 *   Copyright (c) 2007 Snempaa & Richie B.
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program 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 General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package sendSms;

import java.io.*;
import javax.microedition.io.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class ShowSplash extends Canvas implements Runnable {
    private BetamaxSMS parent;
    private Display display;
    private Image s;
    private Displayable d;
    private int screenWidth,screenHeight;
    private boolean skip=false;

    public ShowSplash(BetamaxSMS parent, Displayable d) {
        setFullScreenMode(true);
        this.parent=parent;
        this.d=d;
        display=Display.getDisplay(parent);
        screenWidth = getWidth();
        screenHeight = getHeight();
        try {
            if(parent.splash.equals("yes")) {
                s = Image.createImage("/sendSms/splash.gif");
            }
        } catch (Exception e) {
            System.out.println("No image made");
            skip=true;
        }
        Thread t = new Thread(this);
        t.start();
    }
    
    public void run(){            
       try {
           if(!skip)
               Thread.sleep(3000);
        } catch (Exception e) {
        }
        dismiss();
    }
    
    public void paint(Graphics g) {
       g.drawImage(resizeImage(s), screenWidth / 2, screenHeight / 2, Graphics.HCENTER | Graphics.VCENTER);
    }
    
    public void dismiss() {
        if (skip || isShown())
            display.setCurrent(d);
    }
    
    public void keyReleased(int keyCode) { dismiss(); }
    public void pointerReleased(int x, int y) { dismiss(); }
    
    private Image resizeImage(Image src) {
        int srcWidth = src.getWidth();
        int srcHeight = src.getHeight();
        Image tmp = Image.createImage(screenWidth, srcHeight);
        Graphics g = tmp.getGraphics();
        int ratio = (srcWidth << 16) / screenWidth;
        int pos = ratio/2;

        //Horizontal Resize
        for (int x = 0; x < screenWidth; x++) {
          g.setClip(x, 0, 1, srcHeight);
          g.drawImage(src, x - (pos >> 16), 0, Graphics.LEFT | Graphics.TOP);
          pos += ratio;
        }

        Image resizedImage = Image.createImage(screenWidth, screenHeight);
        g = resizedImage.getGraphics();
        ratio = (srcHeight << 16) / screenHeight;
        pos = ratio/2;        

        //Vertical resize
        for (int y = 0; y < screenHeight; y++) {
          g.setClip(0, y, screenWidth, 1);
          g.drawImage(tmp, 0, y - (pos >> 16), Graphics.LEFT | Graphics.TOP);
          pos += ratio;
        }
        return resizedImage;
    }
}
