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 WeightSelectionActivity : AppCompatActivity() {
    private lateinit var selectedWeightText: TextView
    private lateinit var weight1Above: TextView
    private lateinit var weight2Above: TextView
    private lateinit var weight1Below: TextView
    private lateinit var weight2Below: TextView
    private lateinit var nextButton: MaterialButton
    private lateinit var backButton: ImageButton
    
    private var selectedGender: String? = null
    private var selectedAge: Int = 0
    private var currentWeight = 54
    
    private var lastY: Float = 0f
    private val scrollSensitivity = 15f // 调整这个值可以改变滑动灵敏度
    private val minWeight = 30
    private val maxWeight = 200

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

        // 获取从上一个页面传递的数据
        selectedGender = intent.getStringExtra("selected_gender")
        selectedAge = intent.getIntExtra("selected_age", 0)
        
        // 初始化视图
        selectedWeightText = findViewById(R.id.selectedWeightText)
        weight1Above = findViewById(R.id.weight1Above)
        weight2Above = findViewById(R.id.weight2Above)
        weight1Below = findViewById(R.id.weight1Below)
        weight2Below = findViewById(R.id.weight2Below)
        nextButton = findViewById(R.id.nextButton)
        backButton = findViewById(R.id.backButton)
        
        setupUI()
        setupClickListeners()
    }

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

    private fun updateWeightDisplay() {
        selectedWeightText.text = currentWeight.toString()
        
        // 更新上方体重（显示较小的数字）
        if (currentWeight - 1 >= minWeight) {
            weight1Above.text = (currentWeight - 1).toString()
        } else {
            weight1Above.text = ""
        }
        if (currentWeight - 2 >= minWeight) {
            weight2Above.text = (currentWeight - 2).toString()
        } else {
            weight2Above.text = ""
        }
        
        // 更新下方体重（显示较大的数字）
        if (currentWeight + 1 <= maxWeight) {
            weight1Below.text = (currentWeight + 1).toString()
        } else {
            weight1Below.text = ""
        }
        if (currentWeight + 2 <= maxWeight) {
            weight2Below.text = (currentWeight + 2).toString()
        } else {
            weight2Below.text = ""
        }
    }

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

        backButton.setOnClickListener {
            val intent = Intent(this, AgeSelectionActivity::class.java)
            intent.putExtra("selected_gender", selectedGender)
            startActivity(intent)
            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 newWeight = (currentWeight + change).coerceIn(minWeight, maxWeight)
                    if (newWeight != currentWeight) {
                        currentWeight = newWeight
                        updateWeightDisplay()
                        lastY = currentY
                    }
                }
                return true
            }
        }
        return super.onTouchEvent(event)
    }
} 