package com.develop.step.ui.recipes_detail

import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import com.develop.base.ext.load
import com.develop.base.ext.updateText
import com.develop.common.data_repo.FoodDataProvider
import com.develop.common.ui.CommonBVMFragment
import com.develop.step.R
import com.develop.step.databinding.FragmentDetailCookMethodBinding
import com.develop.step.ui.recipes_detail.model.CookToolItem
import com.develop.step.viewmodel.CookDetailViewModel
import com.drake.brv.utils.grid
import com.drake.brv.utils.models
import com.drake.brv.utils.setup

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

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

    override fun onPostCreateView() {
        super.onPostCreateView()
        binding.rvTools.apply {
            grid(3)
            setup {
                addType<CookToolItem>(R.layout.item_cook_tool)
                onBind {
                    val model = getModel<CookToolItem>()
                    findView<TextView>(R.id.tv_tool_name).updateText(model.text)
                    findView<ImageView>(R.id.iv_tool_icon).load(model.icon)
                }
            }
        }
        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 ?: ""))
            }
            binding.rvTools.models = sourceItems
        }
    }
}