package com.asha.vrlib;

import android.animation.PropertyValuesHolder;
import android.animation.ValueAnimator;
import android.content.Context;
import android.util.Log;
import android.view.KeyEvent;

import com.asha.vrlib.model.MDFlingConfig;

/**
 * @author: xulin
 * @projectName:MD360Player4Android-master
 * @createTime:2020/5/20 9:56
 */
public class MDKeyboardHelper {

    private long lastClickTime = 0;
    private String TAG = "MDKeyboardHelper";
    private int clickDistance = 3000;     //点按移动距离
    private int longClickDis = 2000;      //长按移动距离

    private ValueAnimator valueAnimator;
    private MDFlingConfig mFlingConfig;

    private MDVRLibrary.IAdvanceGestureListener mAdvanceGestureListener;

    public void setAdvanceGestureListener(MDVRLibrary.IAdvanceGestureListener listener) {
        this.mAdvanceGestureListener = listener;
    }

    public boolean handleKeyEvent(int keyCode, KeyEvent event){
        if(event.getAction() == KeyEvent.ACTION_DOWN){
            int realDistance = 0;        //实际移动距离

            if(System.currentTimeMillis() - lastClickTime > 100){
                //点按
               realDistance = clickDistance;
            }else{
                //长按
                realDistance = longClickDis;
            }
            lastClickTime = System.currentTimeMillis();
            Log.d(TAG,"lastClickTime:" + lastClickTime);
            switch (keyCode){
                case KeyEvent.KEYCODE_DPAD_LEFT:        //左
                    //mAdvanceGestureListener.onDrag(-realDistance,0);
                    animStart(realDistance,0);
                    return true;
                case KeyEvent.KEYCODE_DPAD_RIGHT:       //右
                    animStart(-realDistance,0);
                    return true;
                case KeyEvent.KEYCODE_DPAD_UP:          //上
                    animStart(0,realDistance);
                    return true;
                case KeyEvent.KEYCODE_DPAD_DOWN:        //下
                    animStart(0,-realDistance);
                    return true;
                case KeyEvent.KEYCODE_DPAD_CENTER:
                case KeyEvent.KEYCODE_ENTER:            //确定键
                    return true;
                case KeyEvent.KEYCODE_BACK:
                    return true;                        //退出
            }
        }
        return false;
    }


    /**---------------------动画拖拽效果--------------------------*/
    public void animStart(float velocityX, float velocityY) {
        animCancel();

        PropertyValuesHolder hvx = PropertyValuesHolder.ofFloat("vx", velocityX, 0);
        PropertyValuesHolder hvy = PropertyValuesHolder.ofFloat("vy", velocityY, 0);
        valueAnimator = ValueAnimator.ofPropertyValuesHolder(hvx, hvy).setDuration(mFlingConfig.getDuring());
        valueAnimator.setInterpolator(mFlingConfig.getInterpolator());
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            private long lastTime = 0;

            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                long now = animation.getCurrentPlayTime();
                long dur = (now - lastTime);
                float sx = (float) animation.getAnimatedValue("vx") * dur / -1000 * mFlingConfig.getSensitivity();
                float sy = (float) animation.getAnimatedValue("vy") * dur / -1000 * mFlingConfig.getSensitivity();
                lastTime = now;

                if (mAdvanceGestureListener != null){
                    mAdvanceGestureListener.onDrag(sx, sy);
                }
            }
        });
        valueAnimator.start();
    }

    public void setFlingConfig(MDFlingConfig flingConfig) {
        this.mFlingConfig = flingConfig;
    }



    private void animCancel(){
        if (valueAnimator != null){
            valueAnimator.cancel();
        }
    }
}
