package com.adpro.pixelshow.look

import android.content.Intent
import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.ad.maxlibrary.MaxManager
import com.adpro.pixelshow.IAppLifecycle
import com.adpro.pixelshow.event.ClassFrameAdapter
import com.adpro.pixelshow.data.MainViewModel
import com.adpro.pixelshow.databinding.FragmentClassBinding
import com.adpro.pixelshow.event.FirClassListener


class ClassificationFragment : Fragment(),FirClassListener {
    private lateinit var binding:FragmentClassBinding
    private lateinit var viewModel:MainViewModel
    private lateinit var adapter: ClassFrameAdapter

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        binding=FragmentClassBinding.inflate(inflater,container,false)
        MaxManager.onLoadAd()
        init()
        return binding.root
    }
    private fun init(){
        viewModel = ViewModelProvider(requireActivity())[MainViewModel::class.java]
        adapter=ClassFrameAdapter()
        adapter.setListener(this)
        binding.rvView.adapter = adapter
        binding.rvView.layoutManager = LinearLayoutManager(requireContext())
        viewModel.coverMap.value?.takeIf { it.isNotEmpty() }?.let {
            Log.d("DataLoad", "hasLoad")
            updateAdapter()
        }

        viewModel.loadAllClassificationCovers()

        viewModel.loadingState.observe(viewLifecycleOwner) { isLoading ->
            viewModel.coverMap.value?.let { coverMap ->
                if (!isLoading && coverMap.isNotEmpty()) {
                    updateAdapter()
                }
            }
        }

        viewModel.coverMap.observe(viewLifecycleOwner) { coverMap ->
            viewModel.loadingState.value?.let { isLoading ->
                if (!isLoading && coverMap.isNotEmpty()) {
                    updateAdapter()
                }
            }
        }
    }
    private fun updateAdapter() {
        val classificationList = IAppLifecycle.classificationList
        val coverMap = viewModel.coverMap.value

        // 确保数据一致性
        val coversForCategories = classificationList.map { classificationName ->
            coverMap?.get(classificationName) ?: emptyList()
        }

        adapter.setList(classificationList, coversForCategories)
        adapter.notifyDataSetChanged()
        Log.d("AdapterUpdate", "refresh data")
        classificationList.forEachIndexed { index, classification ->
            val count = coversForCategories[index].size
            Log.d("DataDebug", "[$index] $classification: $count data")
        }
    }

    override fun more(name: String) {
        val intent=Intent(requireActivity(),TypeInfoActivity::class.java)
        intent.putExtra(TypeInfoActivity.titleName,name)
        startActivity(intent)
    }
}