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

import android.view.View
import android.widget.ImageView
import android.widget.RelativeLayout
import androidx.appcompat.widget.AppCompatTextView
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.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.develop.food.base.data.model.FoodContentModel
import com.develop.food.base.event.RefreshDataEvent
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.utils.TopResumedActivityHolder
import com.develop.food.base.widgets.CancelConfirmDialog
import com.develop.food.base.widgets.StarView
import com.develop.main.R
import org.greenrobot.eventbus.EventBus

class RecipesListAdapter :
    BaseQuickAdapter<FoodContentModel, BaseViewHolder>(R.layout.food_content_provider) {
    private val roundedCorners =
        RoundedCorners(ResDimension(com.develop.base.R.dimen.convert_30px).toInt())
    private val options = RequestOptions.bitmapTransform(roundedCorners)

    private val cancelConfirmDialog by lazy {
        CancelConfirmDialog()
    }

    var isHistory = false

    override fun convert(holder: BaseViewHolder, item: FoodContentModel) {
        val cover = if (item.isNetRecipes && !item.hasDownloaded) {
            item.cover
        } else {
            FoodDataProvider.getResourcePath(item.cover.toString())
        }
        Glide.with(context).load(cover).apply(options).into(holder.getView(R.id.iv_cover))
        holder.getView<AppCompatTextView>(R.id.tv_food_name).text = item.foodName
        holder.getView<StarView>(R.id.star_view).setStarCount(item.starCount)
        val likeLayout = holder.getView<RelativeLayout>(R.id.like_layout)
        val ivLike = holder.getView<ImageView>(R.id.iv_like)
        holder.getView<AppCompatTextView>(R.id.tv_time_and_level).text =
            "${item.time}・${item.level}"
        val ivNet = holder.getView<ImageView>(R.id.iv_net)
        if (item.isNetRecipes) {
            ivNet.visibility = View.VISIBLE
        } else {
            ivNet.visibility = View.GONE
        }
        if (item.hasDownloaded) {
            //已下载
            ivLike.setBackgroundResource(R.drawable.ic_delete)
        } else {
            if (item.isLike) {
                ivLike.setBackgroundResource(R.drawable.ic_like)
            } else {
                ivLike.setBackgroundResource(R.drawable.ic_unlike)
            }
        }
        likeLayout.setOnClickListener {
            if (item.isNetRecipes) {
                val titleStr = context.getString(R.string.are_you_sure_to_delete)
                val confirmStr = context.getString(R.string.yes)
                val cancelStr = context.getString(R.string.no)
                TopResumedActivityHolder.getCurrentActivity()?.apply {
                    cancelConfirmDialog.apply {
                        this.title = titleStr
                        this.confirmStr = confirmStr
                        this.cancelStr = cancelStr
                        onDialogClickListener = object : CancelConfirmDialog.OnDialogClickListener {
                            override fun onConfirm() {
                                FoodDataProvider.getUserDatabase().runInTransaction {
                                    FoodDataProvider.getUserDatabase().userInfoDao()
                                        .removeOnlineRecipe(
                                            CURRENT_USER_ID, item.foodId
                                        )
                                }
                                FoodDataProvider.getDatabase().runInTransaction {
                                    val recipeDao = FoodDataProvider.getDatabase().recipeDao()
                                    val recipes = recipeDao.queryRecipe(item.foodId)
                                    recipes?.apply {
                                        recipeDao.deleteRecipe(this)
                                    }
                                    FoodDataProvider.deleteRecipeResource(item.foodId)
                                }
                                EventBus.getDefault().post(RefreshDataEvent())
                            }

                            override fun onCancel() {

                            }
                        }
                        showDialog(supportFragmentManager, "cancelConfirmDialog")
                    }
                }
            } else {
                //收藏/取消收藏
                if (item.isLike) {
                    item.isLike = false
                    FoodDataProvider.getUserDatabase().runInTransaction {
                        FoodDataProvider.getUserDatabase().userInfoDao().removeFavoriteRecipe(
                            CURRENT_USER_ID, item.foodId
                        )
                    }
                    ivLike.setBackgroundResource(R.drawable.ic_unlike)
                    if (!isHistory) {
                        EventBus.getDefault().post(RefreshDataEvent())
                    }

                } else {
                    item.isLike = true
                    ivLike.setBackgroundResource(R.drawable.ic_like)
                    FoodDataProvider.getUserDatabase().runInTransaction {
                        FoodDataProvider.getUserDatabase().userInfoDao().insertFavoriteRecipe(
                            UserFavoriteRecipes(CURRENT_USER_ID, item.foodId)
                        )
                    }
                    if (!isHistory) {
                        EventBus.getDefault().post(RefreshDataEvent())
                    }
                }
            }
        }
    }
}