/*
 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
 */

#import "Graphics3D.h"

#import "common.h"
#import "com_sun_rain_graphics_Graphics3D.h"
#import "com_sun_rain_graphics_Image.h"
#import "com_sun_rain_graphics_Color.h"
#import "com_sun_rain_graphics_Font.h"
#import "com_sun_rain_shapes_Shape.h"

#import <OpenGL/OpenGL.h>

static jclass jGraphics3DClass;
static jclass jFontClass;
static jclass jColorClass;
static jclass jByteBufferClass;
static jclass jImageClass;
static jclass jShapeClass;

@implementation Graphics3D

@end

static inline CTFontRef getMacFont(JNIEnv *env, jobject jfont)
{
    return (CTFontRef)jlong_to_ptr((*env)->GetLongField(env, jfont, (*env)->GetFieldID(env, jFontClass, "ptr", "J")));
}

static inline CGColorRef getMacColor(JNIEnv *env, jobject jcolor)
{
    return (CGColorRef)jlong_to_ptr((*env)->GetLongField(env, jcolor, (*env)->GetFieldID(env, jColorClass, "ptr", "J")));
}

static inline CGImageRef getMacImage(JNIEnv *env, jobject jimage)
{
    return (CGImageRef)jlong_to_ptr((*env)->GetLongField(env, jimage, (*env)->GetFieldID(env, jImageClass, "ptr", "J")));
}

static inline CGMutablePathRef getMacShape(JNIEnv *env, jobject jshape)
{
    return (CGMutablePathRef)jlong_to_ptr((*env)->GetLongField(env, jshape, (*env)->GetFieldID(env, jShapeClass, "ptr", "J")));
}

/*
 * Class:     com_sun_rain_shapes_Graphics3D
 * Method:    _initIDs
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_com_sun_rain_graphics_Graphics3D__1initIDs
(JNIEnv *env, jclass jGraphicsClass)
{
    jGraphics3DClass = (*env)->NewGlobalRef(env, jGraphicsClass);
    jFontClass = (*env)->NewGlobalRef(env, (*env)->FindClass(env, "com/sun/rain/graphics/Font"));
    jColorClass = (*env)->NewGlobalRef(env, (*env)->FindClass(env, "com/sun/rain/graphics/Color"));
    jByteBufferClass = (*env)->NewGlobalRef(env, (*env)->FindClass(env, "java/nio/ByteBuffer"));
    jImageClass = (*env)->NewGlobalRef(env, (*env)->FindClass(env, "com/sun/rain/graphics/Image"));
    jShapeClass = (*env)->NewGlobalRef(env, (*env)->FindClass(env, "com/sun/rain/shapes/Shape"));
}

/*
 * Class:     com_sun_rain_graphics_Graphics3D
 * Method:    _createGraphics
 * Signature: ()J
 */
JNIEXPORT jlong JNICALL Java_com_sun_rain_graphics_Graphics3D__1createGraphics
(JNIEnv *env, jobject jGraphics)
{
    Graphics3D *graphics = [[Graphics3D alloc] init];
    return ptr_to_jlong(graphics);
}

/*
 * Class:     com_sun_rain_shapes_Graphics3D
 * Method:    _setColor
 * Signature: (Lcom/sun/rain/graphics/Color;)V
 */
JNIEXPORT void JNICALL Java_com_sun_rain_graphics_Graphics3D__1setColor
(JNIEnv *env, jobject jGraphics, jlong jGraphicsPtr, jobject jcolor)
{
    //Graphics3D *graphics = (Graphics3D*)jlong_to_ptr(jGraphicsPtr);
	
	CGColorRef cgColor = getMacColor(env, jcolor);
	
	const CGFloat *colors = CGColorGetComponents(cgColor);
	glColor4f(colors[0], colors[1], colors[2], colors[3]);
}

/*
 * Class:     com_sun_rain_shapes_Graphics3D
 * Method:    _setFont
 * Signature: (Lcom/sun/rain/graphics/Font;)V
 */
JNIEXPORT void JNICALL Java_com_sun_rain_graphics_Graphics3D__1setFont
(JNIEnv *env, jobject jGraphics, jlong jGraphicsPtr, jobject jfont)
{
    //Graphics3D *graphics = (Graphics3D*)jlong_to_ptr(jGraphicsPtr);
}

/*
 * Class:     com_sun_rain_shapes_Graphics3D
 * Method:    _drawImage
 * Signature: (Lcom/sun/rain/graphics/Image;FFFF)V
 */
JNIEXPORT void JNICALL Java_com_sun_rain_graphics_Graphics3D__1drawImage
(JNIEnv *env, jobject jGraphics, jlong jGraphicsPtr, jobject jimage, jfloat x, jfloat y, jfloat width, jfloat height)
{
    //Graphics3D *graphics = (Graphics3D*)jlong_to_ptr(jGraphicsPtr);
}

/*
 * Class:     com_sun_rain_shapes_Graphics3D
 * Method:    _drawString
 * Signature: (Ljava/lang/String;FF)V
 */
JNIEXPORT void JNICALL Java_com_sun_rain_graphics_Graphics3D__1drawString
(JNIEnv *env, jobject jGraphics, jlong jGraphicsPtr, jstring jString, jfloat x, jfloat y)
{
    //Graphics3D *graphics = (Graphics3D*)jlong_to_ptr(jGraphicsPtr);
}

/*
 * Class:     com_sun_rain_shapes_Graphics3D
 * Method:    _fillRect
 * Signature: (FFFF)V
 */
JNIEXPORT void JNICALL Java_com_sun_rain_graphics_Graphics3D__1fillRect
(JNIEnv *env, jobject jGraphics, jlong jGraphicsPtr, jfloat x, jfloat y, jfloat width, jfloat height)
{
    //Graphics3D *graphics = (Graphics3D*)jlong_to_ptr(jGraphicsPtr);
	
	glBegin(GL_QUADS);
		glVertex2f(      x, y+height);
		glVertex2f(x+width, y+height);
		glVertex2f(x+width,        y);
		glVertex2f(      x,		   y);
	glEnd();
}

/*
 * Class:     com_sun_rain_shapes_Graphics3D
 * Method:    _fillEllipse
 * Signature: (FFFF)V
 */
JNIEXPORT void JNICALL Java_com_sun_rain_graphics_Graphics3D__1fillEllipse
(JNIEnv *env, jobject jGraphics, jlong jGraphicsPtr, jfloat x, jfloat y, jfloat width, jfloat height)
{
    //Graphics3D *graphics = (Graphics3D*)jlong_to_ptr(jGraphicsPtr);
}

/*
 * Class:     com_sun_rain_shapes_Graphics3D
 * Method:    _fillShape
 * Signature: (Lcom/sun/rain/shapes/Shape;Ljava/nio/ByteBuffer;FF)V
 */
JNIEXPORT void JNICALL Java_com_sun_rain_graphics_Graphics3D__1fillShape
(JNIEnv *env, jobject jGraphics, jlong jGraphicsPtr, jobject jshape, jobject jbuffer, jfloat x, jfloat y)
{
    //Graphics3D *graphics = (Graphics3D*)jlong_to_ptr(jGraphicsPtr);
}

/*
 * Class:     com_sun_rain_shapes_Graphics3D
 * Method:    _translate
 * Signature: (FF)V
 */
JNIEXPORT void JNICALL Java_com_sun_rain_graphics_Graphics3D__1translate
(JNIEnv *env, jobject jGraphics, jlong jGraphicsPtr, jfloat x, jfloat y)
{
    //Graphics3D *graphics = (Graphics3D*)jlong_to_ptr(jGraphicsPtr);
}

/*
 * Class:     com_sun_rain_shapes_Graphics3D
 * Method:    _scale
 * Signature: (FF)V
 */
JNIEXPORT void JNICALL Java_com_sun_rain_graphics_Graphics3D__1scale
(JNIEnv *env, jobject jGraphics, jlong jGraphicsPtr, jfloat x, jfloat y)
{
    //Graphics3D *graphics = (Graphics3D*)jlong_to_ptr(jGraphicsPtr);
}

/*
 * Class:     com_sun_rain_graphics_Graphics3D
 * Method:    _rotate
 * Signature: (F)V
 */
JNIEXPORT void JNICALL Java_com_sun_rain_graphics_Graphics3D__1rotate
(JNIEnv *env, jobject jGraphics, jlong jGraphicsPtr, jfloat a)
{
    //Graphics3D *graphics = (Graphics3D*)jlong_to_ptr(jGraphicsPtr);
}

/*
 * Class:     com_sun_rain_shapes_Graphics3D
 * Method:    _pushCTM
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_com_sun_rain_graphics_Graphics3D__1pushCTM
(JNIEnv *env, jobject jGraphics, jlong jGraphicsPtr)
{
    //Graphics3D *graphics = (Graphics3D*)jlong_to_ptr(jGraphicsPtr);
}

/*
 * Class:     com_sun_rain_shapes_Graphics3D
 * Method:    _popCTM
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_com_sun_rain_graphics_Graphics3D__1popCTM
(JNIEnv *env, jobject jGraphics, jlong jGraphicsPtr)
{
    //Graphics3D *graphics = (Graphics3D*)jlong_to_ptr(jGraphicsPtr);
}

/*
 * Class:     com_sun_rain_shapes_Graphics3D
 * Method:    _clip
 * Signature: (FFFF)V
 */
JNIEXPORT void JNICALL Java_com_sun_rain_graphics_Graphics3D__1clip
(JNIEnv *env, jobject jGraphics, jlong jGraphicsPtr, jfloat x, jfloat y, jfloat w, jfloat h)
{
    //Graphics3D *graphics = (Graphics3D*)jlong_to_ptr(jGraphicsPtr);
}

/*
 * Class:     com_sun_rain_shapes_Graphics3D
 * Method:    _pushClip
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_com_sun_rain_graphics_Graphics3D__1pushClip
(JNIEnv *env, jobject jGraphics, jlong jGraphicsPtr)
{
    //Graphics3D *graphics = (Graphics3D*)jlong_to_ptr(jGraphicsPtr);
}

/*
 * Class:     com_sun_rain_shapes_Graphics3D
 * Method:    _popClip
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_com_sun_rain_graphics_Graphics3D__1popClip
(JNIEnv *env, jobject jGraphics, jlong jGraphicsPtr)
{
    //Graphics3D *graphics = (Graphics3D*)jlong_to_ptr(jGraphicsPtr);
}

/*
 * Class:     com_sun_rain_graphics_Graphics3D
 * Method:    _begin
 * Signature: (J)V
 */
JNIEXPORT void JNICALL Java_com_sun_rain_graphics_Graphics3D__1begin
(JNIEnv *env, jobject jGraphics, jlong graphicsPtr, jlong viewPtr)
{
	if (viewPtr != 0L)
	{
		NSView *view = (NSView*)jlong_to_ptr(viewPtr);
		
        NSSize size = [view bounds].size;
        glMatrixMode(GL_PROJECTION);
        glPushMatrix();
        glLoadIdentity();
        glOrtho(0.0f, size.width, size.height, 0.0f, -1.0f, 1.0f);
        {
            glMatrixMode(GL_MODELVIEW);
            glPushMatrix();
            glLoadIdentity();
            {
            }
        }
        
        glClear(GL_COLOR_BUFFER_BIT);
	}
}

/*
 * Class:     com_sun_rain_graphics_Graphics3D
 * Method:    _end
 * Signature: (J)V
 */
JNIEXPORT void JNICALL Java_com_sun_rain_graphics_Graphics3D__1end
(JNIEnv *env, jobject jGraphics, jlong graphicsPtr)
{
#if 0
    GLfloat w = 50.0f;
    GLfloat h = 50.0f;
    glBegin(GL_QUADS);
    {
        glColor4f(1, 0, 0, 1); glTexCoord2f(0.0f, 0.0f); glVertex2f(0.0f, 0.0f);
        glColor4f(0, 1, 0, 1); glTexCoord2f(   w, 0.0f); glVertex2f(   w, 0.0f);
        glColor4f(0, 0, 1, 1); glTexCoord2f(   w,    h); glVertex2f(   w,    h);
        glColor4f(1, 1, 1, 1); glTexCoord2f(0.0f,    h); glVertex2f(0.0f,    h);
    }
    glEnd();
#endif
    
    {
        {
        }
        glMatrixMode(GL_MODELVIEW);
        glPopMatrix();
    }
    glMatrixMode(GL_PROJECTION);
    glPopMatrix();
}

