package com.develop.step.ui.recipes_detail

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import com.develop.base.ext.updateText
import com.develop.common.ui.CommonBVMFragment
import com.develop.step.R
import com.develop.step.databinding.FragmentDetailCookServingBinding
import com.develop.step.ui.recipes_detail.model.CookSourceItem
import com.develop.step.viewmodel.CookDetailViewModel
import com.drake.brv.utils.linear
import com.drake.brv.utils.models
import com.drake.brv.utils.setup

class CookDetailServeFragment :
    CommonBVMFragment<FragmentDetailCookServingBinding, CookDetailViewModel>() {
    override fun createViewModel(): CookDetailViewModel {
        return getViewModelOfActivity(CookDetailViewModel::class.java)
    }

    override fun createViewBinding(
        inflater: LayoutInflater,
        container: ViewGroup?
    ): FragmentDetailCookServingBinding {
        return FragmentDetailCookServingBinding.inflate(inflater, container, false)
    }

    override fun onPostCreateView() {
        super.onPostCreateView()
        binding.rvList.apply {
            linear()
            setup {
                addType<CookSourceItem>(R.layout.item_cook_source)
                onBind {
                    val model = getModel<CookSourceItem>()
                    findView<TextView>(R.id.tv_source_name).updateText(model.name)
                    findView<TextView>(R.id.tv_source_amount).updateText(model.amount)
                }
            }
        }
        viewModel.getRecipeLiveData().observe(viewLifecycleOwner) {
            val sourceItems = mutableListOf<CookSourceItem>()
            for (nutrition in it.nutrition) {
                sourceItems.add(
                    CookSourceItem(
                        nutrition.nutritionName ?: "",
                        "${nutrition.amount} ${nutrition.unit}"
                    )
                )
            }
            binding.rvList.models = sourceItems
        }
    }
}