package com.base.appzxhy.ui.category

import com.base.appzxhy.BuildConfig
import com.base.appzxhy.R
import com.base.appzxhy.base.BaseFragment
import com.base.appzxhy.databinding.FragmentCategoryPageBinding
import com.base.appzxhy.ui.adapter.ShortPlayAdapter
import com.base.appzxhy.utils.ToastUtils.toast
import com.bytedance.sdk.shortplay.api.ShortPlay

class CategoryPageFragment(
    val category: ShortPlay.ShortPlayCategory
) : BaseFragment<FragmentCategoryPageBinding>(FragmentCategoryPageBinding::inflate) {

    private var adapter: ShortPlayAdapter? = null

    override fun initView() {
        super.initView()
        adapter = ShortPlayAdapter(R.layout.item_shortplay_style_4)
        binding.rv.adapter = adapter
        adapter?.itemClick = ::shortPlayClick
    }

    private fun shortPlayClick(shortPlay: ShortPlay) {
        if (BuildConfig.DEBUG) {
            requireContext().toast("${shortPlay.title}")
        }
    }

    override fun onResumeOneShoot() {
        super.onResumeOneShoot()
        val activity = requireActivity() as CategoryActivity?

        activity?.viewModel?.getFeedListByCategoryIds(categoryIds = listOf(category.id)) { list ->
            activity.runOnUiThread {
                adapter?.submitList(list)
            }
        }
    }

    override fun initListener() {
        super.initListener()
    }
}