package com.narutohuo.xindazhou.user.ui.register

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.fragment.app.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.navigation.fragment.findNavController
import com.google.android.material.button.MaterialButton
import com.google.android.material.textfield.TextInputEditText
import com.narutohuo.xindazhou.R
import com.narutohuo.xindazhou.user.ui.constant.UiConstants
import com.narutohuo.xindazhou.user.ui.viewmodel.RegisterViewModel
import com.narutohuo.xindazhou.user.ui.viewmodel.RegisterState
import com.narutohuo.xindazhou.user.ui.viewmodel.RegisterViewModelFactory
import kotlinx.coroutines.launch

/**
 * 注册Fragment
 */
class RegisterFragment : Fragment() {
    
    private val viewModel: RegisterViewModel by viewModels { 
        RegisterViewModelFactory(requireContext().applicationContext as android.app.Application)
    }
    
    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
    
    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(), UiConstants.MSG_MOBILE_EMPTY, Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }
            
            if (TextUtils.isEmpty(password)) {
                Toast.makeText(requireContext(), UiConstants.MSG_PASSWORD_EMPTY, Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }
            
            if (password.length < 6 || password.length > 16) {
                Toast.makeText(requireContext(), UiConstants.MSG_PASSWORD_INVALID, Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }
            
            if (password != confirmPassword) {
                Toast.makeText(requireContext(), UiConstants.MSG_PASSWORD_NOT_MATCH, Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }
            
            viewModel.register(mobile, password)
        }
        
        // 返回登录按钮
        btnBackToLogin.setOnClickListener {
            findNavController().popBackStack()
        }
        
        // 观察注册状态
        lifecycleScope.launch {
            viewModel.registerState.collect { state ->
                when (state) {
                    is RegisterState.Idle -> {
                        progressBar.visibility = View.GONE
                        btnRegister.isEnabled = true
                    }
                    is RegisterState.Loading -> {
                        progressBar.visibility = View.VISIBLE
                        btnRegister.isEnabled = false
                    }
                    is RegisterState.Success -> {
                        progressBar.visibility = View.GONE
                        btnRegister.isEnabled = true
                        Toast.makeText(requireContext(), state.message, Toast.LENGTH_SHORT).show()
                        // 注册成功后自动登录，导航到主界面
                        findNavController().navigate(R.id.action_registerFragment_to_mainFragment)
                    }
                    is RegisterState.Error -> {
                        progressBar.visibility = View.GONE
                        btnRegister.isEnabled = true
                        Toast.makeText(requireContext(), state.message, Toast.LENGTH_SHORT).show()
                    }
                }
            }
        }
    }
}

