package com.design.mischiefgo.ui

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.design.mischiefgo.databinding.FragmentYellowEndBinding
import com.design.mischiefgo.ut.StartAndEndAdapter
import com.design.mischiefgo.entity.Duplicate
import com.design.mischiefgo.entity.MainViewModel
import com.design.mischiefgo.ut.OnMoreClickListener

class YellowEndFragment : Fragment(),OnMoreClickListener {
    private lateinit var binding: FragmentYellowEndBinding
    private lateinit var viewModel: MainViewModel
    private lateinit var adapter: StartAndEndAdapter

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        // Inflate the layout for this fragment
        binding = FragmentYellowEndBinding.inflate(inflater, container, false)
        viewModel = ViewModelProvider(requireActivity())[MainViewModel::class.java]
        initView()
        return binding.root
    }

    override fun onResume() {
        super.onResume()
        viewModel.loadLikeItems()
    }

    private fun initView() {
        adapter = StartAndEndAdapter()
        adapter.setListener(this)
        viewModel.likeItems.observe(viewLifecycleOwner){likeList ->
            Log.d("CollectFragment", "initView: $likeList")
            if(likeList.isEmpty()){
                binding.emptyView.visibility=View.VISIBLE
                binding.recyclerView.visibility=View.GONE
            }else{
                binding.emptyView.visibility=View.GONE
                binding.recyclerView.visibility=View.VISIBLE
                adapter.setList(likeList,0)
                adapter.notifyDataSetChanged()
            }
        }

        binding.recyclerView.adapter = adapter
        binding.recyclerView.layoutManager=LinearLayoutManager(requireContext(),LinearLayoutManager.VERTICAL,false)
    }

    override fun onMoreClick(duplicate: Duplicate) {
        viewModel.updateLikeStatus(duplicate.name,false)
    }
}