package com.design.wallpapergallery.view

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.design.wallpapergallery.IApplication
import com.design.wallpapergallery.adapter.ClassifyAdapter
import com.design.wallpapergallery.data.MainViewModel
import com.design.wallpapergallery.databinding.FragmentClassificationPageBinding
import com.design.wallpapergallery.listener.MainFirListener


class ClassificationFragment : Fragment(),MainFirListener {
    private lateinit var binding:FragmentClassificationPageBinding
    private lateinit var viewModel:MainViewModel
    private lateinit var adapter: ClassifyAdapter

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        binding=FragmentClassificationPageBinding.inflate(inflater,container,false)
        init()
        return binding.root
    }
    private fun init(){
        viewModel = ViewModelProvider(requireActivity())[MainViewModel::class.java]
        adapter=ClassifyAdapter()
        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 = IApplication.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(),MorePhotoActivity::class.java)
        intent.putExtra(MorePhotoActivity.titleName,name)
        startActivity(intent)
    }
}