package org.tensorflow.lite.examples.poseestimation

import android.content.Intent
import android.os.Bundle
import android.view.MotionEvent
import android.widget.ImageButton
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.google.android.material.button.MaterialButton
import kotlin.math.abs

class AgeSelectionActivity : AppCompatActivity() {
    private lateinit var selectedAgeText: TextView
    private lateinit var age1Above: TextView
    private lateinit var age2Above: TextView
    private lateinit var age1Below: TextView
    private lateinit var age2Below: TextView
    private lateinit var nextButton: MaterialButton
    private lateinit var backButton: ImageButton
    private var selectedGender: String? = null
    
    private var currentAge = 25
    private val minAge = 12
    private val maxAge = 90
    
    private var lastY: Float = 0f
    private val scrollSensitivity = 15f // 调整这个值可以改变滑动灵敏度

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_age_selection)

        selectedGender = intent.getStringExtra("selected_gender")
        
        selectedAgeText = findViewById(R.id.selectedAgeText)
        age1Above = findViewById(R.id.age1Above)
        age2Above = findViewById(R.id.age2Above)
        age1Below = findViewById(R.id.age1Below)
        age2Below = findViewById(R.id.age2Below)
        nextButton = findViewById(R.id.nextButton)
        backButton = findViewById(R.id.backButton)
        
        setupUI()
        setupClickListeners()
    }

    private fun setupUI() {
        updateAgeDisplay()
        nextButton.isEnabled = true
    }

    private fun updateAgeDisplay() {
        selectedAgeText.text = currentAge.toString()
        
        // 更新上方年龄（显示较小的数字）
        if (currentAge - 1 >= minAge) {
            age1Above.text = (currentAge - 1).toString()
        } else {
            age1Above.text = ""
        }
        if (currentAge - 2 >= minAge) {
            age2Above.text = (currentAge - 2).toString()
        } else {
            age2Above.text = ""
        }
        
        // 更新下方年龄（显示较大的数字）
        if (currentAge + 1 <= maxAge) {
            age1Below.text = (currentAge + 1).toString()
        } else {
            age1Below.text = ""
        }
        if (currentAge + 2 <= maxAge) {
            age2Below.text = (currentAge + 2).toString()
        } else {
            age2Below.text = ""
        }
    }

    private fun setupClickListeners() {
        nextButton.setOnClickListener {
            val intent = Intent(this, WeightSelectionActivity::class.java)
            intent.putExtra("selected_gender", selectedGender)
            intent.putExtra("selected_age", currentAge)
            startActivity(intent)
            finish()
        }

        backButton.setOnClickListener {
            finish()
        }
    }

    override fun onTouchEvent(event: MotionEvent): Boolean {
        when (event.action) {
            MotionEvent.ACTION_DOWN -> {
                lastY = event.y
                return true
            }
            MotionEvent.ACTION_MOVE -> {
                val currentY = event.y
                val deltaY = currentY - lastY
                
                // 计算应该改变多少年龄
                val change = -(deltaY / scrollSensitivity).toInt()
                if (abs(change) > 0) {
                    // 更新年龄
                    val newAge = (currentAge + change).coerceIn(minAge, maxAge)
                    if (newAge != currentAge) {
                        currentAge = newAge
                        updateAgeDisplay()
                        lastY = currentY
                    }
                }
                return true
            }
        }
        return super.onTouchEvent(event)
    }
} 