package com.zxbw.modulemain.adapter

import android.app.Activity
import android.content.Context
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.zxbw.modulemain.R
import com.zxhl.cms.AppContext
import com.zxhl.cms.common.base.BaseRecyclerAdapter
import com.zxhl.cms.net.model.box.AddressEntity
import com.zxhl.cms.net.model.box.LotteryEntity
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 GoodsListAdapter : BaseRecyclerAdapter<TbGoodsItemEntity, GoodsListAdapter.ViewHolder> {
    private var mContext: Activity? = null
    private val listener: OnRecycleItemClickListener<TbGoodsItemEntity>

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

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val bean = mList[position]
        holder.id_tv_goods_name?.text = bean.title
        if (mContext?.isFinishing == false) {
            if(bean.pict_url?.startsWith("//img") == true){
                holder.id_item_goods_img?.setLoadImageUrl("https:"+bean.pict_url, 0, 5)
            }else{
                holder.id_item_goods_img?.setLoadImageUrl(bean.pict_url, 0, 5)
            }
        }
        holder.id_tv_tb_price_value?.text = "淘宝商城价 : ¥${bean.zk_final_price}"
        if (bean.coupon_amount.isNullOrEmpty()){
            holder.id_tv_yhq_value?.visibility =View.GONE
            holder.id_tv_goods_price?.text ="${bean.zk_final_price}"
        }else{
            holder.id_tv_yhq_value?.visibility =View.VISIBLE
            holder.id_tv_yhq_value?.text = "${bean.coupon_amount}元券"
            var r_price = bean.zk_final_price?.toDoubleOrNull() ?: 0.0
            var c_price = bean.coupon_amount?.toDoubleOrNull() ?: 0.0
            holder.id_tv_goods_price?.text = Utils.getNoMoreThanTwoDigits(
                Utils.sub(
                    r_price,
                    c_price
                )
            )
        }

        if (bean.coupon_info.isNullOrEmpty()){
            holder.id_tv_manjian_value?.visibility =View.GONE
        }else{
            holder.id_tv_manjian_value?.visibility =View.VISIBLE
            holder.id_tv_manjian_value?.text = "${bean.coupon_info}"
        }


        holder.id_rl_goods_item_view?.setOnClickListener {
            listener.onItemClick(it, position, bean)
        }
    }

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

    class ViewHolder : RecyclerView.ViewHolder {
        var id_rl_goods_item_view: RelativeLayout?
        var id_item_goods_img: RecycleImageView?
        var id_tv_goods_name: TextView?
        var id_tv_yhq_value: TextView?
        var id_tv_manjian_value: TextView?
        var id_tv_tb_price_value: TextView?
        var id_tv_goods_price: TextView?

        constructor(
            itemView: View
        ) : super(itemView) {
            id_rl_goods_item_view =
                itemView.findViewById<RelativeLayout>(R.id.id_rl_goods_item_view)
            id_item_goods_img = itemView.findViewById<RecycleImageView>(R.id.id_item_goods_img)
            id_tv_goods_name = itemView.findViewById<TextView>(R.id.id_tv_goods_name)
            id_tv_yhq_value = itemView.findViewById<TextView>(R.id.id_tv_yhq_value)
            id_tv_manjian_value = itemView.findViewById<TextView>(R.id.id_tv_manjian_value)
            id_tv_tb_price_value = itemView.findViewById<TextView>(R.id.id_tv_tb_price_value)
            id_tv_goods_price = itemView.findViewById<TextView>(R.id.id_tv_goods_price)


        }
    }
}