package com.gh.gamecenter.common.base.fragment

import android.annotation.SuppressLint
import android.app.Dialog
import android.os.Bundle
import android.view.*
import com.gh.gamecenter.common.HaloApp
import com.gh.gamecenter.common.R

abstract class BaseDraggableDialogFragment : BaseDialogFragment(), View.OnTouchListener {
    private var mInitPositionY = 0f
    private lateinit var mGestureDetector: GestureDetector
    private lateinit var mRootView: View
    private var mDragCloseView: View? = null

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        mRootView = getRootView()
        mDragCloseView = getDragCloseView()
        mDragCloseView?.setOnTouchListener(this)
        mGestureDetector = GestureDetector(requireContext(), SingleTapConfirm())
    }

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val createDialog = super.onCreateDialog(savedInstanceState)
        createDialog.setCanceledOnTouchOutside(true)

        val window = createDialog.window
        window?.setGravity(Gravity.BOTTOM)
        window?.setWindowAnimations(R.style.community_publication_animation)
        return createDialog
    }

    // dialog drag animation
    @SuppressLint("ClickableViewAccessibility")
    override fun onTouch(v: View, event: MotionEvent): Boolean {
        if (mGestureDetector.onTouchEvent(event) && mRootView.y == 0F) {
            v.performClick()
            return true
        }
        when (event.action) {
            MotionEvent.ACTION_DOWN -> {
                mInitPositionY = mRootView.y - event.rawY
            }
            MotionEvent.ACTION_MOVE -> {
                val offsetY = event.rawY + mInitPositionY
                val dialogY = mRootView.y
                if (dialogY + offsetY > 0) {
                    mRootView.animate()
                        .y(offsetY)
                        .setDuration(0)
                        .start()
                } else {
                    resetDialogPosition()
                }
            }
            MotionEvent.ACTION_CANCEL,
            MotionEvent.ACTION_UP,
            MotionEvent.ACTION_OUTSIDE -> {
                if (mRootView.y >= mRootView.height / 2) {
                    dismissAllowingStateLoss()
                } else {
                    resetDialogPosition(300)
                }
            }
            else -> return false
        }
        return true
    }

    private fun resetDialogPosition(duration: Long = 0) {
        mRootView.animate()
            .y(0F)
            .setDuration(duration)
            .start()
    }

    private class SingleTapConfirm : GestureDetector.SimpleOnGestureListener() {
        override fun onSingleTapUp(event: MotionEvent): Boolean {
            return true
        }
    }

    override fun onStart() {
        super.onStart()
        val width = HaloApp.getInstance().resources.displayMetrics.widthPixels
        val height = dialog?.window?.attributes?.height ?: ViewGroup.LayoutParams.WRAP_CONTENT
        dialog?.window?.setLayout(width, height)
    }

    abstract fun getRootView(): View
    abstract fun getDragCloseView(): View?
    override fun getThemeRes(): Int = R.style.DialogFragmentDimAmount
}