package com.various.funny.sounds.audio.ui.fragment

import androidx.lifecycle.ViewModelProvider
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.various.funny.sounds.audio.databinding.FragmentCollectBinding
import com.various.funny.sounds.audio.ui.adapter.CollectAdapter

class CollectFragment : Fragment() {

    companion object {
        fun newInstance() = CollectFragment()
    }

    private lateinit var viewModel: CollectViewModel
    private lateinit var collectBinding: FragmentCollectBinding
    private lateinit var collectAdapter:CollectAdapter

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        collectBinding = FragmentCollectBinding.inflate(layoutInflater)
        onInitCollect()
        return collectBinding.root
    }

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

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        viewModel = ViewModelProvider(this).get(CollectViewModel::class.java)
        viewModel.getList().observe(viewLifecycleOwner, Observer {

            collectAdapter.updateData(it)
        })
    }

    private fun onInitCollect(){
         collectAdapter = CollectAdapter(requireContext())
        collectBinding.collectRecycler.run {
            adapter = collectAdapter
            layoutManager = LinearLayoutManager(requireContext())
        }
    }

}