package com.zxhl.cms.pay


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.RelativeLayout
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.zxhl.cms.AppContext
import com.zxhl.cms.R
import com.zxhl.cms.common.base.BaseRecyclerAdapter
import com.zxhl.cms.net.model.video.MemberEntity
import com.zxhl.cms.utils.OnRecycleItemClickListener
import com.zxhl.cms.utils.Utils


/**
 * Created by gaoleichao on 2021/8/30
 */
class PayAdapter : BaseRecyclerAdapter<MemberEntity.ProductDetail, PayAdapter.MemberHolder> {

    private var position = -1

    private val mSelectColor: Int
    private val mUnSelectColor: Int
    private var itemlayout: Int
    private val mListener: OnRecycleItemClickListener<MemberEntity.ProductDetail>

    constructor(
        mListener: OnRecycleItemClickListener<MemberEntity.ProductDetail>,
        itemlayout: Int
    ) : super() {
        this.mListener = mListener
        this.itemlayout = itemlayout
        mSelectColor = AppContext.get().resources.getColor(R.color.color_333333)
        mUnSelectColor = AppContext.get().resources.getColor(R.color.color_333333)

    }


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


    override fun onCreateViewHolder(p0: ViewGroup, p1: Int): MemberHolder {
        return MemberHolder(
            LayoutInflater.from(p0.context).inflate(R.layout.item_member_price, p0, false)
        )
//        if (itemlayout == 1) {
//            return MemberHolder(
//                LayoutInflater.from(p0.context).inflate(R.layout.item_member_price, p0, false)
//            )
//        } else {
//            return MemberHolder(
//                LayoutInflater.from(p0.context).inflate(R.layout.item_member_price_2, p0, false)
//            )
//        }

    }

    override fun onBindViewHolder(holder: MemberHolder, pos: Int) {
        val data = mList[pos]
        Log.e("MXL", "pos" + position)
        if (pos == position) {
            holder.mLayout.setBackgroundResource(R.drawable.member_item_select)
            holder.mPrice.setTextColor(mSelectColor)
            //  holder.mTitle.setTextColor(mSelectColor)
            //  holder.mIcon.setTextColor(mSelectColor)
            mListener.onItemClick(holder.itemView, pos, data)
        } else {
            holder.mLayout.setBackgroundResource(R.drawable.member_item_unselect)
            holder.mPrice.setTextColor(mUnSelectColor)
            //      holder.mTitle.setTextColor(mUnSelectColor)
            //      holder.mIcon.setTextColor(mUnSelectColor)
        }
        if (!TextUtils.isEmpty(data.buttonText)) {
            holder.id_ll_limit_song.visibility = View.VISIBLE
            holder.mLabel.visibility = View.VISIBLE
            holder.mLabel.text = "${data.buttonText}"
        } else {
            holder.mLabel.visibility = View.GONE
            holder.id_ll_limit_song.visibility = View.GONE
        }
        //   holder.mTitle.text = data.name
        holder.mPrice.text = data.title
        holder.mPriceMoney.text =
            AppContext.get().resources.getString(R.string.box_detail_open_box_money_unit) + data.title
//        if (pos == 2) {
//            holder.mTips.visibility = View.VISIBLE
//        } else {
//            holder.mTips.visibility = View.GONE
//        }

        //    holder?.mPriceDesc?.text = data.buttonText


    }

    inner class MemberHolder : RecyclerView.ViewHolder {

        var mLayout: RelativeLayout
        var mPrice: TextView
        var mPriceMoney: TextView
        var mLabel: TextView
        var id_ll_limit_song: LinearLayout

        constructor(itemView: View) : super(itemView) {
            mLayout = itemView.findViewById(R.id.id_item_member_price_layout)
            mPrice = itemView.findViewById(R.id.id_item_member_price_value)
            mPriceMoney = itemView.findViewById(R.id.id_item_price_money)
            mLabel = itemView.findViewById(R.id.id_item_label_tv)
            id_ll_limit_song = itemView.findViewById(R.id.id_ll_limit_song)

            itemView?.setOnClickListener {
                val pos = adapterPosition
                if (pos >= 0 && pos < mList.size) {
                    setPosition(pos)
                    notifyDataSetChanged()
                }
            }
        }
    }
}