package com.jishisongfu.duobibi.floating

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Color
import android.graphics.drawable.GradientDrawable
import android.view.MotionEvent
import android.view.View
import android.view.WindowManager
import android.widget.FrameLayout
import android.widget.ProgressBar
import android.widget.TextView
import com.jishisongfu.duobibi.util.SettingsPrefs
import kotlin.math.abs

/**
 * 比价浮窗的视图实体 —— 圆形按钮 + 内置 ProgressBar 切换 Loading 态。支持拖动并记忆位置。
 * 多比比 蓝青科技风:按钮底色 Teal #0EA5A4。
 */
class FloatingButtonView(
    context: Context,
    private val windowManager: WindowManager,
    private val params: WindowManager.LayoutParams,
    private val onClick: () -> Unit,
) : FrameLayout(context) {

    private val label: TextView = TextView(context).apply {
        text = "比价"
        setTextColor(Color.WHITE)
        textSize = 12f
        gravity = android.view.Gravity.CENTER
    }

    private val loading: ProgressBar = ProgressBar(context).apply {
        isIndeterminate = true
        visibility = View.GONE
    }

    private var isDragging = false
    private var downRawX = 0f
    private var downRawY = 0f
    private var downX = 0
    private var downY = 0

    init {
        background = GradientDrawable().apply {
            shape = GradientDrawable.OVAL
            setColor(Color.parseColor("#0EA5A4"))
            setStroke(dp(2), Color.WHITE)
        }
        addView(label, LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT))
        addView(loading, LayoutParams(dp(28), dp(28), android.view.Gravity.CENTER))
    }

    @SuppressLint("ClickableViewAccessibility")
    override fun onTouchEvent(event: MotionEvent): Boolean {
        when (event.action) {
            MotionEvent.ACTION_DOWN -> {
                isDragging = false
                downRawX = event.rawX
                downRawY = event.rawY
                downX = params.x
                downY = params.y
                return true
            }
            MotionEvent.ACTION_MOVE -> {
                val dx = (event.rawX - downRawX).toInt()
                val dy = (event.rawY - downRawY).toInt()
                if (!isDragging && (abs(dx) > TOUCH_SLOP || abs(dy) > TOUCH_SLOP)) {
                    isDragging = true
                }
                if (isDragging) {
                    params.x = downX + dx
                    params.y = downY + dy
                    runCatching { windowManager.updateViewLayout(this, params) }
                }
                return true
            }
            MotionEvent.ACTION_UP -> {
                if (!isDragging) {
                    onClick()
                } else {
                    SettingsPrefs.savePosition(context, params.x, params.y)
                }
                return true
            }
        }
        return super.onTouchEvent(event)
    }

    fun setLoading(loading: Boolean) {
        post {
            this.loading.visibility = if (loading) View.VISIBLE else View.GONE
            this.label.visibility = if (loading) View.GONE else View.VISIBLE
        }
    }

    private fun dp(v: Int): Int = (resources.displayMetrics.density * v).toInt()

    companion object {
        private const val TOUCH_SLOP = 12
    }
}
