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.activityViewModels
import com.develop.food.base.ui.BaseFragment
import com.develop.step.R
import com.develop.step.databinding.FragmentDetailCookSourceBinding
import com.develop.step.widgets.AmountSelectDialog

class CookDetailSourceFragment: BaseFragment() {

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

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        binding = FragmentDetailCookSourceBinding.inflate(
            inflater, container, false
        )
        binding.rvList.adapter = adapter
        viewModel.getRecipeLiveData().observe(viewLifecycleOwner) {
            val sourceItems = mutableListOf<CookSourceItem>()
            for (recipeFood in it.material) {
                sourceItems.add(CookSourceItem(
                    recipeFood.foodName ?: "",
                    "${recipeFood.amount} ${recipeFood.unit}"
                ))
            }
            adapter.setNewInstance(sourceItems)
            val jarCount = it.portionSize.firstOrNull()?.portionSize?.toIntOrNull() ?: 1
            val string = if (jarCount > 1) {
                getString(R.string.make_n_jars, jarCount.toString())
            } else {
                getString(R.string.make_1_jar)
            }
            binding.tvStepTitle.text = string
        }
        return binding.root
    }

}