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

import com.sun.glass.ui.TouchInputSupport;
import com.sun.glass.ui.GestureSupport;
import com.sun.glass.ui.Application;
import com.sun.glass.ui.View;

/**
 * @author Alexey Semenyuk
 */
public class MacGestureSupport {

    private final static int GESTURE_ROTATE = 100;
    private final static int GESTURE_MAGNIFY = 101;
    private final static int GESTURE_SWIPE = 102;

    private final static boolean isDirect = false;

    private final static TouchInputSupport touches = new TouchInputSupport(false);
    private final static GestureSupport gestures = new GestureSupport(false);

    public static void notifyBeginTouchEvent(View view, int modifiers,
                                             int touchEventCount) {
        touches.notifyBeginTouchEvent(view, modifiers, isDirect, touchEventCount);
    }

    public static void notifyNextTouchEvent(View view, int state, long id,
                                            float x, float y) {
        // 'x' and 'y' address normalized position. The normalized position
        // is a scaled value between (0.0) and (1.0,1.0), where (0.0,0.0) is
        // the lower-left position on the touch device.
        //
        // So translate the touch position to integer coordinate where (0.0)
        // is the top-left position and (10000,10000) is the bottom-right
        // position on the touch device.

        final int intX = (int) (10000 * x);
        final int intY = 10000 - (int) (10000 * y);
        touches.notifyNextTouchEvent(view, state, id, intX, intY, intX, intY);
    }

    public static void notifyEndTouchEvent(View view) {
        touches.notifyEndTouchEvent(view);
    }

    public static void rotateGesturePerformed(View view, int modifiers, int x,
                                              int y, int xAbs, int yAbs,
                                              float rotation) {
        gestures.handleDeltaRotation(view, modifiers, isDirect, false, x, y,
                                     xAbs, yAbs, -rotation);
    }

    public static void scrollGesturePerformed(View view, int modifiers, int x,
                                              int y, int xAbs, int yAbs,
                                              float dx, float dy) {
        final int touchCount = touches.getTouchCount();
        if (touchCount == 0) {
            // When inertial scrolling occurs system sends a number of scroll
            // (mouse wheel notifications to be precise) notifications when
            // there is no touch points any more. Handle these notifications
            // in a special way like swipe gestures, i.e. notify view with only
            // GestureEvent#GESTURE_PERFORMED event type.
            gestures.handleScrollingPerformed(view, modifiers, isDirect, true, x,
                                              y, xAbs, yAbs, dx, dy);
        } else {
            gestures.handleDeltaScrolling(view, modifiers, isDirect, false,
                                          touchCount, x, y, xAbs, yAbs, dx, dy);
        }
    }

    public static void swipeGesturePerformed(View view, int modifiers, int dir,
                                             int x, int y, int xAbs, int yAbs) {
        // On OS X system doesn't call NSResponder#endGestureWithEvent
        // for swipe gestures. So notify view with only
        // GestureEvent#GESTURE_PERFORMED event type.
        gestures.handleSwipePerformed(view, modifiers, isDirect, false,
                                      gestures.getScrollGestureTouchCount(
                touches), dir, x, y, xAbs, yAbs);
    }

    public static void magnifyGesturePerformed(View view, int modifiers, int x,
                                               int y, int xAbs, int yAbs,
                                               float scale) {
        gestures.handleDeltaZooming(view, modifiers, isDirect, false, x, y, xAbs,
                                    yAbs, scale, View.GESTURE_NO_DOUBLE_VALUE);
    }

    public static void gestureFinished(View view, int modifiers, int x, int y,
                                       int xAbs, int yAbs) {
        if (gestures.isScrolling()) {
            gestures.handleScrollingEnd(view, modifiers,
                                        gestures.getScrollGestureTouchCount(
                    touches), isDirect, false, x, y, xAbs, yAbs);
        }

        if (gestures.isRotating()) {
            gestures.handleRotationEnd(view, modifiers, isDirect, false, x, y,
                                       xAbs, yAbs);
        }

        if (gestures.isZooming()) {
            gestures.handleZoomingEnd(view, modifiers, isDirect, false, x, y,
                                      xAbs, yAbs);
        }
    }

    native private static void _initIDs();


    static {
        Application.loadNativeLibrary();
        _initIDs();
    }
}