package com.abbidot.tracker.adapter

import android.content.Context
import android.view.View
import android.widget.RelativeLayout
import com.abbidot.baselibrary.list.BaseRecyclerAdapter
import com.abbidot.baselibrary.list.RecyclerViewHolder
import com.abbidot.tracker.R
import com.abbidot.tracker.bean.CreditCardBean
import com.abbidot.tracker.ui.activity.subscribe.PaymentMethodActivity
import com.abbidot.tracker.ui.fragment.subscribe.CreditCardPaymentFragment
import com.abbidot.tracker.util.Util
import com.abbidot.tracker.widget.TypefaceRoundButton

/**
 *Created  by .yzq on 2024/3/21/021.
 * @link
 * @description:
 */
class CreditCardAdapter(
    ctx: Context, list: MutableList<CreditCardBean>?
) : BaseRecyclerAdapter<CreditCardBean>(ctx, list) {
    override fun getEmptyLayoutId(viewType: Int) = R.layout.layout_credit_card_no_data

    override fun getItemLayoutId(viewType: Int) = R.layout.item_credit_card_layout

    override fun bindData(holder: RecyclerViewHolder?, position: Int, item: CreditCardBean) {
        val relativeLayout = holder!!.itemView as RelativeLayout
        val primary = holder.getView(R.id.rb_credit_card_primary_item) as TypefaceRoundButton
        if (item.checked) {
            relativeLayout.setBackgroundResource(R.drawable.shape8_dark_border_pale_yellow_bg)
        } else {
            relativeLayout.setBackgroundResource(R.drawable.shape8_dark_gray_border_white_yellow_bg)
        }
        primary.visibility = if (item.isPrimary) View.VISIBLE else View.GONE
        holder.getTextView(R.id.tv_credit_card_number_item).apply {
            val formatCardNumber = Util.formatCardNumber(item.cardNumber)
            val cardLogoImageId = Util.getCreditCardType(item.cardNumber)
            text = formatCardNumber
            if (cardLogoImageId == 0) setCompoundDrawablesWithIntrinsicBounds(
                0, 0, 0, 0
            )
            else setCompoundDrawablesWithIntrinsicBounds(
                cardLogoImageId, 0, 0, 0
            )
        }
        holder.getView(R.id.iv_credit_card_edit_btn).setOnClickListener {
            (((mContext as PaymentMethodActivity).mFragments[0]) as CreditCardPaymentFragment).goEditCard(
                item
            )
        }
    }
}