package com.zxhl.cms.pay


import android.graphics.Paint
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
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


/**
 * 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 val mPricecolor: Int
    private val mBottomTextcolor: 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)
        mPricecolor = AppContext.get().resources.getColor(R.color.color_999999)
        mBottomTextcolor = AppContext.get().resources.getColor(R.color.e4d8ce)

    }


    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)
        )

    }

    override fun onBindViewHolder(holder: MemberHolder, pos: Int) {
        val data = mList[pos]

        val width = (PhoneUtils.getScreenWidth(AppContext.get()) - Utils.dip2px(60)) / 3
        val height: Float = width * 1.15f
        holder.mLayout.setLayoutParams(RelativeLayout.LayoutParams(width, height.toInt()))

        if (pos == position) {
            holder.mLayout.setBackgroundResource(R.drawable.member_item_select)
            holder.mPrice.setTextColor(mSelectColor)
            holder.mDate.setTextColor(mSelectColor)
            holder.mRmb.setTextColor(mSelectColor)
            holder.mYuanjia.setTextColor(mSelectColor)
            holder.id_tv_yuanjia_rmb.setTextColor(mSelectColor)
            mListener.onItemClick(holder.itemView, pos, data)
        } else {
            holder.mLayout.setBackgroundResource(R.drawable.member_item_unselect)
            holder.mPrice.setTextColor(mUnSelectColor)
            holder.mDate.setTextColor(mUnSelectColor)
            holder.mRmb.setTextColor(mUnSelectColor)
            holder.mYuanjia.setTextColor(mUnSelectColor)
            holder.id_tv_yuanjia_rmb.setTextColor(mUnSelectColor)
        }
        holder.mZengson.text = data.buttonText
        holder.mDate.text = data.name
        //  holder.mYuanjia.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG) //中划线
        holder.mYuanjia.text = data.title + "/" + data.name
        //   holder.mTitle.text = data.name
        holder.mPrice.text = data.unit

//        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 mZengson: TextView
        var mDate: TextView
        var mRmb: TextView
        var mYuanjia: TextView
        var id_tv_yuanjia_rmb: TextView

        constructor(itemView: View) : super(itemView) {
            mLayout = itemView.findViewById(R.id.id_item_member_price_layout)
            mZengson = itemView.findViewById(R.id.id_tv_zengsong)
            mPrice = itemView.findViewById(R.id.id_item_member_price_value)
            mDate = itemView.findViewById(R.id.id_tv_date)
            mRmb = itemView.findViewById(R.id.id_tv_rmb)
            mYuanjia = itemView.findViewById(R.id.id_tv_yuan_jia)
            id_tv_yuanjia_rmb = itemView.findViewById(R.id.id_tv_yuanjia_rmb)

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