package com.develop.main.init.ui.adapter


import android.widget.ImageView
import androidx.appcompat.widget.AppCompatTextView
import androidx.core.content.ContextCompat
import androidx.core.graphics.drawable.DrawableCompat
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.develop.food.base.repo.entity.DevRecipeCategory
import com.develop.food.base.utils.GlobalApp
import com.develop.food.base.utils.isNightTheme
import com.develop.food.base.utils.setGone
import com.develop.food.base.utils.setVisible
import com.develop.main.R
import com.develop.main.init.ui.viewmodel.HomeViewModel


class CategoryAdapter :
    BaseQuickAdapter<DevRecipeCategory, BaseViewHolder>(R.layout.item_filter_and_sort_view) {
    private var selectedPosition = 0

    var homeViewModel: HomeViewModel? = null
    override fun convert(holder: BaseViewHolder, item: DevRecipeCategory) {
        val ivCheck = holder.getView<ImageView>(R.id.iv_check)
        val tvName = holder.getView<AppCompatTextView>(R.id.tv_name)
        if (homeViewModel?.recipesType == HomeViewModel.RecipesType.ONLINE) {
            tvName.text = item.name
        } else {

            var size = if (item.name == GlobalApp().getString(R.string.all)) {
                homeViewModel?.categorySizeList?.get(item.name) ?: "null"
            } else {
                homeViewModel?.categorySizeList?.get(item.number) ?: "null"
            }
            if (size == "null") {
                size = "0"
            }
            tvName.text = "${item.name}($size)"
        }

        if (selectedPosition == holder.adapterPosition) {
            ivCheck.setVisible()
            if (isNightTheme()) {
                val up = ContextCompat.getDrawable(context, R.drawable.ic_check)
                val drawableUp = DrawableCompat.wrap(up!!)
                DrawableCompat.setTint(
                    drawableUp,
                    ContextCompat.getColor(context, R.color.color_000000)
                )
                tvName.setTextColor(context.resources.getColor(R.color.color_000000))
                ivCheck.setImageDrawable(drawableUp)
            } else {
                tvName.setTextColor(context.resources.getColor(R.color.color_F4A12E))
                ivCheck.setBackgroundResource(R.drawable.ic_check)
            }
        } else {
            tvName.setTextColor(context.resources.getColor(R.color.color_6B6B6B))
            ivCheck.setGone()
        }
    }

    fun updateSelectedPosition(adapterPosition: Int) {
        val tempPosition = selectedPosition
        selectedPosition = adapterPosition
        notifyItemChanged(selectedPosition)
        notifyItemChanged(tempPosition)
    }
}