package com.gh.gamecenter.hud

import android.app.Activity
import android.view.MotionEvent
import android.view.View
import android.widget.TextView

import com.gh.gamecenter.R
import com.gh.gamecenter.feature.view.GameIconView

import com.lzf.easyfloat.EasyFloat
import com.lzf.easyfloat.anim.DefaultAnimator
import com.lzf.easyfloat.enums.SidePattern

object HeadUpDisplayHelper {

    private const val HUD_WINDOW = "hud_window"
    private const val AUTO_DISMISS_TIME = 5 * 1000L

    private val dismissRunnable = Runnable { EasyFloat.dismiss(HUD_WINDOW) }

    /**
     * Show HUD
     */
    fun showHUD(
        activity: Activity,
        imageUrl: String,
        title: String,
        desc: String,
        showingCallback:(() -> Unit),
        clickedCallback: (() -> Unit),
        dismissCallback: (() -> Unit)? = null
    ) {
        val floatingView = EasyFloat.getFloatView(HUD_WINDOW)

        if (floatingView != null) {
            return
        }

        EasyFloat.with(activity)
            .setLayout(R.layout.layout_hud)
            .setMatchParent(widthMatch = true, heightMatch = false)
            .setSidePattern(SidePattern.TOP)
            .setAnimator(DefaultAnimator())
            .setImmersionStatusBar(true)
            .setTag(HUD_WINDOW)
            .registerCallback {
                createResult { _, _, view ->
                    view?.let { initView(it, imageUrl, title, desc, clickedCallback) }
                    showingCallback.invoke()
                }
                dismiss {
                    dismissCallback?.invoke()
                }
            }
            .show()
    }

    private fun initView(
        view: View,
        imageUrl: String,
        title: String,
        desc: String,
        clickedCallback: (() -> Unit)? = null
    ) {
        view.findViewById<GameIconView>(R.id.gameIv)?.displayGameIcon(icon = imageUrl, null, null)
        view.findViewById<TextView>(R.id.titleTv)?.text = title
        view.findViewById<TextView>(R.id.descTv)?.text = desc
        view.postDelayed(dismissRunnable, AUTO_DISMISS_TIME)

        view.findViewById<TextView>(R.id.btn)?.setOnClickListener {
            clickedCallback?.invoke()
            EasyFloat.dismiss(HUD_WINDOW)
        }

        view.rootView.setOnTouchListener(object : View.OnTouchListener {
            var actionDownRawY = 0F

            override fun onTouch(rootView: View?, event: MotionEvent?): Boolean {
                if (rootView == null) return false

                when (event?.action) {
                    MotionEvent.ACTION_DOWN -> {
                        actionDownRawY = rootView.y - event.rawY
                    }

                    MotionEvent.ACTION_MOVE -> {
                        val offsetY = event.rawY + actionDownRawY
                        val dialogY = rootView.y
                        if (dialogY + offsetY < 0) {
                            rootView.animate()
                                .y(offsetY)
                                .setDuration(0)
                                .start()
                        } else {
                            rootView.animate()
                                .y(0F)
                                .setDuration(0)
                                .start()
                        }
                    }

                    MotionEvent.ACTION_CANCEL,
                    MotionEvent.ACTION_UP,
                    MotionEvent.ACTION_OUTSIDE -> {
                        if (rootView.y <= -rootView.height / 8) {
                            EasyFloat.dismiss(HUD_WINDOW)
                        } else {
                            rootView.animate()
                                .y(0F)
                                .setDuration(300)
                                .start()
                        }
                    }

                    else -> return false
                }
                return false
            }
        })
    }

}