package com.kdanmobile.reader.test

import android.content.Context
import android.graphics.PointF
import android.view.MotionEvent
import com.kdanmobile.kmpdfkit.globaldata.KMPDFAnnotEditMode
import com.kdanmobile.kmpdfkit.pdfcommon.KMPDFReaderView

open class MyReaderView(context: Context, private val adPageHelper: AdPageHelper? = null) : KMPDFReaderView(context) {

    companion object {
        private const val TOUCH_MOVE_DISTANCE_TOLERATION = 20f
    }

    private val lastTouchPointF = PointF()
    private var touchMoveDistance = 0f
    private var isJustTouch = false

    override fun onScroll(e1: MotionEvent, e2: MotionEvent, distanceX: Float, distanceY: Float): Boolean {
        if (mMode == Mode.Viewing && isJustTouch) {
            isJustTouch = false
            return false
        }
        return super.onScroll(e1, e2, distanceX, distanceY)
    }

    override fun onInterceptTouchEvent(motionEvent: MotionEvent): Boolean {
        val intercept = if (mMode == Mode.Viewing) {
            if (motionEvent.action == MotionEvent.ACTION_DOWN) {
                isJustTouch = true
                lastTouchPointF.set(motionEvent.x, motionEvent.y)
                touchMoveDistance = 0f
            }
            touchMoveDistance += Math.abs(motionEvent.x - lastTouchPointF.x) + Math.abs(motionEvent.y - lastTouchPointF.y)
            lastTouchPointF.set(motionEvent.x, motionEvent.y)

            touchMoveDistance >= TOUCH_MOVE_DISTANCE_TOLERATION && motionEvent.action == MotionEvent.ACTION_MOVE
        } else {
            true
        }
        return intercept && super.onInterceptTouchEvent(motionEvent)
    }

//    override fun onInterceptTouchEvent(motionEvent: MotionEvent): Boolean {
//        return false
//    }

    override fun getDisplayedViewIndex(): Int {
//        return when (!isCreateMode() && !isModifyMode() && !mLinksEnabled || Config.cropMode) {
//            true -> mCurrent
//            false -> adPageHelper?.convertToRawPageIndex(mCurrent) ?: 0
//        }
        return adPageHelper?.convertToRawPageIndex(mCurrent) ?: 0
    }

    override fun setDisplayedViewIndex(i: Int, isRecordLastJumpPageNum: Boolean) {
        super.setDisplayedViewIndex(adPageHelper?.convertToPageIndex(i) ?: 0, isRecordLastJumpPageNum)
//        val page = when (!isCreateMode() && !isModifyMode() && !mLinksEnabled || Config.cropMode) {
//            false -> i
//            true -> adPageHelper?.convertToPageIndex(i) ?: 0
//        }
//        super.setDisplayedViewIndex(page, isRecordLastJumpPageNum)
    }

    override fun refresh(reflow: Boolean) {
//        val page = adPageHelper?.convertToRawPageIndex(mCurrent) ?: 0
//        super.refresh(reflow)
//        super.setDisplayedViewIndex(adPageHelper?.convertToPageIndex(page)?: 0, false)
        val page = mCurrent
        super.refresh(reflow)
        super.setDisplayedViewIndex(page, false)
    }

    private fun isEditMode(): Boolean {
        return kmpdfFactory.kmpdfAnnotEditMode.pdfAnnotEditMode.run {
            this != KMPDFAnnotEditMode.Mode.FREETEXT_CREATE &&
                    this != KMPDFAnnotEditMode.Mode.FREETEXT_MODIFY &&
                    this != KMPDFAnnotEditMode.Mode.SHAPE_CREATE &&
                    this != KMPDFAnnotEditMode.Mode.SHAPE_MODIFY &&
                    this != KMPDFAnnotEditMode.Mode.LINK_CREATE &&
                    this != KMPDFAnnotEditMode.Mode.STAMP_MODIFY &&
                    this != KMPDFAnnotEditMode.Mode.STAMP_CREATE &&
                    this != KMPDFAnnotEditMode.Mode.FORMTEXT_MODIFY &&
                    this != KMPDFAnnotEditMode.Mode.SIGN_CREATE &&
                    this != KMPDFAnnotEditMode.Mode.SIGN_MODIFY &&
                    this != KMPDFAnnotEditMode.Mode.LINK_LONGPRESS_CREATE &&
                    this != KMPDFAnnotEditMode.Mode.TEXT_CREATE &&
                    this != KMPDFAnnotEditMode.Mode.TEXT_MODIFY
        }
    }

    private fun isCreateMode(): Boolean {
        return kmpdfFactory.kmpdfAnnotEditMode.pdfAnnotEditMode.run {
            this == KMPDFAnnotEditMode.Mode.FREETEXT_CREATE ||
                    this == KMPDFAnnotEditMode.Mode.SHAPE_CREATE ||
                    this == KMPDFAnnotEditMode.Mode.LINK_CREATE ||
                    this == KMPDFAnnotEditMode.Mode.STAMP_CREATE ||
                    this == KMPDFAnnotEditMode.Mode.SIGN_CREATE ||
                    this == KMPDFAnnotEditMode.Mode.LINK_LONGPRESS_CREATE ||
                    this == KMPDFAnnotEditMode.Mode.TEXT_CREATE
        }
    }

    private fun isModifyMode(): Boolean {
        return kmpdfFactory.kmpdfAnnotEditMode.pdfAnnotEditMode.run {
            this == KMPDFAnnotEditMode.Mode.FREETEXT_MODIFY ||
                    this == KMPDFAnnotEditMode.Mode.SHAPE_MODIFY ||
                    this == KMPDFAnnotEditMode.Mode.STAMP_MODIFY ||
                    this == KMPDFAnnotEditMode.Mode.FORMTEXT_MODIFY ||
                    this == KMPDFAnnotEditMode.Mode.SIGN_MODIFY ||
                    this == KMPDFAnnotEditMode.Mode.TEXT_MODIFY
        }
    }
}