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.GridLayoutManager
import com.adpro.pixelshow.event.ChangeLoveAdapter
import com.adpro.pixelshow.data.MainViewModel
import com.adpro.pixelshow.data.Movie
import com.adpro.pixelshow.databinding.FragmentLikePageBinding
import com.adpro.pixelshow.event.SecFavListener

class LikeFragment : Fragment(),SecFavListener {
    private lateinit var binding: FragmentLikePageBinding
    private lateinit var viewModel: MainViewModel
    private lateinit var adapter:ChangeLoveAdapter
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        binding=FragmentLikePageBinding.inflate(inflater,container,false)
        init()
        return binding.root
    }
    private fun init(){
        viewModel=ViewModelProvider(requireActivity())[MainViewModel::class.java]
        adapter=ChangeLoveAdapter()
        adapter.setListener(this)
        viewModel.likeMovies.observe(viewLifecycleOwner) { movies ->
            Log.d("LikeFragment","paper is$movies")
            if (movies.isNullOrEmpty()) {
                // 可以在这里显示空状态视图
                binding.layoutNoData.visibility = View.VISIBLE
                binding.rvView.visibility = View.GONE
            } else {
                binding.layoutNoData.visibility = View.GONE
                binding.rvView.visibility = View.VISIBLE
                adapter.setList(movies)
                adapter.notifyDataSetChanged()
            }
        }
        val gridLayoutManager = GridLayoutManager(requireActivity(), 3)
        binding.rvView.layoutManager=gridLayoutManager
        binding.rvView.adapter=adapter
    }

    override fun inItemClick(paper: Movie) {
        val intent= Intent(context, DownloadRequestActivity::class.java)
        intent.putExtra(DownloadRequestActivity.paperKey,paper)
        startActivity(intent)
    }
}