package com.zxbw.modulemain.adapter

import android.app.Activity
import android.graphics.Paint
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.ProgressBar
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.SkillGoodsEntity
import com.zxhl.cms.utils.OnRecycleItemClickListener
import com.zxhl.cms.widget.RecycleImageView

class FlashSaleAdapter : BaseRecyclerAdapter<SkillGoodsEntity, FlashSaleAdapter.ViewHolder> {
    private var mContext: Activity? = null
    private val listener: OnRecycleItemClickListener<SkillGoodsEntity>

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

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        var bean = mList[position]
        holder.id_tv_status?.text = bean.skillStatusDesc
        if (TextUtils.equals(bean.skillStatus, "1") || TextUtils.equals(bean.skillStatus, "2")) {
            if (!TextUtils.isEmpty(bean.currentStore)) {
                if (bean.currentStore!!.toInt() > 0) {
                    holder.id_tv_qianggou?.setBackgroundResource(R.drawable.button_qiang)
                }else{
                    holder.id_tv_qianggou?.setBackgroundResource(R.drawable.button_bukeqiang)
                    holder.id_tv_status?.text = "已抢光"
                }
            }
        } else {
            holder.id_tv_qianggou?.setBackgroundResource(R.drawable.button_bukeqiang)
        }
        if(!TextUtils.isEmpty(bean.store)){
            holder.goumai_progress?.max = bean.store!!.toInt()
            holder.goumai_progress?.progress = bean.currentStore!!.toInt()
        }
        if (!TextUtils.equals(bean.goodsType, "99")) {

            holder.id_img_member_bg?.setLoadImageUrl(bean.icon, false)
          //  holder.id_img_icon?.setLoadImageUrl(bean.card?.icon, false)
            holder.id_tv_name?.text =
                "${bean.card?.brand}${bean.card?.category}${bean.card?.goodsName}"
            holder.id_tv_sale_price?.text = "${bean.skillPrice}"
            holder.id_tv_yuanjia?.text = "${bean.card?.originPrice}"

        } else {
            holder.id_img_member_bg?.setLoadImageUrl(bean.icon, false)
            holder.id_tv_name?.text = "${bean.goods?.productName}"
            holder.id_tv_sale_price?.text = "${bean.skillPrice}"
            holder.id_tv_yuanjia?.text = "${bean.goods?.originalPriceDb}"
        }
        holder.id_tv_yuanjia?.getPaint()?.setFlags(Paint.STRIKE_THRU_TEXT_FLAG) //中划线

//        holder.id_ll_item?.setOnClickListener {
//            val intent = Intent(mContext, SkillPayActivity::class.java)
//            intent.putExtra("skillgoods", bean)
//            mContext?.startActivity(intent)
//        }
        holder.id_ll_item?.setOnClickListener {
            if (TextUtils.equals(bean.skillStatus, "1") || TextUtils.equals(bean.skillStatus, "2")) {
                listener.onItemClick(it, position, bean)
            }

        }

    }

    override fun getItemCount(): Int {
        val size = mList.size
        if (size > 3) {
            return 3
        } else {
            return size
        }
    }

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

    class ViewHolder : RecyclerView.ViewHolder {
        var id_ll_item: LinearLayout?
        var id_img_member_bg: RecycleImageView?
       // var id_img_icon: RecycleImageView?
        var id_tv_name: TextView?
        var id_tv_sale_price: TextView?
        var id_tv_yuanjia: TextView?
        var id_tv_status: TextView?
        var id_tv_qianggou: TextView?
        var goumai_progress: ProgressBar?

        constructor(
            itemView: View
        ) : super(itemView) {
            id_ll_item = itemView.findViewById(R.id.id_ll_item)
            id_img_member_bg = itemView.findViewById(R.id.id_img_member_bg)
          //  id_img_icon = itemView.findViewById(R.id.id_img_icon)
            id_tv_name = itemView.findViewById(R.id.id_tv_name)
            id_tv_sale_price = itemView.findViewById(R.id.id_tv_sale_price)
            id_tv_yuanjia = itemView.findViewById(R.id.id_tv_yuanjia)
            id_tv_status = itemView.findViewById(R.id.id_tv_status)
            id_tv_qianggou = itemView.findViewById(R.id.id_tv_qianggou)
            goumai_progress = itemView.findViewById(R.id.goumai_progress)
        }
    }
}