package com.develop.step.details

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import androidx.recyclerview.widget.GridLayoutManager
import com.develop.food.base.repo.FoodDataProvider
import com.develop.step.databinding.FragmentDetailCookMethodBinding

class CookDetailToolsFragment : Fragment() {

    private lateinit var binding: FragmentDetailCookMethodBinding
    private val adapter = CookDetailToolAdapter()
    private val viewModel by activityViewModels<CookDetailViewModel>()

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        binding = FragmentDetailCookMethodBinding.inflate(
            inflater, container, false
        )
        binding.rvTools.layoutManager = GridLayoutManager(
            requireContext(), 3
        )
        binding.rvTools.adapter = adapter
        viewModel.getRecipeLiveData().observe(viewLifecycleOwner) {
            val sourceItems = mutableListOf<CookToolItem>()
            for (accessory in it.accessory) {
                val path = FoodDataProvider.getResourcePath(accessory.photoPath)
                sourceItems.add(CookToolItem(path, accessory.name ?: ""))
            }
            adapter.setNewInstance(sourceItems)
        }
        return binding.root
    }

}