package com.zxbw.modulemain.adapter

import android.content.Context
import android.graphics.Paint
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.zxbw.modulemain.R
import com.zxhl.cms.AppContext
import com.zxhl.cms.common.base.BaseRecyclerAdapter
import com.zxhl.cms.net.model.qy.TbGoodsItemEntity
import com.zxhl.cms.utils.OnRecycleItemClickListener
import com.zxhl.cms.utils.Utils
import com.zxhl.cms.widget.RecycleImageView

//搜索商品排行榜
class SearchGoodsTopAdapter :
    BaseRecyclerAdapter<TbGoodsItemEntity, SearchGoodsTopAdapter.ViewHolder> {
    private var mContext: Context? = null
    private val listener: OnRecycleItemClickListener<TbGoodsItemEntity>

    private var mTopIconList = listOf(
        R.drawable.icon_goods_top1,
        R.drawable.icon_goods_top2,
        R.drawable.icon_goods_top3,
        R.drawable.icon_goods_top4,
        R.drawable.icon_goods_top5,
        R.drawable.icon_goods_top6,
        R.drawable.icon_goods_top7,
        R.drawable.icon_goods_top8,
        R.drawable.icon_goods_top9
    )

    constructor(
        content: Context?,
        listener: OnRecycleItemClickListener<TbGoodsItemEntity>
    ) : super() {
        mContext = content
        this.listener = listener
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        var bean = mList[position]
        holder.id_rl_goods_item?.setOnClickListener {
            listener?.onItemClick(it, position, bean)
        }
        holder.id_tv_goods_name?.text = bean.title

        if (bean.coupon_amount.isNullOrEmpty()) {
            holder.id_tv_goods_price?.text = "${bean.zk_final_price}"
        } else {
            var r_price = bean.zk_final_price?.toDoubleOrNull() ?: 0 as Double
            var c_price = bean.coupon_amount?.toDoubleOrNull() ?: 0 as Double
            holder.id_tv_goods_price?.text = Utils.getNoMoreThanTwoDigits(
                Utils.sub(
                    r_price,
                    c_price
                )
            )
        }

        if (bean.pict_url?.startsWith("//img") == true) {
            holder.id_img_goods_img?.setLoadImageUrl("https:" + bean.pict_url, 0, 5)
        } else {
            holder.id_img_goods_img?.setLoadImageUrl(bean.pict_url, 0, 5)
        }

        holder.id_tv_goods_sale_num?.text = "${bean.volume ?: "0"}人付款"
//        holder.id_tv_goods_old_price?.text = "¥${bean.originalPriceDb}"
        holder.id_tv_goods_old_price?.text = "¥${bean.zk_final_price}"
        holder.id_tv_goods_old_price?.paint?.flags = Paint.STRIKE_THRU_TEXT_FLAG
        if (position < 9) {
            holder.id_img_goods_number?.setImageResource(mTopIconList[position])
        }

    }

    override fun getItemCount(): Int {
        if (mList.size > 9) {
            return 9;
        } else {
            return super.getItemCount()
        }
    }

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

    class ViewHolder : RecyclerView.ViewHolder {
        var id_rl_goods_item: RelativeLayout?
        var id_img_goods_img: RecycleImageView?
        var id_tv_goods_name: TextView?
        var id_tv_goods_price: TextView?
        var id_tv_goods_old_price: TextView?
        var id_tv_goods_sale_num: TextView?
        var id_img_goods_number: ImageView?

        constructor(
            itemView: View
        ) : super(itemView) {
            id_rl_goods_item = itemView.findViewById<RelativeLayout>(R.id.id_rl_goods_item)
            id_img_goods_img = itemView.findViewById<RecycleImageView>(R.id.id_img_goods_img)
            id_tv_goods_name = itemView.findViewById<TextView>(R.id.id_tv_goods_name)
            id_tv_goods_price = itemView.findViewById<TextView>(R.id.id_tv_goods_price)
            id_tv_goods_old_price = itemView.findViewById<TextView>(R.id.id_tv_goods_old_price)
            id_tv_goods_sale_num = itemView.findViewById<TextView>(R.id.id_tv_goods_sale_num)
            id_img_goods_number = itemView.findViewById<ImageView>(R.id.id_img_goods_number)
        }
    }

}