package com.narutohuo.xindazhou.common.auth.ui

import android.os.Bundle
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import com.google.android.material.button.MaterialButton
import com.google.android.material.textfield.TextInputEditText
import com.narutohuo.xindazhou.common.R
import com.narutohuo.xindazhou.common.auth.AuthManager
import com.narutohuo.xindazhou.common.auth.NavigationCallback
import kotlinx.coroutines.launch

/**
 * 注册 Fragment（完整封装）
 * 
 * 使用单 Activity 架构，通过 NavigationCallback 接口触发导航
 */
class RegisterFragment : Fragment() {
    
    private lateinit var etMobile: TextInputEditText
    private lateinit var etPassword: TextInputEditText
    private lateinit var etConfirmPassword: TextInputEditText
    private lateinit var btnRegister: MaterialButton
    private lateinit var btnBackToLogin: MaterialButton
    private lateinit var progressBar: View
    
    private val navigationCallback: NavigationCallback?
        get() = activity as? NavigationCallback
    
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_register, container, false)
    }
    
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        
        etMobile = view.findViewById(R.id.etMobile)
        etPassword = view.findViewById(R.id.etPassword)
        etConfirmPassword = view.findViewById(R.id.etConfirmPassword)
        btnRegister = view.findViewById(R.id.btnRegister)
        btnBackToLogin = view.findViewById(R.id.btnBackToLogin)
        progressBar = view.findViewById(R.id.progressBar)
        
        // 注册按钮
        btnRegister.setOnClickListener {
            val mobile = etMobile.text?.toString()?.trim() ?: ""
            val password = etPassword.text?.toString() ?: ""
            val confirmPassword = etConfirmPassword.text?.toString() ?: ""
            
            if (TextUtils.isEmpty(mobile)) {
                Toast.makeText(requireContext(), "请输入手机号", Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }
            
            if (TextUtils.isEmpty(password)) {
                Toast.makeText(requireContext(), "请输入密码", Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }
            
            if (password.length < 6 || password.length > 16) {
                Toast.makeText(requireContext(), "密码长度为6-16位", Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }
            
            if (password != confirmPassword) {
                Toast.makeText(requireContext(), "两次输入的密码不一致", Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }
            
            // 显示加载状态
            progressBar.visibility = View.VISIBLE
            btnRegister.isEnabled = false
            
            // 调用 AuthManager 注册
            lifecycleScope.launch {
                AuthManager.register(requireActivity(), mobile, password) { result ->
                    progressBar.visibility = View.GONE
                    btnRegister.isEnabled = true
                    
                    result.onSuccess {
                        Toast.makeText(requireContext(), "注册成功，请登录", Toast.LENGTH_SHORT).show()
                        // 注册成功后，通过接口回调触发导航到登录页
                        navigationCallback?.navigateToLogin()
                    }.onFailure { error ->
                        Toast.makeText(requireContext(), error.message ?: "注册失败", Toast.LENGTH_SHORT).show()
                    }
                }
            }
        }
        
        // 返回登录按钮
        btnBackToLogin.setOnClickListener {
            // 通过接口回调触发导航到登录页
            navigationCallback?.navigateToLogin()
        }
    }
}

