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


package com.sun.glass.ui.x11;

import com.sun.glass.ui.Application;
import com.sun.glass.ui.Pen;
import com.sun.glass.ui.Pixels;
import com.sun.glass.ui.View;

import java.util.Map;

/**
 *
 * @author ddhill
 */
public class X11View extends View {

    static {
        X11Application.initLibrary();
    }

    protected X11View(Pen pen) {
        super(pen);
    }

    // Constants
    private static long multiClickTime =  300; //Todo
    private static int multiClickMaxX = 2;
    private static int multiClickMaxY = 2;

    native private static long _getMultiClickTime_impl();
    native private static int _getMultiClickMaxX_impl();
    native private static int _getMultiClickMaxY_impl();

    // view variables
    private int x;
    private int y;
    private long nativePtr;

    static long getMultiClickTime_impl() {
        if (multiClickTime == -1)
            multiClickTime = _getMultiClickTime_impl();
        return multiClickTime;
    }

    static int getMultiClickMaxX_impl() {
        if (multiClickMaxX == -1)
            multiClickMaxX = _getMultiClickMaxX_impl();
        return multiClickMaxX;
    }

    static int getMultiClickMaxY_impl() {
        if (multiClickMaxY == -1)
            multiClickMaxY = _getMultiClickMaxY_impl();
        return multiClickMaxY;
    }

    @Override protected void _enableInputMethodEvents(long ptr, boolean enable) {
    }

    @Override
    native protected long _create(Map caps);

    @Override
    protected long _getNativeView(long ptr) {
        // passed in is the View.ptr, need to pass back out the Window ?
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    protected int _getX(long ptr) {
        return x;
    }

    @Override
    protected int _getY(long ptr) {
        return y;
    }

    @Override
    native protected void _setParent(long ptr, long parentPtr);

    @Override
    native protected boolean _close(long ptr);

    @Override
    protected void _repaint(long ptr) {
        // native code is there but does nothing yet
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    protected native boolean _begin(long ptr, boolean isValid);

    @Override
    protected native void _end(long ptr, boolean isValid, boolean flush);

    native protected void _uploadPixelsToWindow(long ptr, long windowPtr, Pixels pixels);

    @Override protected void _uploadPixels(long ptr, Pixels pixels) {
        if(getWindow() != null) {
            _uploadPixelsToWindow(ptr, getWindow().getNativeWindow(), pixels);
        }
    }

    private void _notifyMove(int x, int y) {
        this.x = x;
        this.y = y;
    }

    @Override
    protected native boolean _enterFullscreen(long ptr, boolean animate, boolean keepRatio, boolean hideCursor);

    @Override
    protected native void _exitFullscreen(long ptr, boolean animate);

}
