package com.design.zenspace.preview

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.zenspace.databinding.FragmentCollectWallBinding
import com.design.zenspace.tools.LikeWallAdapter
import com.design.zenspace.entity.MainViewModel
import com.design.zenspace.entity.Feature
import com.design.zenspace.tools.SecFavListener

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

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