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

import com.sun.glass.utils.Disposer;
import com.sun.glass.utils.DisposerRecord;

import java.nio.IntBuffer;

public abstract class Robot {

    // Perhaps we can drop this native pointer if it's unneeded
    private long ptr;

    protected abstract long _create();
    protected Robot() {
        ptr = _create();
        if (ptr == 0L) {
            throw new RuntimeException("could not create platform robot");
        }
        Disposer.addRecord(this, this.disposerRecord = new RobotDisposerRecord(ptr));
    }

    private static class RobotDisposerRecord implements DisposerRecord {
        // destroy() sets the ptr to 0L
        public volatile long ptr;

        public RobotDisposerRecord(long ptr) {
            this.ptr = ptr;
        }

        @Override
        public void dispose() {
            if (ptr != 0L) {
                //_destroy(ptr);
            }
        }
    }
    private final RobotDisposerRecord disposerRecord;

    protected abstract void _destroy(long ptr);
    public void destroy() {
        _destroy(ptr);
        ptr = 0L;
        disposerRecord.ptr = 0L;
    }

    protected abstract void _keyPress(long ptr, int code);
    /**
     * Generate a key pressed event.
     * @param code key code for this event
     */
    public void keyPress(int code) {
        _keyPress(ptr, code);
    }

    protected abstract void _keyRelease(long ptr, int code);
    /**
     * Generate a key released event.
     *
     * @param code key code for this event
     */
    public void keyRelease(int code) {
        _keyRelease(ptr, code);
    }

    protected abstract void _mouseMove(long ptr, int x, int y);
    /**
     * Generate a mouse moved event.
     *
     * @param x screen coordinate x
     * @param y screen coordinate y
     */
    public void mouseMove(int x, int y) {
        _mouseMove(ptr, x, y);
    }

    protected abstract void _mousePress(long ptr, int buttons);
    /**
     * Generate a mouse press event with specified buttons mask.
     *
     * Up to 32-buttons mice are supported. Other buttons are inaccessible
     * by the robot. Bits 0, 1, and 2 mean LEFT, RIGHT, and MIDDLE mouse buttons
     * respectively.
     *
     * @param button buttons to have generated the event
     */
    public void mousePress(int buttons) {
        _mousePress(ptr, buttons);
    }

    protected abstract void _mouseRelease(long ptr, int buttons);
    /**
     * Generate a mouse release event with specified buttons mask.
     *
     * @param button buttons to have generated the event
     */
    public void mouseRelease(int buttons) {
        _mouseRelease(ptr, buttons);
    }

    protected abstract void _mouseWheel(long ptr, int wheelAmt);
    /**
     * Generate a mouse wheel event.
     *
     * @param wheelAmt amount the wheel has turned of wheel turning
     */
    public void mouseWheel(int wheelAmt) {
        _mouseWheel(ptr, wheelAmt);
    }

    protected abstract int _getMouseX(long ptr);
    public int getMouseX() {
        return _getMouseX(ptr);
    }

    protected abstract int _getMouseY(long ptr);
    public int getMouseY() {
        return _getMouseY(ptr);
    }

    protected abstract int _getPixelColor(long ptr, int x, int y);
    /**
     * Returns pixel color at specified screen coordinates in IntARGB format.
     */
    public int getPixelColor(int x, int y) {
        return _getPixelColor(ptr, x, y);
    }

    protected abstract void _getScreenCapture(long ptr, int x, int y, int width, int height, int[] data);
    /**
     * Returns a capture of the specified area of the screen.
     */
    public Pixels getScreenCapture(int x, int y, int width, int height) {
        int data[] = new int[width * height];

        _getScreenCapture(ptr, x, y, width, height, data);

        return Application.GetApplication().createPixels(width, height, IntBuffer.wrap(data));
    }
}
