package com.ujuad.demo.ui.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
import androidx.fragment.app.Fragment
import com.ujuad.demo.MyApplication
import com.ujuad.demo.R
import com.ujuad.demo.databinding.FragmentHomeBinding
import com.ujuad.demo.viewmodel.AppViewModel


/**
 * 首页Fragment，展示应用概览和快速操作入口
 */
class HomeFragment : Fragment() {

    private val appViewModel: AppViewModel by lazy {
        (requireActivity().application as MyApplication).appViewModel
    }
    private var _binding: FragmentHomeBinding? = null
    private val binding get() = _binding!!


    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {

        _binding = FragmentHomeBinding.inflate(inflater, container, false)

        return binding.root
    }

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

        // 设置按钮点击事件
        setupButtonListeners()

        // 更新SDK状态显示
        updateSdkStatus(appViewModel.sdkInitialized.value)
    }

    /**
     * 设置所有按钮的点击监听器
     */
    private fun setupButtonListeners() {
        // 权限管理按钮
        binding.permissionButton.setOnClickListener {
            //跳转到权限管理页面
            navigateToFragment(PermissionFragment())
        }

        // 关于按钮
        binding.aboutButton.setOnClickListener {
            //跳转到关于页面
            navigateToFragment(AboutFragment())
        }

        binding.btnAdList.setOnClickListener {
            //跳转到广告列表页面
            navigateToFragment(AdGroupListFragment())
        }

        binding.btnAdMix.setOnClickListener {
            //跳转到混合广告页面
            navigateToFragment(AdMixFragment())
        }
    }

    /**
     * 导航到指定Fragment
     * @param fragment 要跳转的Fragment实例
     */
    private fun navigateToFragment(fragment: Fragment) {
        try {
            requireActivity().supportFragmentManager.beginTransaction()
                .replace(R.id.flContainer, fragment)
                .addToBackStack(null) // 添加到返回栈，允许用户返回
                .commit()
        } catch (e: Exception) {
            Toast.makeText(requireContext(), "页面跳转失败: ${e.message}", Toast.LENGTH_SHORT)
                .show()
        }
    }


    /**
     * 更新SDK状态显示
     * @param isInitialized SDK是否已初始化
     */
    private fun updateSdkStatus(isInitialized: Boolean) {
        if (isInitialized) {
            binding.initStatus.text = "已初始化"
            binding.initStatus.setTextColor(resources.getColor(R.color.success, null))
        } else {
            binding.initStatus.text = "未初始化"
            binding.initStatus.setTextColor(resources.getColor(R.color.error, null))
        }
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }

    companion object {
        private const val TAG = "HomeFragment"
    }
}
