/*Copyright 2011 indiandragon

 This file is part of j2me2AB.

 J2meAB 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.

 j2me2AB 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 j2me2AB.  If not, see <http://www.gnu.org/licenses/>.
 */
package javax.microedition.lcdui;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.net.Uri;

public class Image {

    private Bitmap bitmap;
    public static final int TRANS_NONE = 0;
    public static final int TRANS_ROT90 = 5;
    public static final int TRANS_ROT180 = 3;
    public static final int TRANS_ROT270 = 6;

    private static Image rotate(Image im, int mode) throws IOException {

        if (im == null) {
            return null;
        }
        if (mode == Image.TRANS_ROT90) {
            mode = 90;
        } else if (mode == Image.TRANS_ROT180) {
            mode = 180;
        } else if (mode == Image.TRANS_ROT270) {
            mode = 270;
        } else {
            return im;
        }

        Image imm = Image.createImage(im.getWidth(), im.getHeight());

        imm.setBitmap(rotate(im.getBitmap(), mode));
        return imm;
    }

    private static Bitmap rotate(Bitmap b, int degrees) {
        if (degrees != 0 && b != null) {
            Matrix m = new Matrix();

            m.setRotate(degrees, (float) b.getWidth() / 2, (float) b.getHeight() / 2);
            try {
                Bitmap b2 = Bitmap.createBitmap(
                        b, 0, 0, b.getWidth(), b.getHeight(), m, true);
                if (b != b2) {
                    b.recycle();
                    b = b2;
                }
            } catch (OutOfMemoryError ex) {
                throw ex;
            }
        }
        return b;
    }

    public static Image createImage(byte[] imageData, int imageOffset,
            int imageLength) throws IOException {
        Bitmap bitmap = BitmapFactory.decodeByteArray(imageData, imageOffset,
                imageLength);
        return new Image(bitmap);
    }

    public static Image createImage(Image image, int x, int y, int width,
            int height, int transform) throws IOException {
//        return new Image(image.bitmap);

        return rotate(image, transform);
    }

    public static Image createImage(String path) throws IOException {
        return new Image(loadBitmap(path));
    }

    public static Image createImage(InputStream stream) throws IOException {
        Bitmap bitmap = BitmapFactory.decodeStream(stream);
        return new Image(bitmap);
    }

    public static Image createImage(int width, int height) throws IOException {
        Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
        return new Image(bitmap);
    }

    public static Image createImage(int resourceid) throws IOException {
        Bitmap bitmap;
        bitmap = BitmapFactory.decodeResource(
                UIGlobal.APP_CONTEXT.getResources(), resourceid);
        Image img = new Image(bitmap);
        System.out.println("test -->" + img.getBitmap().getHeight());
        System.out.println("View ---> " + img.getBitmap());

        return img;

    }

    public static Image createRGBImage(int[] rgb, int width, int height,
            boolean processAlpha) throws IOException {
        return new Image(Bitmap.createBitmap(rgb, width, height,
                Bitmap.Config.ARGB_8888));
    }

    public void getRGB(int[] rgb, int offset, int scanlength, int x, int y,
            int width, int height) {
        this.bitmap.getPixels(rgb, offset, scanlength, x, y, width, height);
    }

    public void resize(int newWidth, int newHeight) throws IOException {

        final int width = getWidth();

        final int height = getHeight();
        if (width == newWidth && height == newHeight) {
            return;
        }

        int[] pixels = new int[width * height];

        getRGB(pixels, 0, width, 0, 0, width, height);

        Bitmap bm = getBitmap();

        setBitmap(Bitmap.createScaledBitmap(bm, newWidth, newHeight, true));

    }
    static Image im;

    protected Image(Bitmap bitmap) {
        im = this;
        this.bitmap = bitmap;
    }

    public Bitmap getBitmap() {
        return this.bitmap;
    }

    public void setBitmap(Bitmap bitmap) {
        this.bitmap = bitmap;
    }

    public int getWidth() {
        return this.bitmap.getWidth();
    }

    public int getHeight() {
        return this.bitmap.getHeight();
    }

    private static Bitmap loadBitmap(String url) {
        Bitmap bm = null;
        InputStream is = null;
        BufferedInputStream bis = null;

        if (url.startsWith("http://")) {
            try {
                URLConnection conn = new URL(url).openConnection();
                conn.connect();
                is = conn.getInputStream();
                bis = new BufferedInputStream(is, 8192);
                bm = BitmapFactory.decodeStream(bis);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (bis != null) {
                    try {
                        bis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (is != null) {
                    try {
                        is.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        } else {
            try {

                is = im.getClass().getResourceAsStream(url);
                bis = new BufferedInputStream(is, 8192);
                bm = BitmapFactory.decodeStream(bis);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (bis != null) {
                    try {
                        bis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (is != null) {
                    try {
                        is.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        return bm;
    }
}
