package com.cheng.blzb.ui.fragment.mine.coupon.list

import android.text.TextUtils
import android.widget.TextView
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.example.base.extensions.getColor
import com.example.base.extensions.getYYYYMMDD2
import com.example.base.ui.list.LoadMoreAdapter
import com.example.base.utils.SpanUtils
import com.cheng.blzb.R
import com.cheng.blzb.common.Constants
import java.text.DecimalFormat

class CouponAdapter(private var expired: Boolean) : LoadMoreAdapter<com.cheng.blzb.bean.CouponEntity>(R.layout.listitem_coupon) {

    init {
        addChildClickViewIds(R.id.btn_next)
    }

    override fun convert(holder: BaseViewHolder, item: com.cheng.blzb.bean.CouponEntity) {
        holder.setText(
            R.id.tv_name,
            if (!TextUtils.isEmpty(item.coupon_name)) item.coupon_name else "${item.coupon_value_name}${item.coupon_type_name}"
        )
        holder.setText(R.id.tv_expire_time, "有效期至 ${(item.expire_timestamp.toLong() * 1000).getYYYYMMDD2()}")
        holder.setText(R.id.tv_type, item.coupon_type_name)
        holder.getView<TextView>(R.id.tv_discount_amount).typeface = Constants.dDIN_PRO_M
        when (item.coupon_type) {
            com.cheng.blzb.bean.CouponEntity.TYPE_CASH -> {
                SpanUtils.with(holder.getView(R.id.tv_discount_amount))
                    .append("￥")
                    .setFontSize(12, true)
                    .append(DecimalFormat("0.##").format(item.coupon_value.toFloat() / 100))
                    .create()
                if (!TextUtils.isEmpty(item.threshold)) {
                    holder.setText(R.id.tv_threshold_amount, "满${DecimalFormat("0.##").format(item.threshold.toFloat() / 100)}可用")
                } else {
                    holder.setText(R.id.tv_threshold_amount, "无门槛")
                }
            }

            com.cheng.blzb.bean.CouponEntity.TYPE_DISCOUNT -> {
                SpanUtils.with(holder.getView(R.id.tv_discount_amount))
                    .append(DecimalFormat("0.##").format(item.coupon_value.toFloat() * 10))
                    .append("折")
                    .setFontSize(12, true)
                    .create()
                if (!TextUtils.isEmpty(item.threshold)) {
                    holder.setText(R.id.tv_threshold_amount, "满${DecimalFormat("0.##").format(item.threshold.toFloat() / 100)}可用")
                } else {
                    holder.setText(R.id.tv_threshold_amount, "无门槛")
                }
            }
        }

        if (!expired) {
            holder.setTextColor(R.id.tv_discount_amount, getColor(R.color.color_ff5846))
            holder.setTextColor(R.id.tv_threshold_amount, getColor(R.color.color_ff5846))
            holder.setTextColor(R.id.tv_name, getColor(R.color.color_1a1a1a))
            holder.setTextColor(R.id.tv_type, getColor(R.color.color_fb7528))
            holder.setBackgroundResource(R.id.tv_type, R.drawable.shape_10fb7528_cor2)
            holder.setBackgroundResource(R.id.layout_amount, R.drawable.shape_fdf4f2_cor6)
            holder.setGone(R.id.btn_next, false)
            holder.setGone(R.id.iv_expire_tag, true)
        } else {
            holder.setTextColor(R.id.tv_discount_amount, getColor(R.color.color_d8d8d8))
            holder.setTextColor(R.id.tv_threshold_amount, getColor(R.color.color_d8d8d8))
            holder.setTextColor(R.id.tv_name, getColor(R.color.color_d8d8d8))
            holder.setTextColor(R.id.tv_type, getColor(R.color.color_d8d8d8))
            holder.setBackgroundResource(R.id.tv_type, R.drawable.shape_10d8d8d8_cor2)
            holder.setBackgroundResource(R.id.layout_amount, R.drawable.shape_f6f6f6_cor6)
            holder.setGone(R.id.btn_next, true)
            holder.setGone(R.id.iv_expire_tag, false)
        }
    }
}