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

// http://developer.apple.com/documentation/Cocoa/Reference/ApplicationKit/Classes/nscursor_Class/Reference/Reference.html

#import <Cocoa/Cocoa.h>

#import "common.h"
#import "com_sun_glass_ui_Cursor.h"
#import "com_sun_glass_ui_mac_MacCursor.h"

#import "GlassMacros.h"

//#define VERBOSE
#ifndef VERBOSE
    #define LOG(MSG, ...)
#else
    #define LOG(MSG, ...) GLASS_LOG(MSG, ## __VA_ARGS__);
#endif

#define CURSOR_BEST_SIZE 32

/*
 * Class:     com_sun_glass_ui_mac_MacCursor
 * Method:    _initIDs
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_com_sun_glass_ui_mac_MacCursor__1initIDs
(JNIEnv *env, jclass jCursorClass)
{
    if (jSizeInit == NULL)
    {
        jSizeInit = (*env)->GetMethodID(env, (*env)->FindClass(env, "com/sun/glass/ui/Size"), "<init>", "(II)V");
    }
}

/*
 * Class:     com_sun_glass_ui_mac_MacCursor
 * Method:    _createCursor
 * Signature: (IILcom/sun/glass/ui/Pixels;)J
 */
JNIEXPORT jlong JNICALL Java_com_sun_glass_ui_mac_MacCursor__1createCursor
(JNIEnv *env, jclass jCursorClass, jint x, jint y, jobject jPixels)
{
    LOG("Java_com_sun_glass_ui_mac_MacCursor__1createCursor");
    jlong jcursor = 0;
    
    GLASS_ASSERT_MAIN_JAVA_THREAD(env);
    GLASS_POOL_ENTER;
    {
        NSImage *image = NULL;
        (*env)->CallVoidMethod(env, jPixels, jPixelsAttachData, ptr_to_jlong(&image));
        if (image != NULL)
        {
            NSCursor *cursor = [[NSCursor alloc] initWithImage:image hotSpot:NSMakePoint(x, y)];
            jcursor = ptr_to_jlong(cursor);
            [image release];
        }
    }
    GLASS_POOL_EXIT;
    GLASS_CHECK_EXCEPTION(env);
    
    return jcursor;
}

/*
 * Class:     com_sun_glass_ui_mac_MacCursor
 * Method:    _set
 * Signature: (I)V
 */
JNIEXPORT void JNICALL Java_com_sun_glass_ui_mac_MacCursor__1set
(JNIEnv *env, jclass jCursorClass, jint jtype)
{
    LOG("Java_com_sun_glass_ui_mac_MacCursor__1set");
    
    GLASS_ASSERT_MAIN_JAVA_THREAD(env);
    GLASS_POOL_ENTER;
    {
        NSCursor *cursor = nil;
        switch (jtype)
        {
            case com_sun_glass_ui_Cursor_CURSOR_CUSTOM:
                // should never reach here, custom cursors are handled by Java_com_sun_glass_ui_mac_MacCursor__1setCustom
            case com_sun_glass_ui_Cursor_CURSOR_DEFAULT:
            default:
                cursor = [NSCursor arrowCursor];
                break;
            case com_sun_glass_ui_Cursor_CURSOR_TEXT:
                cursor = [NSCursor IBeamCursor];
                break;
            case com_sun_glass_ui_Cursor_CURSOR_CROSSHAIR:
                cursor = [NSCursor crosshairCursor];
                break;
            case com_sun_glass_ui_Cursor_CURSOR_CLOSED_HAND:
                cursor = [NSCursor closedHandCursor];
                break;
            case com_sun_glass_ui_Cursor_CURSOR_OPEN_HAND:
                cursor = [NSCursor openHandCursor];
                break;
            case com_sun_glass_ui_Cursor_CURSOR_POINTING_HAND:
                cursor = [NSCursor pointingHandCursor];
                break;
            case com_sun_glass_ui_Cursor_CURSOR_RESIZE_LEFT:
                cursor = [NSCursor resizeLeftCursor];
                break;
            case com_sun_glass_ui_Cursor_CURSOR_RESIZE_RIGHT:
                cursor = [NSCursor resizeRightCursor];
                break;
            case com_sun_glass_ui_Cursor_CURSOR_RESIZE_UP:
                cursor = [NSCursor resizeUpCursor];
                break;
            case com_sun_glass_ui_Cursor_CURSOR_RESIZE_DOWN:
                cursor = [NSCursor resizeDownCursor];
                break;
            case com_sun_glass_ui_Cursor_CURSOR_RESIZE_LEFTRIGHT:
                cursor = [NSCursor resizeLeftRightCursor];
                break;
            case com_sun_glass_ui_Cursor_CURSOR_RESIZE_UPDOWN:
                cursor = [NSCursor resizeUpDownCursor];
                break;
            case com_sun_glass_ui_Cursor_CURSOR_DISAPPEAR:
                cursor = [NSCursor disappearingItemCursor];
                break;
            case com_sun_glass_ui_Cursor_CURSOR_WAIT:
                // Not implemented, setting CURSOR_DEFAULT instead
                cursor = [NSCursor arrowCursor];
                break;
        }
        
        if ([NSThread isMainThread] == YES)
        {
            [cursor set];
        }
        else
        {
            [cursor performSelectorOnMainThread:@selector(set) withObject:nil waitUntilDone:YES];
        }
    }
    GLASS_POOL_EXIT;
    GLASS_CHECK_EXCEPTION(env);
}

/*
 * Class:     com_sun_glass_ui_mac_MacCursor
 * Method:    _setCustom
 * Signature: (J)V
 */
JNIEXPORT void JNICALL Java_com_sun_glass_ui_mac_MacCursor__1setCustom
(JNIEnv *env, jclass jCursorClass, jlong cursorPtr)
{
    LOG("Java_com_sun_glass_ui_mac_MacCursor__1setCustom");
    
    GLASS_ASSERT_MAIN_JAVA_THREAD(env);
    GLASS_POOL_ENTER;
    {
        NSCursor *cursor = (NSCursor*)jlong_to_ptr(cursorPtr);
        [cursor set];
    }
    GLASS_POOL_EXIT;
    GLASS_CHECK_EXCEPTION(env);
}

/*
 * Class:     com_sun_glass_ui_mac_MacCursor
 * Method:    _setVisible
 * Signature: (Z)V
 */
JNIEXPORT void JNICALL Java_com_sun_glass_ui_mac_MacCursor__1setVisible
(JNIEnv *env, jclass jCursorClass, jboolean visible)
{
    LOG("Java_com_sun_glass_ui_mac_MacCursor__1setVisible");
    
    GLASS_ASSERT_MAIN_JAVA_THREAD(env);
    GLASS_POOL_ENTER;
    {
        if (visible == JNI_TRUE)
        {
            [NSCursor unhide];
        }
        else
        {
            [NSCursor hide];
        }
    }
    GLASS_POOL_EXIT;
    GLASS_CHECK_EXCEPTION(env);
}

/*
 * Class:     com_sun_glass_ui_mac_MacCursor
 * Method:    _getBestSize
 * Signature: (II)Lcom.sun.glass.ui.Size;
 */
JNIEXPORT jobject JNICALL Java_com_sun_glass_ui_mac_MacCursor__1getBestSize
(JNIEnv *env, jclass jCursorClass, jint width, jint height)
{
    LOG("Java_com_sun_glass_ui_mac_MacCursor__1setVisible");
    
    jobject jsize = NULL;
    
    GLASS_ASSERT_MAIN_JAVA_THREAD(env);
    GLASS_POOL_ENTER;
    {
        jint widthBest = width;
        jint heightBest = height;
        
        NSImage *image = [[[NSCursor arrowCursor] image] retain];
        
        if (widthBest <= 0)
        {
            if (image != nil)
            {
                widthBest = [image size].width;
            }
            else
            {
                widthBest = CURSOR_BEST_SIZE;
            }
        }
        
        if (heightBest <= 0)
        {
            if (image != nil)
            {
                heightBest = [image size].height;
            }
            else
            {
                heightBest = CURSOR_BEST_SIZE;
            }
        }
        
        [image release];
        
        jsize = (*env)->NewObject(env, (*env)->FindClass(env, "com/sun/glass/ui/Size"), jSizeInit, widthBest, heightBest);
    }
    GLASS_POOL_EXIT;
    GLASS_CHECK_EXCEPTION(env);
    
    return jsize;
}
