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.GridLayoutManager
import com.design.wallpapergallery.adapter.AddLikeAdapter
import com.design.wallpapergallery.data.MainViewModel
import com.design.wallpapergallery.data.Photo
import com.design.wallpapergallery.databinding.FragmentLikePageBinding
import com.design.wallpapergallery.listener.MainSecListener

class LikeFragment : Fragment(),MainSecListener {
    private lateinit var binding: FragmentLikePageBinding
    private lateinit var viewModel: MainViewModel
    private lateinit var adapter:AddLikeAdapter
    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=AddLikeAdapter()
        adapter.setListener(this)
        viewModel.likePhotos.observe(viewLifecycleOwner) { photos ->
            Log.d("LikeFragment","paper is$photos")
            if (photos.isNullOrEmpty()) {
                // 可以在这里显示空状态视图
                binding.layoutNoData.visibility = View.VISIBLE
                binding.rvView.visibility = View.GONE
            } else {
                binding.layoutNoData.visibility = View.GONE
                binding.rvView.visibility = View.VISIBLE
                adapter.setList(photos)
                adapter.notifyDataSetChanged()
            }
        }
        val gridLayoutManager = GridLayoutManager(requireActivity(), 3)
        binding.rvView.layoutManager=gridLayoutManager
        binding.rvView.adapter=adapter
    }

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