package com.cheng.blzb.ui.fragment.mine.vip

import android.graphics.Typeface
import android.view.ViewGroup
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.setPadding
import androidx.recyclerview.widget.GridLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.cheng.blzb.R
import com.cheng.blzb.bean.VipGoodsEntity
import com.cheng.blzb.common.Constants
import com.example.base.extensions.getColor
import com.example.base.utils.DensityUtils
import com.example.base.utils.SpanUtils
import java.text.DecimalFormat

class VipGoodsAdapter(var type: Int) : BaseQuickAdapter<VipGoodsEntity, BaseViewHolder>(R.layout.listitem_vip_goods) {

    override fun convert(holder: BaseViewHolder, item: VipGoodsEntity) {
        holder.setGone(R.id.tv_tag, true)
        if (item.tips.isNotEmpty()) {
            holder.setVisible(R.id.tv_tag, true)
            holder.setText(R.id.tv_tag, item.tips)
        }

        holder.setText(R.id.tv_goods_name, item.goods_name)
        holder.getView<TextView>(R.id.tv_goods_name).textSize = if (item.checked) 18f else 12f
        holder.getView<TextView>(R.id.tv_goods_name).typeface = if (item.checked) Typeface.DEFAULT_BOLD else Typeface.DEFAULT

        SpanUtils.with(holder.getView(R.id.tv_price))
            .append("￥")
            .setFontSize(if (item.checked) 12 else 10, true)
            .append(DecimalFormat("0.##").format(item.price.toFloat()))
            .setFontSize(if (item.checked) 30 else 22, true)
            .setTypeface(Constants.dDIN_PRO_M)
            .create()
        SpanUtils.with(holder.getView(R.id.tv_origin_price))
            .append("￥${item.origin_price}")
            .setFontSize(if (item.checked) 13 else 10, true)
            .setStrikethrough()
            .create()

        val layoutContent = holder.getView<ViewGroup>(R.id.layout_content)
        val layoutParams = layoutContent.layoutParams as ConstraintLayout.LayoutParams
        if (item.checked) {
            holder.itemView.setPadding(0)

            layoutParams.dimensionRatio = "h,124:134"
            layoutContent.layoutParams = layoutParams

            if (type == 0) {
                holder.setTextColor(R.id.tv_goods_name, getColor(R.color.color_7e4024))
                holder.setTextColor(R.id.tv_price, getColor(R.color.color_f11e1e))
                holder.setTextColor(R.id.tv_origin_price, getColor(R.color.color_797069))
                holder.setBackgroundResource(R.id.layout_content, R.mipmap.ic_personal_goods_checked)
            } else {
                holder.setTextColor(R.id.tv_goods_name, getColor(R.color.color_c5ffe1))
                holder.setTextColor(R.id.tv_price, getColor(R.color.color_f6fbd0))
                holder.setTextColor(R.id.tv_origin_price, getColor(R.color.color_779086))
                holder.setBackgroundResource(R.id.layout_content, R.mipmap.ic_enterprise_goods_checked)
            }
        } else {
            holder.itemView.setPadding(DensityUtils.dp2px(10f))

            layoutParams.dimensionRatio = "h,96:104"
            layoutContent.layoutParams = layoutParams

            if (type == 0) {
                holder.setTextColor(R.id.tv_goods_name, getColor(R.color.color_fffaee))
                holder.setTextColor(R.id.tv_price, getColor(R.color.color_fffaee))
                holder.setTextColor(R.id.tv_origin_price, getColor(R.color.color_797069))
                holder.setBackgroundResource(R.id.layout_content, R.mipmap.ic_personal_goods_default)
            } else {
                holder.setTextColor(R.id.tv_goods_name, getColor(R.color.color_9eb9ab))
                holder.setTextColor(R.id.tv_price, getColor(R.color.color_9eb9ab))
                holder.setTextColor(R.id.tv_origin_price, getColor(R.color.color_56665f))
                holder.setBackgroundResource(R.id.layout_content, R.mipmap.ic_enterprise_goods_default)
            }
        }

        if (recyclerView.layoutManager is GridLayoutManager) {
            holder.itemView.layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
        } else {
            val itemWidth = (recyclerView.width - DensityUtils.dp2px(50f)) / 3
            holder.itemView.layoutParams = ViewGroup.LayoutParams(itemWidth, ViewGroup.LayoutParams.MATCH_PARENT)
        }
    }
}