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.LinearLayout
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.google.android.material.button.MaterialButton
import kotlin.math.abs

class HeightSelectionActivity : AppCompatActivity() {
    private lateinit var selectedHeightText: TextView
    private lateinit var heightUnit: TextView
    private lateinit var height1Above: TextView
    private lateinit var height2Above: TextView
    private lateinit var height1Below: TextView
    private lateinit var height2Below: TextView
    private lateinit var nextButton: MaterialButton
    private lateinit var backButton: ImageButton
    private var selectedGender: String? = null
    private var selectedAge: Int = 0
    private var selectedWeight: Int = 0

    private var currentHeight = 167
    private val minHeight = 100
    private val maxHeight = 220

    private var lastY: Float = 0f
    private val scrollSensitivity = 15f // 调整这个值可以改变滑动灵敏度

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

        selectedGender = intent.getStringExtra("selected_gender")
        selectedAge = intent.getIntExtra("selected_age", 0)
        selectedWeight = intent.getIntExtra("selected_weight", 0)

        selectedHeightText = findViewById(R.id.selectedHeightText)
        heightUnit = findViewById(R.id.heightUnit)
        height1Above = findViewById(R.id.height1Above)
        height2Above = findViewById(R.id.height2Above)
        height1Below = findViewById(R.id.height1Below)
        height2Below = findViewById(R.id.height2Below)
        nextButton = findViewById(R.id.nextButton)
        backButton = findViewById(R.id.backButton)

        setupUI()
        setupClickListeners()
    }

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

    private fun updateHeightDisplay() {
        selectedHeightText.text = currentHeight.toString()
        // 上方身高
        if (currentHeight - 1 >= minHeight) {
            height1Above.text = (currentHeight - 1).toString()
        } else {
            height1Above.text = ""
        }
        if (currentHeight - 2 >= minHeight) {
            height2Above.text = (currentHeight - 2).toString()
        } else {
            height2Above.text = ""
        }
        // 下方身高
        if (currentHeight + 1 <= maxHeight) {
            height1Below.text = (currentHeight + 1).toString()
        } else {
            height1Below.text = ""
        }
        if (currentHeight + 2 <= maxHeight) {
            height2Below.text = (currentHeight + 2).toString()
        } else {
            height2Below.text = ""
        }
    }

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

        backButton.setOnClickListener {
            val intent = Intent(this, WeightSelectionActivity::class.java)
            intent.putExtra("selected_gender", selectedGender)
            intent.putExtra("selected_age", selectedAge)
            intent.putExtra("selected_weight", selectedWeight)
            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 newHeight = (currentHeight + change).coerceIn(minHeight, maxHeight)
                    if (newHeight != currentHeight) {
                        currentHeight = newHeight
                        updateHeightDisplay()
                        lastY = currentY
                    }
                }
                return true
            }
        }
        return super.onTouchEvent(event)
    }
} 