package com.develop.main.init.ui.provider

import android.view.View
import android.widget.ImageView
import android.widget.RelativeLayout
import androidx.appcompat.widget.AppCompatTextView
import androidx.cardview.widget.CardView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions
import com.chad.library.adapter.base.provider.BaseItemProvider
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.develop.food.base.data.model.FoodContentModel
import com.develop.food.base.repo.CURRENT_USER_ID
import com.develop.food.base.repo.FoodDataProvider
import com.develop.food.base.repo.entity.UserFavoriteRecipes
import com.develop.food.base.utils.ResDimension
import com.develop.food.base.widgets.StarView
import com.develop.main.R
import com.develop.main.init.ui.adapter.RecipesMainAdapter

class RecipesContentProvider : BaseItemProvider<Any>() {
    override val itemViewType: Int
        get() = RecipesMainAdapter.CONTENT_TAG
    override val layoutId: Int
        get() = R.layout.food_content_provider
    private val roundedCorners =
        RoundedCorners(ResDimension(com.develop.base.R.dimen.convert_30px).toInt())
    private val options = RequestOptions.bitmapTransform(roundedCorners)
    var onRecipesContentClickListener: OnRecipesContentClickListener? = null
    override fun convert(helper: BaseViewHolder, item: Any) {
        item as FoodContentModel
        val cover = if (item.isNetRecipes) {
            item.cover
        } else {
            FoodDataProvider.getResourcePath(item.cover.toString())
        }
        Glide.with(context).load(cover).apply(options).into(helper.getView(R.id.iv_cover))
        helper.getView<AppCompatTextView>(R.id.tv_food_name).text = item.foodName
        helper.getView<StarView>(R.id.star_view).apply {
            setStarCount(item.starCount)
            setCommentNumber(item.useNum.toString())
        }
        helper.getView<CardView>(R.id.recipes_layout).setOnClickListener {
            onRecipesContentClickListener?.onRecipesContentClick(item, helper.adapterPosition)
        }
        helper.getView<AppCompatTextView>(R.id.tv_time_and_level).text =
            "${item.time}・${item.level}"
        val likeLayout = helper.getView<RelativeLayout>(R.id.like_layout)
        val ivLike = helper.getView<ImageView>(R.id.iv_like)
        val ivNet = helper.getView<ImageView>(R.id.iv_net)
        if (item.isNetRecipes) {
            ivNet.visibility = View.VISIBLE
        } else {
            ivNet.visibility = View.GONE
        }
        if (item.isLike) {
            ivLike.setBackgroundResource(R.drawable.ic_like)
        } else {
            ivLike.setBackgroundResource(R.drawable.ic_unlike)
        }
        likeLayout.setOnClickListener {
            //收藏/取消收藏
            if (item.isLike) {
                item.isLike = false
                FoodDataProvider.getUserDatabase().runInTransaction {
                    FoodDataProvider.getUserDatabase().userInfoDao().removeFavoriteRecipe(
                        CURRENT_USER_ID, item.foodId
                    )
                }
                ivLike.setBackgroundResource(R.drawable.ic_unlike)
            } else {
                item.isLike = true
                ivLike.setBackgroundResource(R.drawable.ic_like)
                FoodDataProvider.getUserDatabase().runInTransaction {
                    FoodDataProvider.getUserDatabase().userInfoDao().insertFavoriteRecipe(
                        UserFavoriteRecipes(CURRENT_USER_ID, item.foodId)
                    )
                }
            }
        }
    }

    interface OnRecipesContentClickListener {
        fun onRecipesContentClick(recipesContent: FoodContentModel, position: Int)
    }
}