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


#include "mat_common.h"

#include <com_sun_glass_ui_Screen.h>

//Todo: all Screens are currently the XDefaultScreenOfDisplay()

void mat_SetJavaScreen(Screen *screen, JNIEnv *env, jobject jscreen)
{
    //Todo: cache ids ? seems not to be called often.
    if (jscreen != NULL)
    {
        (*env)->SetLongField(env, jscreen,
                (*env)->GetFieldID(env, mat_jScreenClass, "ptr", "J"), ptr_to_jlong(screen));
        (*env)->SetIntField(env, jscreen,
                (*env)->GetFieldID(env, mat_jScreenClass, "depth", "I"), DefaultDepthOfScreen(screen));
        (*env)->SetIntField(env, jscreen,
                (*env)->GetFieldID(env, mat_jScreenClass, "x", "I"), 0);
        (*env)->SetIntField(env, jscreen,
                (*env)->GetFieldID(env, mat_jScreenClass, "y", "I"), 0);
        (*env)->SetIntField(env, jscreen,
                (*env)->GetFieldID(env, mat_jScreenClass, "width", "I"), WidthOfScreen(screen));
        (*env)->SetIntField(env, jscreen,
                (*env)->GetFieldID(env, mat_jScreenClass, "height", "I"), HeightOfScreen(screen));

        (*env)->SetIntField(env, jscreen,
                (*env)->GetFieldID(env, mat_jScreenClass, "visibleX", "I"), 0);
        (*env)->SetIntField(env, jscreen,
                (*env)->GetFieldID(env, mat_jScreenClass, "visibleY", "I"), 0);
        (*env)->SetIntField(env, jscreen,
                (*env)->GetFieldID(env, mat_jScreenClass, "visibleWidth", "I"), WidthOfScreen(screen));
        (*env)->SetIntField(env, jscreen,
                (*env)->GetFieldID(env, mat_jScreenClass, "visibleHeight", "I"), HeightOfScreen(screen));

        // Todo: fix value for scale, resolutionX, resolutionY
        (*env)->SetFloatField(env, jscreen,
                (*env)->GetFieldID(env, mat_jScreenClass, "scale", "F"), 1.0);
        (*env)->SetIntField(env, jscreen,
                (*env)->GetFieldID(env, mat_jScreenClass, "resolutionX", "I"), 72);
        (*env)->SetIntField(env, jscreen,
                (*env)->GetFieldID(env, mat_jScreenClass, "resolutionY", "I"), 72);
        GLASS_CHECK_EXCEPTION(env);
    }
}



/*
 * Class:     com_sun_glass_ui_Screen
 * Method:    _getDeepestScreen
 * Signature: (Lcom/sun/glass/ui/Screen;)Lcom/sun/glass/ui/Screen;
 */
JNIEXPORT jobject JNICALL Java_com_sun_glass_ui_x11_X11Screen__1getDeepestScreen
  (JNIEnv *env, jclass jscreenClass, jobject jscreen) {
    DEBUG_PRINTF("X11Screen_getDeepestScreen\n");
    Screen *xscreen;
    xscreen = matScreenDefault();
    mat_SetJavaScreen(xscreen, env, jscreen);
    return jscreen;
}

/*
 * Class:     com_sun_glass_ui_Screen
 * Method:    _getMainScreen
 * Signature: (Lcom/sun/glass/ui/Screen;)Lcom/sun/glass/ui/Screen;
 */
JNIEXPORT jobject JNICALL Java_com_sun_glass_ui_x11_X11Screen__1getMainScreen
  (JNIEnv *env, jclass jscreenClass, jobject jscreen) {
    DEBUG_PRINTF("X11Screen_getMainScreen\n");
    Screen *xscreen;
    xscreen = matScreenDefault();
    mat_SetJavaScreen(xscreen, env, jscreen);
    return jscreen;
}

/*
 * Class:     com_sun_glass_ui_Screen
 * Method:    _getScreenForLocation
 * Signature: (Lcom/sun/glass/ui/Screen;II)Lcom/sun/glass/ui/Screen;
 */
JNIEXPORT jobject JNICALL Java_com_sun_glass_ui_x11_X11Screen__1getScreenForLocation
  (JNIEnv *env, jclass jscreenClass, jobject jscreen, jint x, jint y) {
    DEBUG_PRINTF("X11Screen_getScreenForLocation\n");
    Screen *xscreen;
    xscreen = matScreenDefault();
    mat_SetJavaScreen(xscreen, env, jscreen);
    return jscreen;
}

/*
 * Class:     com_sun_glass_ui_Screen
 * Method:    _getScreenForPtr
 * Signature: (Lcom/sun/glass/ui/Screen;J)Lcom/sun/glass/ui/Screen;
 */
JNIEXPORT jobject JNICALL Java_com_sun_glass_ui_x11_X11Screen__1getScreenForPtr
  (JNIEnv *env, jclass jscreenClass, jobject jscreen, jlong ptr) {
    DEBUG_PRINTF("X11Screen_getScreenForPointer\n");
    Screen *xscreen;
    xscreen = matScreenDefault();
    mat_SetJavaScreen(xscreen, env, jscreen);
    return jscreen;
}

/*
 * Class:     com_sun_glass_ui_Screen
 * Method:    _getScreens
 * Signature: (Ljava/util/Vector;)Ljava/util/Vector;
 */
JNIEXPORT jobject JNICALL Java_com_sun_glass_ui_x11_X11Screen__1getScreens
  (JNIEnv *env, jclass jscreenClass, jobject vectorScreens) {
    DEBUG_PRINTF("X11Screen_getScreens\n");

    jobject jscreen = (*env)->NewObject(env, mat_jScreenClass, (*env)->GetMethodID(env, mat_jScreenClass, "<init>", "()V"));
    Screen *xscreen = matScreenDefault();
    mat_SetJavaScreen(xscreen, env, jscreen);
    (*env)->CallVoidMethod(env, vectorScreens, mat_jListAddElement, jscreen);
    return vectorScreens;
}
