package com.quyunshuo.module.home.adapter

import android.graphics.Color
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.recyclerview.widget.RecyclerView
import com.google.gson.Gson
import com.module.pay.common.PriceUnit
import com.quyunshuo.androidbaseframemvvm.base.utils.SpUtils
import com.quyunshuo.androidbaseframemvvm.base.utils.commons.StringUtils
import com.quyunshuo.androidbaseframemvvm.common.bean.ProductDataBean
import com.quyunshuo.androidbaseframemvvm.common.constant.MMKVName
import com.quyunshuo.androidbaseframemvvm.common.listener.AdapterClickListener
import com.quyunshuo.androidbaseframemvvm.common.util.UiUtil
import com.quyunshuo.module.home.R
import com.quyunshuo.module.home.databinding.HomeItemHomeProductBinding

class HomeProductAdapter(var productList: ArrayList<ProductDataBean>) :
    RecyclerView.Adapter<HomeProductAdapter.MyViewHolder>() {
    private val TAG = "HomeProductAdapter"
    var listener: AdapterClickListener? = null

    inner class MyViewHolder(binding: HomeItemHomeProductBinding) :
        RecyclerView.ViewHolder(binding.root) {
        private val mBinding = binding
        fun bind(productDataBean: ProductDataBean) {
            mBinding.run {
                //图片
                ivSugar.setImageResource(
                    UiUtil.getResId(
                        productDataBean.imgID,
                        R.drawable::class.java
                    )
                )
                Log.d(TAG, "bind: $productDataBean")
                //商品名字：自定义、自带
                if (productDataBean.customName == "") {
                    tvName.setText(UiUtil.getResId(productDataBean.nameId, R.string::class.java))
                } else {
                    tvName.text = productDataBean.customName
                }
                var priceUnit = SpUtils.getString(MMKVName.PRICE_UNIT, "")
                if (StringUtils.isNotNullOrEmpty(priceUnit)) {
                    var priceUnitBean = Gson().fromJson(priceUnit, PriceUnit::class.java)
                    priceUnitTv.text = priceUnitBean.unit
                    priceUnitTv.setTextColor(Color.parseColor(priceUnitBean.color))
                    homeImageview.visibility = View.GONE
                }
                //价格
                tvPrice.setText("" + productDataBean.price)

                //点击事件
                clProduct.setOnClickListener {
                    listener?.onClickListener(it, layoutPosition, "")
                }
            }
        }


    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
        val projectVH = MyViewHolder(
            HomeItemHomeProductBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        )
        return projectVH
    }

    override fun getItemCount(): Int {
        return productList.size
    }

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        holder.bind(productList[position])


    }

    fun setOnclickListener(adapter: AdapterClickListener) {
        this.listener = adapter
    }
}