package com.mime.dualscreenview.view

import android.content.Context
import android.os.SystemClock
import android.util.Log
import android.view.GestureDetector
import android.view.MotionEvent
import android.view.View
import android.view.View.OnTouchListener
import com.mime.dualscreenview.common.Blog
import com.mime.dualscreenview.view.GestureAnalyser.GestureType
import kotlin.math.abs
import kotlin.math.pow
import kotlin.math.sqrt


enum class TouchArea {
    Left,Center,Right, DoubleLeft, DoubleRight
}

class GestureAnalyser @JvmOverloads constructor(
    swipeSlopeIntolerance: Int = 3,
    doubleTapMaxDelayMillis: Int = 500,
    doubleTapMaxDownMillis: Int = 100
) {
    private val initialX = DoubleArray(5)
    private val initialY = DoubleArray(5)
    private val finalX = DoubleArray(5)
    private val finalY = DoubleArray(5)
    private val currentX = DoubleArray(5)
    private val currentY = DoubleArray(5)
    private val delX = DoubleArray(5)
    private val delY = DoubleArray(5)

    private var numFingers = 0
    private var initialT: Long = 0
    private var finalT: Long = 0
    private var currentT: Long = 0

    private var prevInitialT: Long = 0
    private var prevFinalT: Long = 0

    private var swipeSlopeIntolerance = 3

    private val doubleTapMaxDelayMillis: Long
    private val doubleTapMaxDownMillis: Long

    init {
        this.swipeSlopeIntolerance = swipeSlopeIntolerance
        this.doubleTapMaxDownMillis = doubleTapMaxDownMillis.toLong()
        this.doubleTapMaxDelayMillis = doubleTapMaxDelayMillis.toLong()
    }

    fun trackGesture(ev: MotionEvent) {
        val n = ev.pointerCount
        for (i in 0 until n) {
            initialX[i] = ev.getX(i).toDouble()
            initialY[i] = ev.getY(i).toDouble()
        }
        numFingers = n
        initialT = SystemClock.uptimeMillis()
    }

    fun untrackGesture() {
        numFingers = 0
        prevFinalT = SystemClock.uptimeMillis()
        prevInitialT = initialT
    }

    fun getGesture(ev: MotionEvent): GestureType {
        var averageDistance = 0.0
        for (i in 0 until numFingers) {
            finalX[i] = ev.getX(i).toDouble()
            finalY[i] = ev.getY(i).toDouble()
            delX[i] = finalX[i] - initialX[i]
            delY[i] = finalY[i] - initialY[i]

            averageDistance += sqrt(
                (finalX[i] - initialX[i]).pow(2.0) + (finalY[i] - initialY[i]).pow(
                    2.0
                )
            )
        }
        averageDistance /= numFingers.toDouble()

        finalT = SystemClock.uptimeMillis()
        val gt = GestureType()
        gt.gestureFlag = calcGesture()
        gt.gestureDuration = finalT - initialT
        gt.gestureDistance = averageDistance
        return gt
    }

    fun getOngoingGesture(ev: MotionEvent): Int {
        for (i in 0 until numFingers) {
            currentX[i] = ev.getX(i).toDouble()
            currentY[i] = ev.getY(i).toDouble()
            delX[i] = finalX[i] - initialX[i]
            delY[i] = finalY[i] - initialY[i]
        }
        currentT = SystemClock.uptimeMillis()
        return calcGesture()
    }

    private fun calcGesture(): Int {
        if (isDoubleTap) {
            return DOUBLE_TAP_1
        }

        if (numFingers == 1) {
            if ((-(delY[0])) > (swipeSlopeIntolerance * (abs(
                    delX[0]
                )))
            ) {
                return SWIPE_1_UP
            }

            if (((delY[0])) > (swipeSlopeIntolerance * (abs(
                    delX[0]
                )))
            ) {
                return SWIPE_1_DOWN
            }

            if ((-(delX[0])) > (swipeSlopeIntolerance * (abs(
                    delY[0]
                )))
            ) {
                return SWIPE_1_LEFT
            }

            if (((delX[0])) > (swipeSlopeIntolerance * (abs(
                    delY[0]
                )))
            ) {
                return SWIPE_1_RIGHT
            }
        }
        if (numFingers == 2) {
            if (((-delY[0]) > (swipeSlopeIntolerance * abs(
                    delX[0]
                ))) && ((-delY[1]) > (swipeSlopeIntolerance * abs(
                    delX[1]
                )))
            ) {
                return SWIPE_2_UP
            }
            if (((delY[0]) > (swipeSlopeIntolerance * abs(
                    delX[0]
                ))) && ((delY[1]) > (swipeSlopeIntolerance * abs(
                    delX[1]
                )))
            ) {
                return SWIPE_2_DOWN
            }
            if (((-delX[0]) > (swipeSlopeIntolerance * abs(
                    delY[0]
                ))) && ((-delX[1]) > (swipeSlopeIntolerance * abs(
                    delY[1]
                )))
            ) {
                return SWIPE_2_LEFT
            }
            if (((delX[0]) > (swipeSlopeIntolerance * abs(
                    delY[0]
                ))) && ((delX[1]) > (swipeSlopeIntolerance * abs(
                    delY[1]
                )))
            ) {
                return SWIPE_2_RIGHT
            }
            if (finalFingDist(0, 1) > 2 * (initialFingDist(0, 1))) {
                return UNPINCH_2
            }
            if (finalFingDist(0, 1) < 0.5 * (initialFingDist(0, 1))) {
                return PINCH_2
            }
        }
        if (numFingers == 3) {
            if (((-delY[0]) > (swipeSlopeIntolerance * abs(
                    delX[0]
                )))
                && ((-delY[1]) > (swipeSlopeIntolerance * abs(
                    delX[1]
                )))
                && ((-delY[2]) > (swipeSlopeIntolerance * abs(
                    delX[2]
                )))
            ) {
                return SWIPE_3_UP
            }
            if (((delY[0]) > (swipeSlopeIntolerance * abs(
                    delX[0]
                )))
                && ((delY[1]) > (swipeSlopeIntolerance * abs(
                    delX[1]
                )))
                && ((delY[2]) > (swipeSlopeIntolerance * abs(
                    delX[2]
                )))
            ) {
                return SWIPE_3_DOWN
            }
            if (((-delX[0]) > (swipeSlopeIntolerance * abs(
                    delY[0]
                )))
                && ((-delX[1]) > (swipeSlopeIntolerance * abs(
                    delY[1]
                )))
                && ((-delX[2]) > (swipeSlopeIntolerance * abs(
                    delY[2]
                )))
            ) {
                return SWIPE_3_LEFT
            }
            if (((delX[0]) > (swipeSlopeIntolerance * abs(
                    delY[0]
                )))
                && ((delX[1]) > (swipeSlopeIntolerance * abs(
                    delY[1]
                )))
                && ((delX[2]) > (swipeSlopeIntolerance * abs(
                    delY[2]
                )))
            ) {
                return SWIPE_3_RIGHT
            }

            if ((finalFingDist(0, 1) > 1.75 * (initialFingDist(0, 1)))
                && (finalFingDist(1, 2) > 1.75 * (initialFingDist(1, 2)))
                && (finalFingDist(2, 0) > 1.75 * (initialFingDist(2, 0)))
            ) {
                return UNPINCH_3
            }
            if ((finalFingDist(0, 1) < 0.66 * (initialFingDist(0, 1)))
                && (finalFingDist(1, 2) < 0.66 * (initialFingDist(1, 2)))
                && (finalFingDist(2, 0) < 0.66 * (initialFingDist(2, 0)))
            ) {
                return PINCH_3
            }
        }
        if (numFingers == 4) {
            if (((-delY[0]) > (swipeSlopeIntolerance * abs(
                    delX[0]
                )))
                && ((-delY[1]) > (swipeSlopeIntolerance * abs(
                    delX[1]
                )))
                && ((-delY[2]) > (swipeSlopeIntolerance * abs(
                    delX[2]
                )))
                && ((-delY[3]) > (swipeSlopeIntolerance * abs(
                    delX[3]
                )))
            ) {
                return SWIPE_4_UP
            }
            if (((delY[0]) > (swipeSlopeIntolerance * abs(
                    delX[0]
                )))
                && ((delY[1]) > (swipeSlopeIntolerance * abs(
                    delX[1]
                )))
                && ((delY[2]) > (swipeSlopeIntolerance * abs(
                    delX[2]
                )))
                && ((delY[3]) > (swipeSlopeIntolerance * abs(
                    delX[3]
                )))
            ) {
                return SWIPE_4_DOWN
            }
            if (((-delX[0]) > (swipeSlopeIntolerance * abs(
                    delY[0]
                )))
                && ((-delX[1]) > (swipeSlopeIntolerance * abs(
                    delY[1]
                )))
                && ((-delX[2]) > (swipeSlopeIntolerance * abs(
                    delY[2]
                )))
                && ((-delX[3]) > (swipeSlopeIntolerance * abs(
                    delY[3]
                )))
            ) {
                return SWIPE_4_LEFT
            }
            if (((delX[0]) > (swipeSlopeIntolerance * abs(
                    delY[0]
                )))
                && ((delX[1]) > (swipeSlopeIntolerance * abs(
                    delY[1]
                )))
                && ((delX[2]) > (swipeSlopeIntolerance * abs(
                    delY[2]
                )))
                && ((delX[3]) > (swipeSlopeIntolerance * abs(
                    delY[3]
                )))
            ) {
                return SWIPE_4_RIGHT
            }
            if ((finalFingDist(0, 1) > 1.5 * (initialFingDist(0, 1)))
                && (finalFingDist(1, 2) > 1.5 * (initialFingDist(1, 2)))
                && (finalFingDist(2, 3) > 1.5 * (initialFingDist(2, 3)))
                && (finalFingDist(3, 0) > 1.5 * (initialFingDist(3, 0)))
            ) {
                return UNPINCH_4
            }
            if ((finalFingDist(0, 1) < 0.8 * (initialFingDist(0, 1)))
                && (finalFingDist(1, 2) < 0.8 * (initialFingDist(1, 2)))
                && (finalFingDist(2, 3) < 0.8 * (initialFingDist(2, 3)))
                && (finalFingDist(3, 0) < 0.8 * (initialFingDist(3, 0)))
            ) {
                return PINCH_4
            }
        }
        return 0
    }

    private fun initialFingDist(fingNum1: Int, fingNum2: Int): Double {
        return sqrt(
            (initialX[fingNum1] - initialX[fingNum2]).pow(2.0) + (initialY[fingNum1] - initialY[fingNum2]).pow(
                2.0
            )
        )
    }

    private fun finalFingDist(fingNum1: Int, fingNum2: Int): Double {
        return sqrt(
            (finalX[fingNum1] - finalX[fingNum2]).pow(2.0) + (finalY[fingNum1] - finalY[fingNum2]).pow(
                2.0
            )
        )
    }

    val isDoubleTap: Boolean
        get() = if (initialT - prevFinalT < doubleTapMaxDelayMillis && finalT - initialT < doubleTapMaxDownMillis && prevFinalT - prevInitialT < doubleTapMaxDownMillis) {
            true
        } else {
            false
        }

    inner class GestureType {
        var gestureFlag: Int = 0
        var gestureDuration: Long = 0

        var gestureDistance: Double = 0.0
    }


    companion object {
        const val DEBUG: Boolean = true

        // Finished gestures flags
        const val SWIPE_1_UP: Int = 11
        const val SWIPE_1_DOWN: Int = 12
        const val SWIPE_1_LEFT: Int = 13
        const val SWIPE_1_RIGHT: Int = 14
        const val SWIPE_2_UP: Int = 21
        const val SWIPE_2_DOWN: Int = 22
        const val SWIPE_2_LEFT: Int = 23
        const val SWIPE_2_RIGHT: Int = 24
        const val SWIPE_3_UP: Int = 31
        const val SWIPE_3_DOWN: Int = 32
        const val SWIPE_3_LEFT: Int = 33
        const val SWIPE_3_RIGHT: Int = 34
        const val SWIPE_4_UP: Int = 41
        const val SWIPE_4_DOWN: Int = 42
        const val SWIPE_4_LEFT: Int = 43
        const val SWIPE_4_RIGHT: Int = 44
        const val PINCH_2: Int = 25
        const val UNPINCH_2: Int = 26
        const val PINCH_3: Int = 35
        const val UNPINCH_3: Int = 36
        const val PINCH_4: Int = 45
        const val UNPINCH_4: Int = 46

        const val DOUBLE_TAP_1: Int = 107

        //Ongoing gesture flags
        const val SWIPING_1_UP: Int = 101
        const val SWIPING_1_DOWN: Int = 102
        const val SWIPING_1_LEFT: Int = 103
        const val SWIPING_1_RIGHT: Int = 104
        const val SWIPING_2_UP: Int = 201
        const val SWIPING_2_DOWN: Int = 202
        const val SWIPING_2_LEFT: Int = 203
        const val SWIPING_2_RIGHT: Int = 204
        const val PINCHING: Int = 205
        const val UNPINCHING: Int = 206
        private const val TAG = "GestureAnalyser"
    }
}

class SimpleFingerGestures : OnTouchListener {
    private var debug = true
    var consumeTouchEvents: Boolean = false

    protected var tracking: BooleanArray = booleanArrayOf(false, false, false, false, false)
    private var ga: GestureAnalyser
    private var onFingerGestureListener: OnFingerGestureListener? = null


    /**
     * Constructor that creates an internal [in.championswimmer.sfg.lib.GestureAnalyser] object as well
     */
    constructor() {
        ga = GestureAnalyser()
    }

    constructor(
        swipeSlopeIntolerance: Int,
        doubleTapMaxDelayMillis: Int,
        doubleTapMaxDownMillis: Int
    ) {
        ga = GestureAnalyser(swipeSlopeIntolerance, doubleTapMaxDelayMillis, doubleTapMaxDownMillis)
    }

    fun setDebug(debug: Boolean) {
        this.debug = debug
    }

    constructor(omfgl: OnFingerGestureListener?) {
        ga = GestureAnalyser()
        setOnFingerGestureListener(omfgl)
    }

    /**
     * Register a callback to be invoked when multi-finger gestures take place
     *
     *
     * <br></br>
     *
     *
     * For the callbacks implemented via this, check the interface [in.championswimmer.sfg.lib.SimpleFingerGestures.OnFingerGestureListener]
     *
     *
     * @param omfgl The callback that will run
     */
    fun setOnFingerGestureListener(omfgl: OnFingerGestureListener?) {
        onFingerGestureListener = omfgl
    }


    override fun onTouch(view: View, ev: MotionEvent): Boolean {
        if (debug) Log.d(TAG, "onTouch")
        when (ev.action and MotionEvent.ACTION_MASK) {
            MotionEvent.ACTION_DOWN -> {
                if (debug) Log.d(TAG, "ACTION_DOWN")
                startTracking(0)
                ga.trackGesture(ev)
                return consumeTouchEvents
            }

            MotionEvent.ACTION_UP -> {
                if (debug) Log.d(TAG, "ACTION_UP")
                if (tracking[0]) {
                    doCallBack(ga.getGesture(ev))
                }
                stopTracking(0)
                ga.untrackGesture()
                return consumeTouchEvents
            }

            MotionEvent.ACTION_POINTER_DOWN -> {
                if (debug) Log.d(TAG, "ACTION_POINTER_DOWN" + " " + "num" + ev.pointerCount)
                startTracking(ev.pointerCount - 1)
                ga.trackGesture(ev)
                return consumeTouchEvents
            }

            MotionEvent.ACTION_POINTER_UP -> {
                if (debug) Log.d(TAG, "ACTION_POINTER_UP" + " " + "num" + ev.pointerCount)
                if (tracking[1]) {
                    doCallBack(ga.getGesture(ev))
                }
                stopTracking(ev.pointerCount - 1)
                ga.untrackGesture()
                return consumeTouchEvents
            }

            MotionEvent.ACTION_CANCEL -> {
                if (debug) Log.d(TAG, "ACTION_CANCEL")
                return true
            }

            MotionEvent.ACTION_MOVE -> {
                if (debug) Log.d(TAG, "ACTION_MOVE")
                return consumeTouchEvents
            }
        }
        return consumeTouchEvents
    }

    private fun doCallBack(mGt: GestureType) {
        when (mGt.gestureFlag) {
            GestureAnalyser.SWIPE_1_UP -> onFingerGestureListener!!.onSwipeUp(
                1,
                mGt.gestureDuration,
                mGt.gestureDistance
            )

            GestureAnalyser.SWIPE_1_DOWN -> onFingerGestureListener!!.onSwipeDown(
                1,
                mGt.gestureDuration,
                mGt.gestureDistance
            )

            GestureAnalyser.SWIPE_1_LEFT -> onFingerGestureListener!!.onSwipeLeft(
                1,
                mGt.gestureDuration,
                mGt.gestureDistance
            )

            GestureAnalyser.SWIPE_1_RIGHT -> onFingerGestureListener!!.onSwipeRight(
                1,
                mGt.gestureDuration,
                mGt.gestureDistance
            )

            GestureAnalyser.SWIPE_2_UP -> onFingerGestureListener!!.onSwipeUp(
                2,
                mGt.gestureDuration,
                mGt.gestureDistance
            )

            GestureAnalyser.SWIPE_2_DOWN -> onFingerGestureListener!!.onSwipeDown(
                2,
                mGt.gestureDuration,
                mGt.gestureDistance
            )

            GestureAnalyser.SWIPE_2_LEFT -> onFingerGestureListener!!.onSwipeLeft(
                2,
                mGt.gestureDuration,
                mGt.gestureDistance
            )

            GestureAnalyser.SWIPE_2_RIGHT -> onFingerGestureListener!!.onSwipeRight(
                2,
                mGt.gestureDuration,
                mGt.gestureDistance
            )

            GestureAnalyser.PINCH_2 -> onFingerGestureListener!!.onPinch(
                2,
                mGt.gestureDuration,
                mGt.gestureDistance
            )

            GestureAnalyser.UNPINCH_2 -> onFingerGestureListener!!.onUnpinch(
                2,
                mGt.gestureDuration,
                mGt.gestureDistance
            )

            GestureAnalyser.SWIPE_3_UP -> onFingerGestureListener!!.onSwipeUp(
                3,
                mGt.gestureDuration,
                mGt.gestureDistance
            )

            GestureAnalyser.SWIPE_3_DOWN -> onFingerGestureListener!!.onSwipeDown(
                3,
                mGt.gestureDuration,
                mGt.gestureDistance
            )

            GestureAnalyser.SWIPE_3_LEFT -> onFingerGestureListener!!.onSwipeLeft(
                3,
                mGt.gestureDuration,
                mGt.gestureDistance
            )

            GestureAnalyser.SWIPE_3_RIGHT -> onFingerGestureListener!!.onSwipeRight(
                3,
                mGt.gestureDuration,
                mGt.gestureDistance
            )

            GestureAnalyser.PINCH_3 -> onFingerGestureListener!!.onPinch(
                3,
                mGt.gestureDuration,
                mGt.gestureDistance
            )

            GestureAnalyser.UNPINCH_3 -> onFingerGestureListener!!.onUnpinch(
                3,
                mGt.gestureDuration,
                mGt.gestureDistance
            )

            GestureAnalyser.SWIPE_4_UP -> onFingerGestureListener!!.onSwipeUp(
                4,
                mGt.gestureDuration,
                mGt.gestureDistance
            )

            GestureAnalyser.SWIPE_4_DOWN -> onFingerGestureListener!!.onSwipeDown(
                4,
                mGt.gestureDuration,
                mGt.gestureDistance
            )

            GestureAnalyser.SWIPE_4_LEFT -> onFingerGestureListener!!.onSwipeLeft(
                4,
                mGt.gestureDuration,
                mGt.gestureDistance
            )

            GestureAnalyser.SWIPE_4_RIGHT -> onFingerGestureListener!!.onSwipeRight(
                4,
                mGt.gestureDuration,
                mGt.gestureDistance
            )

            GestureAnalyser.PINCH_4 -> onFingerGestureListener!!.onPinch(
                4,
                mGt.gestureDuration,
                mGt.gestureDistance
            )

            GestureAnalyser.UNPINCH_4 -> {
                onFingerGestureListener!!.onUnpinch(4, mGt.gestureDuration, mGt.gestureDistance)
                onFingerGestureListener!!.onDoubleTap(1)
            }

            GestureAnalyser.DOUBLE_TAP_1 -> onFingerGestureListener!!.onDoubleTap(1)
        }
    }

    private fun startTracking(nthPointer: Int) {
        for (i in 0..nthPointer) {
            tracking[i] = true
        }
    }

    private fun stopTracking(nthPointer: Int) {
        for (i in nthPointer until tracking.size) {
            tracking[i] = false
        }
    }


    /**
     * Interface definition for the callback to be invoked when 2-finger gestures are performed
     */
    interface OnFingerGestureListener {
        /**
         * Called when user swipes **up** with two fingers
         *
         * @param fingers         number of fingers involved in this gesture
         * @param gestureDuration duration in milliSeconds
         * @return
         */
        fun onSwipeUp(fingers: Int, gestureDuration: Long, gestureDistance: Double): Boolean

        /**
         * Called when user swipes **down** with two fingers
         *
         * @param fingers         number of fingers involved in this gesture
         * @param gestureDuration duration in milliSeconds
         * @return
         */
        fun onSwipeDown(fingers: Int, gestureDuration: Long, gestureDistance: Double): Boolean

        /**
         * Called when user swipes **left** with two fingers
         *
         * @param fingers         number of fingers involved in this gesture
         * @param gestureDuration duration in milliSeconds
         * @return
         */
        fun onSwipeLeft(fingers: Int, gestureDuration: Long, gestureDistance: Double): Boolean

        /**
         * Called when user swipes **right** with two fingers
         *
         * @param fingers         number of fingers involved in this gesture
         * @param gestureDuration duration in milliSeconds
         * @return
         */
        fun onSwipeRight(fingers: Int, gestureDuration: Long, gestureDistance: Double): Boolean

        /**
         * Called when user **pinches** with two fingers (bring together)
         *
         * @param fingers         number of fingers involved in this gesture
         * @param gestureDuration duration in milliSeconds
         * @return
         */
        fun onPinch(fingers: Int, gestureDuration: Long, gestureDistance: Double): Boolean

        /**
         * Called when user **un-pinches** with two fingers (take apart)
         *
         * @param fingers         number of fingers involved in this gesture
         * @param gestureDuration duration in milliSeconds
         * @return
         */
        fun onUnpinch(fingers: Int, gestureDuration: Long, gestureDistance: Double): Boolean

        fun onDoubleTap(fingers: Int): Boolean
    }

    companion object {
        // Will see if these need to be used. For now just returning duration in milliS
        const val GESTURE_SPEED_SLOW: Long = 1500
        const val GESTURE_SPEED_MEDIUM: Long = 1000
        const val GESTURE_SPEED_FAST: Long = 500
        private const val TAG = "SimpleFingerGestures"
    }
}

//abstract class OnSwipeTouchListener(val context: Context?) : View.OnTouchListener {
//    companion object {
//        private const val SWIPE_DISTANCE_THRESHOLD = 100
//        private const val SWIPE_VELOCITY_THRESHOLD = 100
//    }
//    private val gestureDetector: GestureDetector
//    abstract fun onSwipeLeft()
//    abstract fun onSwipeRight()
//    abstract fun onSwipeDown()
//    abstract fun onSwipeUp()
//    abstract fun onSingleTap(area : TouchArea)
//    abstract fun onDoubleTap(area : TouchArea)
//
//    override fun onTouch(v: View?, event: MotionEvent?): Boolean {
//        if (event == null) {
//            return false
//        }
//        return gestureDetector.onTouchEvent(event!!)
//    }
//
//
//    private inner class GestureListener : GestureDetector.SimpleOnGestureListener() {
//
//        override fun onDown(e: MotionEvent): Boolean {
//            return true
//        }
//
//        override fun onDoubleTapEvent(e: MotionEvent): Boolean {
//            val width: Int = context?.resources?.displayMetrics?.widthPixels ?: 0
//            val height: Int = context?.resources?.displayMetrics?.heightPixels ?: 0
//            var touchArea : TouchArea = TouchArea.Center
//            if(width > 0 && height > 0) {
//                val centerAreaSize = width * 0.4
//                var sideAreaSize = (width - centerAreaSize) * 0.5
//                if(e.x < sideAreaSize) {
//                    touchArea = TouchArea.DoubleLeft
//                } else if(e.x > sideAreaSize && e.x < width - sideAreaSize) {
//
//                } else {
//                    touchArea = TouchArea.DoubleRight
//                }
//            }
//            onDoubleTap(touchArea)
//            return super.onDoubleTapEvent(e)
//        }
//
//        override fun onSingleTapConfirmed(e: MotionEvent): Boolean {
//            val width: Int = context?.resources?.displayMetrics?.widthPixels ?: 0
//            val height: Int = context?.resources?.displayMetrics?.heightPixels ?: 0
//            var touchArea : TouchArea = TouchArea.Center
//            if(width > 0 && height > 0) {
//                val centerAreaSize = width * 0.4
//                var sideAreaSize = (width - centerAreaSize) * 0.5
//                if(e.x < sideAreaSize) {
//                    touchArea = TouchArea.Left
//                } else if(e.x > sideAreaSize && e.x < width - sideAreaSize) {
//
//                } else {
//                    touchArea = TouchArea.Right
//                }
//            }
//            onSingleTap(touchArea)
//            return super.onSingleTapConfirmed(e)
//        }
//
////        override fun onSingleTapUp(e: MotionEvent): Boolean {
////
////            return super.onSingleTapUp(e)
////        }
//
//        override fun onFling(
//            e1: MotionEvent?,
//            e2: MotionEvent,
//            velocityX: Float,
//            velocityY: Float
//        ): Boolean {
//Blog.LOGE("e1.pointerCount >> ${e1?.pointerCount}")
//            Blog.LOGE("e2.pointerCount >> ${e2.pointerCount}")
//            val distanceX = e2.x - (e1?.x ?: 0f)
//            val distanceY = e2.y - (e1?.y ?: 0f)
//            if (Math.abs(distanceX) > Math.abs(distanceY)
//                && Math.abs(distanceX) > SWIPE_DISTANCE_THRESHOLD
//                && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
//                if (distanceX > 0) onSwipeRight() else onSwipeLeft()
//                return true
//            } else if (Math.abs(distanceY) > Math.abs(distanceX)
//                && Math.abs(distanceY) > SWIPE_DISTANCE_THRESHOLD
//                && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
//                if (distanceY > 0) onSwipeDown() else onSwipeUp()
//                return true
//            } else {
//
//            }
//            return false
//        }
//    }
//    init {
//        gestureDetector = GestureDetector(context, GestureListener())
//    }
//}