package com.app.input.personalised.board.boartfragment

import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import com.app.input.personalised.board.BoartApp
import com.app.input.personalised.board.databean.ThemeCategory
import com.app.input.personalised.board.databinding.FragmentCategoryBinding
import com.app.input.personalised.board.listhelpers.CategoryGridAdapter
import com.app.input.personalised.board.boartactivity.ThemeListActivity

class CategoryFragment : Fragment() {

    private lateinit var vb: FragmentCategoryBinding
    private lateinit var categoryGridAdapter: CategoryGridAdapter
    
    // 懒加载标志，避免重复初始化
    private var isDataInitialized = false

    companion object {
        @JvmStatic
        fun newInstance() = CategoryFragment()
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        vb = FragmentCategoryBinding.inflate(layoutInflater)
        return vb.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        // 延迟到 onResume 时初始化，此时 Fragment 已经可见
        isDataInitialized = true
    }
    
    override fun onResume() {
        super.onResume()
        // 在 onResume 时才加载数据，避免切换时阻塞
        if (isDataInitialized && !::categoryGridAdapter.isInitialized) {
            // 延迟加载 RecyclerView，让切换动画先完成
            view?.postDelayed({
                if (isAdded && !isDetached) {
                    initCategoryList()
                }
            }, 500) // 延迟 500ms，确保切换动画完全完成
        }
    }

    private fun initCategoryList() {
        val categoryList = try {
            val boartAppList = BoartApp.Companion.list
            if (boartAppList.isEmpty()) {
                mutableListOf()
            } else {
                boartAppList.toMutableList()
            }
        } catch (e: UninitializedPropertyAccessException) {
            mutableListOf()
        }

        categoryGridAdapter = CategoryGridAdapter(requireContext(), categoryList).apply {
            setOnCategoryCardClickListener(object : CategoryGridAdapter.OnCategoryCardClickListener {
                override fun onCategoryCardClick(beanWrapper: ThemeCategory) {
                    // 跳转到分类二级界面
                    val intent = Intent(requireContext(), ThemeListActivity::class.java).apply {
                        putExtra(ThemeListActivity.KEY_NAME, beanWrapper.parentName)
                    }
                    startActivity(intent)
                }
            })
        }

        vb.categoryRecycler.apply {
            layoutManager = LinearLayoutManager(requireContext())
            adapter = categoryGridAdapter
            // 优化性能：减少缓存大小
            setItemViewCacheSize(5) // 从10减少到5
            // 禁用过度滚动，减少渲染开销
            overScrollMode = View.OVER_SCROLL_NEVER
        }
    }
}

