package com.zxbw.modulemain.box.adapter

import android.app.Activity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.zxbw.modulemain.R
import com.zxbw.modulemain.view.DialogUtils
import com.zxhl.cms.AppContext
import com.zxhl.cms.common.base.BaseRecyclerAdapter
import com.zxhl.cms.net.model.box.GoodList
import com.zxhl.cms.widget.RecycleImageView

class GoodsPoolAdapter : BaseRecyclerAdapter<GoodList, GoodsPoolAdapter.ViewHolder> {
    private var mContext: Activity? = null

    constructor(
        content: Activity?
    ) : super() {
        mContext = content
    }

    open

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        var bean = mList[position]
        holder.item_goods_img?.setLoadImageUrl(bean.mainImage, false)
        holder.item_goods_pinzhi?.text = bean.cateName
        if (bean.goodsLevel == "0") {
            holder.item_goods_pinzhi?.setBackgroundResource(R.drawable.shape_putong_bg_left)
        } else if (bean.goodsLevel == "1") {
            holder.item_goods_pinzhi?.setBackgroundResource(R.drawable.shape_xiyou_bg_left)
        } else if (bean.goodsLevel == "2") {
            holder.item_goods_pinzhi?.setBackgroundResource(R.drawable.shape_shishi_bg_left)
        } else if (bean.goodsLevel == "3") {
            holder.item_goods_pinzhi?.setBackgroundResource(R.drawable.shape_chuanshuo_bg_left)
        }
        holder.item_goods_name?.text = bean.productName
        holder.item_goods_price?.text = bean.salePriceStr
        holder.item_goods_gailv?.text =
            mContext?.resources?.getString(
                R.string.box_detail_open_box_shop_pool_gailv,
                bean.winningProbability
            )
        holder.item_view?.setOnClickListener {
            if (mContext != null) {
                var goodsImgList: List<String> =
                    Gson().fromJson(bean?.descImg, object : TypeToken<List<String>>() {}.type)
                if (!goodsImgList.isNullOrEmpty()){
                    DialogUtils.showGoodsDetailDialog(mContext!!, goodsImgList)
                }
            }
        }
    }


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        return ViewHolder(
            LayoutInflater.from(AppContext.get())
                .inflate(R.layout.item_goods_pool3, parent, false)
        )
    }

    class ViewHolder : RecyclerView.ViewHolder {
        var item_view: RelativeLayout?
        var item_goods_img: RecycleImageView?
        var item_goods_pinzhi: TextView?
        var item_goods_name: TextView?
        var item_goods_price: TextView?
        var item_goods_gailv: TextView?

        constructor(
            itemView: View
        ) : super(itemView) {
            item_view = itemView.findViewById<RelativeLayout>(R.id.id_rl_goods_detail_item_view)
            item_goods_img = itemView.findViewById<RecycleImageView>(R.id.id_img_good)
            item_goods_pinzhi = itemView.findViewById<TextView>(R.id.id_tv_pinzhi)
            item_goods_name = itemView.findViewById<TextView>(R.id.id_good_name)
            item_goods_price = itemView.findViewById<TextView>(R.id.id_tv_goods_price)
            item_goods_gailv = itemView.findViewById<TextView>(R.id.id_tv_goods_gailv)
        }
    }
}