package com.adealink.weparty.commonui.widget.floatview.view

import android.view.MotionEvent
import com.adealink.frame.log.Log
import com.adealink.frame.util.removeUiCallbacks
import com.adealink.frame.util.runOnUiThread
import com.adealink.weparty.commonui.widget.floatview.data.ILayoutFloatData
import com.adealink.weparty.commonui.widget.slide.Slide
import com.adealink.weparty.commonui.widget.slide.SlideBuilder

abstract class BaseSlideFloatView(baseFloatData: ILayoutFloatData) : BaseLayoutFloatView(baseFloatData) {

    companion object {
        private const val TAG = "BaseSlideUpFloatView"

        private const val DISMISS_PERCENT = 10f

        private const val MAX_ALPHA = 0.8f

        @JvmStatic
        private fun getAlphaBy(percent: Float): Float {
            return if (percent > DISMISS_PERCENT) {
                1f - MAX_ALPHA * (percent - DISMISS_PERCENT) / (100f - DISMISS_PERCENT)
            } else {
                1f
            }
        }
    }

    abstract fun slideDirection(): Slide.SlideDirection

    private val slide: Slide = SlideBuilder(this@BaseSlideFloatView)
        .slideToParent()
        .slideDirection(slideDirection()) //支持左右滑动
        .autoSlideToEndPercent(DISMISS_PERCENT)
        .listeners(object : Slide.Listener {
            override fun onSlide(percent: Float) {
                //Log.d(TAG, "onSlide, percent:$percent")
                alpha = getAlphaBy(percent)
                this@BaseSlideFloatView.onSlide(percent)
            }

            override fun onSlideFinish(slideToEnd: Boolean) {
                Log.d(TAG, "onSlideFinish, slideToEnd:$slideToEnd")
                if (slideToEnd) {
                    removeSelf("Slide To End")
                } else {
                    alpha = 1f
                }
                this@BaseSlideFloatView.onSlideFinish(slideToEnd)
            }
        })
        .build()

    //自动消失
    open var autoDismiss: Boolean = false

    open var autoDismissDelayTs: Long = 3_000 //3秒后自动消失

    private val autoDismissRunnable = Runnable {
        removeSelf("Auto Dismiss")
    }

    override fun onAttachedToWindow() {
        super.onAttachedToWindow()
        Log.d(TAG, "onAttachedToWindow, autoDismiss:$autoDismiss")
        if (autoDismiss) {
            removeCallbacks(autoDismissRunnable)
            runOnUiThread(autoDismissRunnable, autoDismissDelayTs)
            Log.d(TAG, "onAttachedToWindow, autoDismiss after $autoDismissDelayTs ms")
        }
    }

    override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()
        Log.d(TAG, "onDetachedFromWindow, autoDismiss:$autoDismiss")
        if (autoDismiss) {
            removeUiCallbacks(autoDismissRunnable)
        }
    }

    override fun onInterceptTouchEvent(ev: MotionEvent?): Boolean {
        return true
    }

    override fun onTouchEvent(event: MotionEvent?): Boolean {
        if (event != null) {
            slide.onTouch(this, event)
        }
        return true
    }

    open fun onSlide(percent: Float) {
    }

    open fun onSlideFinish(slideToEnd: Boolean) {
    }

}