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 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 com.narutohuo.xindazhou.common.dialog.ServerConfigDialog
import kotlinx.coroutines.launch
import androidx.lifecycle.lifecycleScope

/**
 * 登录 Fragment（完整封装）
 * 
 * 使用单 Activity 架构，通过 NavigationCallback 接口触发导航
 */
class LoginFragment : Fragment() {
    
    private lateinit var etMobile: TextInputEditText
    private lateinit var etPassword: TextInputEditText
    private lateinit var btnLogin: MaterialButton
    private lateinit var btnRegister: MaterialButton
    private lateinit var progressBar: View
    // 【测试功能】服务器配置按钮
    private var ivServerConfig: View? = null
    
    private val navigationCallback: NavigationCallback?
        get() = activity as? NavigationCallback
    
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_login, 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)
        btnLogin = view.findViewById(R.id.btnLogin)
        btnRegister = view.findViewById(R.id.btnRegister)
        progressBar = view.findViewById(R.id.progressBar)
        ivServerConfig = view.findViewById(R.id.ivServerConfig)
        
        // 【测试功能】服务器配置按钮
        ivServerConfig?.setOnClickListener {
            val dialog = ServerConfigDialog()
            dialog.show(parentFragmentManager, "ServerConfigDialog")
        }
        
        // 登录按钮
        btnLogin.setOnClickListener {
            val mobile = etMobile.text?.toString()?.trim() ?: ""
            val password = etPassword.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
            }
            
            // 显示加载状态
            progressBar.visibility = View.VISIBLE
            btnLogin.isEnabled = false
            
            // 调用 AuthManager 登录
            lifecycleScope.launch {
                AuthManager.login(requireActivity(), mobile, password) { result ->
                    progressBar.visibility = View.GONE
                    btnLogin.isEnabled = true
                    
                    result.onSuccess {
                        Toast.makeText(requireContext(), "登录成功", Toast.LENGTH_SHORT).show()
                        // 登录成功后，通过接口回调触发导航到主界面
                        navigationCallback?.navigateToMain()
                    }.onFailure { error ->
                        Toast.makeText(requireContext(), error.message ?: "登录失败", Toast.LENGTH_SHORT).show()
                    }
                }
            }
        }
        
        // 注册按钮
        btnRegister.setOnClickListener {
            // 通过接口回调触发导航到注册页
            navigationCallback?.navigateToRegister()
        }
    }
}

