/*
 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
 */
package com.sun.glass.ui;

import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.IntBuffer;
import java.nio.ByteOrder;

/**
 * The object wraps the given raw pixels data.
 */
public abstract class Pixels {
    /**
     * The Format specifies the native byte order of the
     * underlying chunk of image data.
     * The data may be either INTs or BYTEs depending on
     * the constructor used.
     * The format ABCD implies the following byte order:
     * BYTE[0] = A
     * BYTE[1] = B
     * BYTE[2] = C
     * BYTE[3] = D
     * BYTE[4] = A
     * ...
     * Calling code should take care of endianness of the platform
     * when passing image data as ints.
     */
    public static class Format {
        public static final int BYTE_BGRA_PRE = 1;
        public static final int BYTE_ARGB = 2;
    };

    public static int getNativeFormat() {
        return Application.GetApplication().staticPixels_getNativeFormat();
    }

    // Need:
    // Clipboard:
    //    public Pixels(final int width, final int height, final byte[] data)
    //
    // Robot:
    // Rain:
    //    public Pixels(final int width, final int height, final int[] data)
    //
    // PixelUtils == Prism == GlassToolkit :
    //    public Pixels(final int width, final int height, final ByteBuffer)
    //    public Pixels(final int width, final int height, final IntBuffer)

    protected final int width;
    protected final int height;
    protected final int bytesPerComponent;

    protected final ByteBuffer bytes;
    protected final IntBuffer ints;

    protected Pixels(final int width, final int height, final ByteBuffer pixels) {
        this.width = width;
        this.height = height;
        this.bytesPerComponent = 1;
        this.bytes = pixels.slice();
        if ((this.width <= 0) || (this.height <= 0) || ((this.width * this.height * 4) != this.bytes.capacity())) {
            throw new IllegalArgumentException("Wrong byte buffer size "+width+"x"+height+" ["+(this.width*this.height*4)+"] != "+this.bytes.capacity());
        }
        
        this.ints = null;
    }
    
    protected Pixels(final int width, final int height, IntBuffer pixels) {
        this.width = width;
        this.height = height;
        this.bytesPerComponent = 4;
        this.ints = pixels.slice();
        if ((this.width <= 0) || (this.height <= 0) || ((this.width * this.height) != this.ints.capacity())) {
            throw new IllegalArgumentException("Wrong int buffer size "+this.width+"x"+this.height+" ["+(this.width*this.height)+"] != "+this.ints.capacity());
        }
        
        this.bytes = null;
    }

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

    public int getHeight() {
        return this.height;
    }
    
    public int getBytesPerComponent() {
        return this.bytesPerComponent;
    }

    /*
     * Return the original pixels buffer.
     */
    public Buffer getPixels() {
        if (this.bytes != null) {
            this.bytes.rewind();
            return this.bytes;
        } else if (this.ints != null) {
            this.ints.rewind();
            return this.ints;
        } else {
            throw new RuntimeException("Unexpected Pixels state.");
        }
    }

    /*
     * Return a copy of pixels as bytes.
     */
    public ByteBuffer asByteBuffer() {
        ByteBuffer bb = ByteBuffer.allocateDirect(getWidth()*getHeight()*4);
        bb.order(ByteOrder.nativeOrder());
        bb.rewind();
        asByteBuffer(bb);
        return bb;
    }

    /*
     * Copy pixels into provided byte buffer.
     * The ByteBuffer must be direct.
     */
    public void asByteBuffer(ByteBuffer bb) {
        if (bb.isDirect() == false) {
            throw new RuntimeException("Expected direct buffer.");
        } else if (bb.remaining() < (getWidth()*getHeight()*4)) {
            throw new RuntimeException("Too small buffer.");
        }
        _fillDirectByteBuffer(bb);
    }

    // This method is called from the native code to reduce the number of JNI up-calls.
    private void attachData(long ptr) {
        if (this.ints != null) {
            int[] array = !this.ints.isDirect() ? this.ints.array() : null;
            _attachInt(ptr, this.width, this.height, this.ints, array, array != null ? this.ints.arrayOffset() : 0);
        }
        if (this.bytes != null) {
            byte[] array = !this.bytes.isDirect() ? this.bytes.array() : null;
            _attachByte(ptr, this.width, this.height, this.bytes, array, array != null ? this.bytes.arrayOffset() : 0);
        }
    }
    
    protected abstract void _fillDirectByteBuffer(ByteBuffer bb);
    protected abstract void _attachInt(long ptr, int w, int h, IntBuffer ints, int[] array, int offset);
    protected abstract void _attachByte(long ptr, int w, int h, ByteBuffer bytes, byte[] array, int offset);

    @Override public boolean equals(Object object) {
        boolean equals = ((object != null) && (getClass().equals(object.getClass()) == true));
        if (equals == true) {
            Pixels pixels = (Pixels)object;
            equals = ((getWidth() == pixels.getWidth()) && (getHeight() == pixels.getHeight()));
            if (equals == true) {
                ByteBuffer b1 = asByteBuffer();
                ByteBuffer b2 = pixels.asByteBuffer();
                equals = (b1.compareTo(b2) == 0);
            }
        }
        return equals;
    }

    @Override public int hashCode() {
        int val = getWidth();
        val = 31*val + getHeight();
        val = 17*val + asByteBuffer().hashCode();
        return val;
    }
}
