package com.zxbw.modulemain.adapter

import android.app.Activity
import android.graphics.Color
import android.graphics.Paint
import android.text.TextUtils
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
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.video.MemberEntity
import com.zxhl.cms.utils.OnRecycleItemClickListener

class RechargeAdapter :
    BaseRecyclerAdapter<MemberEntity.ProductDetail, RechargeAdapter.ViewHolder> {
    private var mContext: Activity? = null
    private val listener: OnRecycleItemClickListener<MemberEntity.ProductDetail>
    private val mSelectColor: Int
    private val mUnSelectColor: Int
    private var position = -1
    private var price:String?=""
    constructor(
        content: Activity?,
        listener: OnRecycleItemClickListener<MemberEntity.ProductDetail>
    ) : super() {
        mContext = content
        this.listener = listener
        mSelectColor = AppContext.get().resources.getColor(R.color.color_ba7e36)
        mUnSelectColor = AppContext.get().resources.getColor(R.color.color_333333)
    }

    fun setPosition(pos: Int) {
        position = pos
        notifyDataSetChanged()
    }

    fun updateData( discountprice: String) {
        price=discountprice
        notifyDataSetChanged()
    }

    override fun onBindViewHolder(holder: ViewHolder, pos: Int) {
        var bean = mList[pos]
        holder.id_tv_activity_price_text?.text = "活动价"
        holder.id_tv_price?.text = "${bean.cash}"
        if (pos == position) {
            if(!TextUtils.isEmpty(price)){
                Log.e("MXL","更新ui")
                holder.id_tv_activity_price_text?.text="已减${price}元"
                holder.id_tv_price?.text = "${bean.cash?.toDoubleOrNull()?.minus(price?.toDoubleOrNull()!!)}"
            }

            holder.id_ll_item?.setBackgroundResource(R.drawable.shape_edd49d_r6)
            holder.id_tv_activity_price_text?.setBackgroundResource(R.drawable.shape_edd49d_r11)
            holder.id_tv_activity_price_text?.setTextColor(mSelectColor)
            holder.id_tv_price?.setTextColor(mSelectColor)
            holder.id_tv_orgin_price?.setTextColor(mSelectColor)
            holder.id_tv_rmb?.setTextColor(mSelectColor)
            //  listener.onItemClick(holder.itemView, pos, bean)
        } else {
            holder.id_ll_item?.setBackgroundResource(R.drawable.shape_cbcbcb_r6)
            holder.id_tv_activity_price_text?.setBackgroundResource(R.drawable.shape_a3a4a6_r10)
            holder.id_tv_activity_price_text?.setTextColor(Color.parseColor("#ffffff"))
            holder.id_tv_price?.setTextColor(mUnSelectColor)
            holder.id_tv_orgin_price?.setTextColor(Color.parseColor("#999999"))
            holder.id_tv_rmb?.setTextColor(mUnSelectColor)
        }


        holder.id_tv_orgin_price?.text = "原价 ¥${bean.originalPrice}"
        holder.id_tv_orgin_price?.getPaint()?.setFlags(Paint.STRIKE_THRU_TEXT_FLAG)

    }

//    override fun getItemCount(): Int {
//        return 3
//    }

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

    inner class ViewHolder : RecyclerView.ViewHolder {
        var id_ll_item: LinearLayout?

        // var id_goods_icon: RecycleImageView?
        var id_tv_activity_price_text: TextView?
        var id_tv_rmb: TextView?
        var id_tv_price: TextView?
        var id_tv_orgin_price: TextView?


        constructor(
            itemView: View
        ) : super(itemView) {
            id_ll_item =
                itemView.findViewById<LinearLayout>(R.id.id_ll_item)
            id_tv_activity_price_text =
                itemView.findViewById<TextView>(R.id.id_tv_activity_price_text)
            id_tv_rmb = itemView.findViewById<TextView>(R.id.id_tv_rmb)
            id_tv_price = itemView.findViewById<TextView>(R.id.id_tv_price)
            id_tv_orgin_price = itemView.findViewById<TextView>(R.id.id_tv_orgin_price)

            id_ll_item?.setOnClickListener {
                val pos = adapterPosition
                Log.e("MXL", "pos:" + pos)
                if (pos >= 0 && pos < mList.size) {
                    setPosition(pos)
                    listener.onItemClick(it, pos, mList[pos])
                }
            }
        }
    }
}