// 忘记密码验证码输入页面
package com.example.myapplication.ui.login

import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.view.KeyEvent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.EditText
import androidx.fragment.app.Fragment
import com.example.myapplication.R
import android.widget.TextView
import androidx.navigation.fragment.findNavController


class ForgetPasswordVerifyFragment : Fragment() {
    
    private lateinit var codeInputs: List<EditText>
    
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_forget_password_verify, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        //验证码页面
        view.findViewById<
                TextView>(R.id.nextstep).setOnClickListener {
             findNavController().navigate(R.id.action_forgetPasswordVerifyFragment_to_forgetPasswordResetFragment)
          }

        codeInputs = listOf<EditText>(
            view.findViewById(R.id.et_code_1),
            view.findViewById(R.id.et_code_2),
            view.findViewById(R.id.et_code_3),
            view.findViewById(R.id.et_code_4),
            view.findViewById(R.id.et_code_5),
            view.findViewById(R.id.et_code_6)
        )
    
        setupVerifyCodeInputs()
    }

    private fun setupVerifyCodeInputs() {

        codeInputs.forEachIndexed { index, editText ->
    
            // 输入监听：自动跳到下一格
            editText.addTextChangedListener(object : TextWatcher {
                override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
    
                override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {}
    
                override fun afterTextChanged(s: Editable?) {
                    val text = s?.toString() ?: ""
    
                    // 保证只有1位
                    if (text.length > 1) {
                        editText.setText(text.last().toString())
                        editText.setSelection(1)
                    }
    
                    if (text.isNotEmpty() && index < codeInputs.size - 1) {
                        codeInputs[index + 1].requestFocus()
                    }
                }
            })
    
            // 关键：监听删除键，通过 EditorAction 捕捉硬件删除
            editText.setOnKeyListener { _, keyCode, event ->
                if (event.action == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_DEL) {
    
                    // 当前为空 -> 回到上一格
                    if (editText.text.isEmpty() && index > 0) {
                        val prev = codeInputs[index - 1]
                        prev.requestFocus()
                        prev.text.clear()
                        return@setOnKeyListener true
                    }
                }
                false
            }
    
            // 特别关键：有些系统不会触发 KEYCODE_DEL，这个可以兜底捕捉删除动作
            editText.setOnEditorActionListener { _, _, event ->
                if (event != null &&
                    event.action == KeyEvent.ACTION_DOWN &&
                    event.keyCode == KeyEvent.KEYCODE_DEL
                ) {
                    if (editText.text.isEmpty() && index > 0) {
                        val prev = codeInputs[index - 1]
                        prev.requestFocus()
                        prev.text.clear()
                        true
                    } else false
                } else false
            }
        }
    }
    
    
    


    // 获取完整验证码
    private fun getVerifyCode(): String {
        return codeInputs.joinToString("") { it.text.toString() }
    }
}