package com.asha.vrlib.strategy.interactive;

import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.util.Log;

import com.asha.vrlib.MD360Director;
import com.asha.vrlib.common.MDMainHandler;
import com.asha.vrlib.common.VRUtil;

/**
 * Created by hzqiujiadi on 16/3/19.
 * hzqiujiadi ashqalcn@gmail.com
 */
public class MotionStrategy extends AbsInteractiveStrategy implements SensorEventListener {

    private static final String TAG = "MotionStrategy";

    private int mDeviceRotation;

    private float[] mSensorMatrix = new float[16];

    private boolean mRegistered = false;

    private Boolean mIsSupport = null;

    public MotionStrategy(InteractiveModeManager.Params params) {
        super(params);
    }

    @Override
    public void onResume(Context context) {
        registerSensor(context);
    }

    @Override
    public void onPause(Context context) {
        unregisterSensor(context);
    }

    @Override
    public boolean handleDrag(int distanceX, int distanceY) {
        return false;
    }

    @Override
    public void onOrientationChanged(Activity activity) {
        mDeviceRotation = activity.getWindowManager().getDefaultDisplay().getRotation();
    }

    @Override
    public void on(Activity activity) {
        mDeviceRotation = activity.getWindowManager().getDefaultDisplay().getRotation();
        for (MD360Director director : getDirectorList()){
            director.reset();
        }
    }

    @Override
    public void off(Activity activity) {
        unregisterSensor(activity);
    }

    @Override
    public boolean isSupport(Activity activity) {
        if (mIsSupport == null){
            SensorManager mSensorManager = (SensorManager) activity
                    .getSystemService(Context.SENSOR_SERVICE);
            Sensor sensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR);
            mIsSupport = (sensor != null);
        }
        return mIsSupport;
    }

    protected void registerSensor(Context context){
        if (mRegistered) return;

        SensorManager mSensorManager = (SensorManager) context
                .getSystemService(Context.SENSOR_SERVICE);
        Sensor sensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR);

        if (sensor == null){
            Log.e(TAG,"TYPE_ROTATION_VECTOR sensor not support!");
            return;
        }

        mSensorManager.registerListener(this, sensor, getParams().mMotionDelay, MDMainHandler.sharedHandler());

        mRegistered = true;
    }

    protected void unregisterSensor(Context context){
        if (!mRegistered) return;

        SensorManager mSensorManager = (SensorManager) context
                .getSystemService(Context.SENSOR_SERVICE);
        mSensorManager.unregisterListener(this);

        mRegistered = false;
    }

    @Override
    public void onSensorChanged(final SensorEvent event) {
        if (event.accuracy != 0){
            if (getParams().mSensorListener != null){
                getParams().mSensorListener.onSensorChanged(event);
            }

            int type = event.sensor.getType();
            switch (type){
                case Sensor.TYPE_ROTATION_VECTOR:
                    // post
                    getParams().mGLHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            VRUtil.sensorRotationVector2Matrix(event, mDeviceRotation, mSensorMatrix);
                            for (MD360Director director : getDirectorList()){
                                director.updateSensorMatrix(mSensorMatrix);
                                // if (mDisplayMode == DISPLAY_MODE_NORMAL) break;
                            }
                        }
                    });
                    break;
            }
        }
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
        if (getParams().mSensorListener != null){
            getParams().mSensorListener.onAccuracyChanged(sensor,accuracy);
        }
    }
}

@@ -3374,16 +3374,111 @@
 // post%0A
+                    VRUtil.sensorRotationVector2Matrix(event, mDeviceRotation, mSensorMatrix);%0A
         
@@ -3521,20 +3521,28 @@
 ost(
-new 
+updateSensor
 Runnable
 () %7B
@@ -3537,20 +3537,18 @@
 Runnable
-() %7B
+);
 %0A       
@@ -3564,30 +3564,27 @@
     
-    @Override%0A
+break;%0A    
         
     
@@ -3575,32 +3575,34 @@
             
+%7D%0A
         
         publ
@@ -3593,155 +3593,124 @@
     
+%7D%0A
     
+%7D%0A%0A
     p
-ublic void run() %7B%0A                            VRUtil.sensorRotationVector2Matrix(event, mDeviceRotation, mSensorMatrix);%0A                
+rivate Runnable updateSensorRunnable = new Runnable() %7B%0A        @Override%0A        public void run() %7B%0A
     
@@ -3783,32 +3783,16 @@
         
-   
-             
 director
@@ -3843,32 +3843,16 @@
         
-              
-  
 // if (m
@@ -3910,115 +3910,8 @@
     
-                %7D%0A                        %7D%0A                    %7D);%0A                    break;%0A            
 %7D%0A  
@@ -3915,32 +3915,33 @@
 %0A        %7D%0A    %7D
+;
 %0A%0A    @Override%0A

use the same runnable to update sensor.