package org.tensorflow.lite.examples.poseestimation

import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Path
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View
import androidx.core.content.ContextCompat
import kotlin.math.abs
import kotlin.math.max
import kotlin.math.min

class CustomWeightPicker @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {

    private val paint = Paint().apply {
        isAntiAlias = true
        color = ContextCompat.getColor(context, R.color.purple_500)
    }

    private val path = Path()
    private var lastX = 0f
    private var scrollOffset = 0f
    private var selectedWeight = 54
    private val minWeight = 30
    private val maxWeight = 200
    private val scaleWidth = 40f  // 每个刻度的宽度
    private val scaleHeight = 20f // 刻度的高度
    private val centerLineWidth = 2f

    private var onWeightChangeListener: ((Int) -> Unit)? = null

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        
        val centerX = width / 2f
        val centerY = height / 2f
        
        // 绘制刻度
        val startX = centerX + scrollOffset
        val startWeight = selectedWeight - (centerX / scaleWidth).toInt()
        
        for (i in -10..10) {
            val x = startX + i * scaleWidth
            val weight = startWeight + i
            
            if (weight in minWeight..maxWeight) {
                // 绘制刻度线
                canvas.drawLine(
                    x,
                    centerY - scaleHeight,
                    x,
                    centerY + scaleHeight,
                    paint
                )
                
                // 绘制刻度值
                if (i % 2 == 0) {
                    canvas.drawText(
                        weight.toString(),
                        x - 10f,
                        centerY + scaleHeight + 20f,
                        paint
                    )
                }
            }
        }
    }

    override fun onTouchEvent(event: MotionEvent): Boolean {
        when (event.action) {
            MotionEvent.ACTION_DOWN -> {
                lastX = event.x
                return true
            }
            MotionEvent.ACTION_MOVE -> {
                val deltaX = event.x - lastX
                scrollOffset += deltaX
                
                // 计算新的体重值
                val newWeight = selectedWeight - (deltaX / scaleWidth).toInt()
                if (newWeight in minWeight..maxWeight) {
                    selectedWeight = newWeight
                    onWeightChangeListener?.invoke(selectedWeight)
                }
                
                lastX = event.x
                invalidate()
                return true
            }
        }
        return super.onTouchEvent(event)
    }

    fun setOnWeightChangeListener(listener: (Int) -> Unit) {
        onWeightChangeListener = listener
    }

    fun getSelectedWeight(): Int = selectedWeight
} 