package com.zxhl.cms.pay

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.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.PhoneUtils
import com.zxhl.cms.utils.Utils

class StarCoinPayAdapter :
    BaseRecyclerAdapter<MemberEntity.ProductDetail, StarCoinPayAdapter.MemberHolder> {

    private var position = -1

    private val mSelectColor: Int
    private val mUnSelectColor: Int

    private val mListener: OnRecycleItemClickListener<MemberEntity.ProductDetail>

    constructor(mListener: OnRecycleItemClickListener<MemberEntity.ProductDetail>) : super() {
        this.mListener = mListener
        mSelectColor = AppContext.get().resources.getColor(R.color.e2c7b2)
        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_layout_star_coin_pay, p0, false)
        )

    }

    override fun onBindViewHolder(holder: MemberHolder, pos: Int) {
        val data = mList[pos]
//        if(pos>0){
//            val width = (PhoneUtils.getScreenWidth(AppContext.get()) - Utils.dip2px(80)) / 3
//            holder.mLayout.setLayoutParams(RelativeLayout.LayoutParams(width, width))
//        }else{
//            val width = PhoneUtils.getScreenWidth(AppContext.get()) - Utils.dip2px(40)
//            holder.mLayout.setLayoutParams(RelativeLayout.LayoutParams(width, 100))
//        }

        if (pos == position) {
            holder.mLayout.setBackgroundResource(R.drawable.member_item_select)
            holder.mPrice.setTextColor(mSelectColor)
            holder.mStarCoin.setTextColor(mSelectColor)
            holder.mRmb.setTextColor(mSelectColor)
            mListener.onItemClick(holder.itemView, pos, data)
        } else {
            holder.mLayout.setBackgroundResource(R.drawable.member_item_unselect)
            holder.mPrice.setTextColor(mUnSelectColor)
            holder.mStarCoin.setTextColor(mUnSelectColor)
            holder.mRmb.setTextColor(mUnSelectColor)
        }
        if(!TextUtils.isEmpty(data.buttonText)){
            holder.mZengson.visibility=View.VISIBLE
            holder.mZengson.text=data.buttonText
        }else{
            holder.mZengson.visibility=View.GONE
        }
        holder.mStarCoin.text=data.name
        holder.mPrice.text = data.title
    }

    inner class MemberHolder : RecyclerView.ViewHolder {

        var mLayout: LinearLayout
        var mPrice: TextView
        var mZengson: TextView
        var mStarCoin: TextView
        var mRmb: TextView

        constructor(itemView: View) : super(itemView) {
            mLayout = itemView.findViewById(R.id.id_item_coin_price_layout)
            mZengson = itemView.findViewById(R.id.id_tv_zengsong)
            mPrice = itemView.findViewById(R.id.id_item_member_price_value)
            mStarCoin = itemView.findViewById(R.id.id_tv_star_coin)
            mRmb= itemView.findViewById(R.id.id_tv_rmb)

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