// Copyright (C) 2016 Elviss Strazdins
// This file is part of the Ouzel engine.

package lv.elviss.ouzel;

import android.content.Context;
import android.opengl.GLSurfaceView;
import android.view.KeyEvent;
import android.view.MotionEvent;

public class OuzelSurfaceView extends GLSurfaceView implements InputDeviceListener
{
    private final InputManager inputManager;

    public OuzelSurfaceView(Context context)
    {
        super(context);

        inputManager = new InputManager();
        inputManager.registerInputDeviceListener(this, null);

        this.setFocusableInTouchMode(true);
    }
    
    @Override
    public boolean onTouchEvent(final MotionEvent event)
    {
        switch (event.getAction() & MotionEvent.ACTION_MASK)
        {
            case MotionEvent.ACTION_POINTER_DOWN:
            {
                final int indexPointerDown = event.getAction() >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
                final int pointerId = event.getPointerId(indexPointerDown);
                final float x = event.getX(indexPointerDown);
                final float y = event.getY(indexPointerDown);

                this.queueEvent(new Runnable()
                {
                    @Override
                    public void run()
                    {
                        OuzelLibJNIWrapper.handleActionDown(pointerId, x, y);
                    }
                });
                break;
            }

            case MotionEvent.ACTION_DOWN:
            {
                final int pointerId = event.getPointerId(0);
                final float x = event.getX(0);
                final float y = event.getY(0);

                this.queueEvent(new Runnable()
                {
                    @Override
                    public void run()
                    {
                        OuzelLibJNIWrapper.handleActionDown(pointerId, x, y);
                    }
                });
                break;
            }

            case MotionEvent.ACTION_MOVE:
            {
                final int pointerId = event.getPointerId(0);
                final float x = event.getX(0);
                final float y = event.getY(0);

                this.queueEvent(new Runnable()
                {
                    @Override
                    public void run()
                    {
                        OuzelLibJNIWrapper.handleActionMove(pointerId, x, y);
                    }
                });
                break;
            }

            case MotionEvent.ACTION_POINTER_UP:
            {
                final int indexPointUp = event.getAction() >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
                final int pointerId = event.getPointerId(indexPointUp);
                final float x = event.getX(indexPointUp);
                final float y = event.getY(indexPointUp);

                this.queueEvent(new Runnable()
                {
                    @Override
                    public void run()
                    {
                        OuzelLibJNIWrapper.handleActionUp(pointerId, x, y);
                    }
                });
                break;
            }

            case MotionEvent.ACTION_UP:
            {
                final int pointerId = event.getPointerId(0);
                final float x = event.getX(0);
                final float y = event.getY(0);

                this.queueEvent(new Runnable()
                {
                    @Override
                    public void run()
                    {
                        OuzelLibJNIWrapper.handleActionUp(pointerId, x, y);
                    }
                });
                break;
            }

            case MotionEvent.ACTION_CANCEL:
            {
                final int pointerId = event.getPointerId(0);
                final float x = event.getX(0);
                final float y = event.getY(0);

                this.queueEvent(new Runnable()
                {
                    @Override
                    public void run()
                    {
                        OuzelLibJNIWrapper.handleActionCancel(pointerId, x, y);
                    }
                });
                break;
            }
        }

        return super.onTouchEvent(event);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        inputManager.onInputEvent(event);
        // TODO: implement
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event)
    {
        inputManager.onInputEvent(event);
        // TODO: implement
        return super.onKeyUp(keyCode, event);
    }

    @Override
    public void onInputDeviceAdded(int deviceId)
    {
        // TODO: implement
    }

    @Override
    public void onInputDeviceChanged(int deviceId)
    {
        // TODO: implement
    }

    @Override
    public void onInputDeviceRemoved(int deviceId)
    {
        // TODO: implement
    }
}

@@ -1125,171 +1125,8 @@
 );%0A%0A
-                this.queueEvent(new Runnable()%0A                %7B%0A                    @Override%0A                    public void run()%0A                    %7B%0A        
     
@@ -1211,55 +1211,19 @@
     
-    %7D%0A                %7D);%0A                break
+return true
 ;%0A  
@@ -1451,171 +1451,8 @@
 );%0A%0A
-                this.queueEvent(new Runnable()%0A                %7B%0A                    @Override%0A                    public void run()%0A                    %7B%0A        
     
@@ -1537,55 +1537,19 @@
     
-    %7D%0A                %7D);%0A                break
+return true
 ;%0A  
@@ -1777,171 +1777,8 @@
 );%0A%0A
-                this.queueEvent(new Runnable()%0A                %7B%0A                    @Override%0A                    public void run()%0A                    %7B%0A        
     
@@ -1863,55 +1863,19 @@
     
-    %7D%0A                %7D);%0A                break
+return true
 ;%0A  
@@ -2244,171 +2244,8 @@
 );%0A%0A
-                this.queueEvent(new Runnable()%0A                %7B%0A                    @Override%0A                    public void run()%0A                    %7B%0A        
     
@@ -2328,55 +2328,19 @@
     
-    %7D%0A                %7D);%0A                break
+return true
 ;%0A  
@@ -2566,171 +2566,8 @@
 );%0A%0A
-                this.queueEvent(new Runnable()%0A                %7B%0A                    @Override%0A                    public void run()%0A                    %7B%0A        
     
@@ -2650,55 +2650,19 @@
     
-    %7D%0A                %7D);%0A                break
+return true
 ;%0A  
@@ -2892,171 +2892,8 @@
 );%0A%0A
-                this.queueEvent(new Runnable()%0A                %7B%0A                    @Override%0A                    public void run()%0A                    %7B%0A        
     
@@ -2980,55 +2980,19 @@
     
-    %7D%0A                %7D);%0A                break
+return true
 ;%0A  

Fix Android event handling