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

import android.util.TypedValue
import android.view.ViewGroup
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.graphics.toColorInt
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 com.flyjingfish.gradienttextviewlib.GradientTextView
import java.text.DecimalFormat

class GuideVipAdapter: BaseQuickAdapter<VipGoodsEntity, BaseViewHolder>(R.layout.listitem_guide_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)
        SpanUtils.with(holder.getView(R.id.tv_price))
            .append("￥")
            .setFontSize(if (item.checked) 14 else 11, true)
            .append(DecimalFormat("0.##").format(item.price.toFloat()))
            .setFontSize(if (item.checked) 34 else 29, true)
            .create()
        SpanUtils.with(holder.getView(R.id.tv_origin_price))
            .append("￥${item.origin_price}")
            .setStrikethrough()
            .create()

        holder.setBackgroundResource(R.id.layout_content, if (item.checked) R.mipmap.ic_guide_vip_goods_checked else R.mipmap.ic_guide_vip_goods_default)

        holder.getView<TextView>(R.id.tv_tag).setTextSize(TypedValue.COMPLEX_UNIT_SP, if (item.checked) 10f else 8f)
        holder.getView<TextView>(R.id.tv_goods_name).setTextSize(TypedValue.COMPLEX_UNIT_SP, if (item.checked) 20f else 16f)
        holder.getView<TextView>(R.id.tv_origin_price).setTextSize(TypedValue.COMPLEX_UNIT_SP, if (item.checked) 10f else 8f)

        holder.getView<TextView>(R.id.tv_goods_name).typeface = Constants.pmzdbt
        holder.getView<TextView>(R.id.tv_price).typeface = Constants.dDIN_PRO_M

        val parentWidth = recyclerView.width
        val minWidth = (0.3 * parentWidth).toInt()

        val layoutContent = holder.getView<ViewGroup>(R.id.layout_content)
        val layoutParams =  layoutContent.layoutParams as ConstraintLayout.LayoutParams
        if (item.checked) {
            layoutParams.dimensionRatio = "h,122:148"
            layoutContent.layoutParams = layoutParams

            holder.itemView.layoutParams = ViewGroup.LayoutParams(parentWidth - minWidth * 2 - DensityUtils.dp2px(20f), ViewGroup.LayoutParams.MATCH_PARENT)

            holder.getView<TextView>(R.id.tv_tag).setPadding(DensityUtils.dp2px(6f), DensityUtils.dp2px(2f), DensityUtils.dp2px(6f), DensityUtils.dp2px(2f))

            val tvGoodsName = holder.getView<GradientTextView>(R.id.tv_goods_name)
            tvGoodsName.angle = 270f
            tvGoodsName.gradientColors = intArrayOf("#20F3E6".toColorInt(), "#05ACE8".toColorInt())
            holder.setTextColor(R.id.tv_price, getColor(R.color.white))
            holder.setTextColor(R.id.tv_origin_price, getColor(R.color.color_a6acb5))
        } else {
            layoutParams.dimensionRatio = "h,100:118"
            layoutContent.layoutParams = layoutParams

            holder.itemView.layoutParams = ViewGroup.LayoutParams(minWidth, ViewGroup.LayoutParams.MATCH_PARENT)

            holder.getView<TextView>(R.id.tv_tag).setPadding(DensityUtils.dp2px(4f), DensityUtils.dp2px(1f), DensityUtils.dp2px(4f), DensityUtils.dp2px(1f))

            val tvGoodsName = holder.getView<GradientTextView>(R.id.tv_goods_name)
            tvGoodsName.angle = 0f
            tvGoodsName.gradientColors = intArrayOf("#FFFFFF".toColorInt(), "#61D0FC".toColorInt())
            holder.setTextColor(R.id.tv_price, getColor(R.color.color_9bbbdf))
            holder.setTextColor(R.id.tv_origin_price, getColor(R.color.color_a6acb5))
        }
    }
}