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

import android.annotation.SuppressLint
import android.graphics.Color
import android.util.TypedValue
import android.view.ViewGroup
import android.widget.TextView
import androidx.core.view.setPadding
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.example.base.extensions.getColor
import com.example.base.ui.list.LoadMoreAdapter
import com.example.base.utils.DensityUtils
import com.example.base.utils.SpanUtils
import com.cheng.bole.R
import com.cheng.bole.common.Constants
import java.text.DecimalFormat

class VipMealAdapter : LoadMoreAdapter<com.cheng.bole.bean.VipGoodsEntity>(R.layout.listitem_vip_meal) {

    @SuppressLint("NotifyDataSetChanged")
    override fun convert(holder: BaseViewHolder, item: com.cheng.bole.bean.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) 12 else 10, true)
            .append(DecimalFormat("0.##").format(item.price.toFloat()))
            .setFontSize(if (item.checked) 28 else 22, true)
            .setTypeface(Constants.dDIN_PRO_M)
            .create()
        SpanUtils.with(holder.getView(R.id.tv_origin_price))
            .append("￥${item.origin_price}")
            .setStrikethrough()
            .create()
        holder.setText(R.id.tv_save_price, "限时立省￥${DecimalFormat("0.##").format(item.origin_price.toFloat() - item.price.toFloat())}")

        holder.setBackgroundResource(R.id.tv_save_price, if (item.checked) R.drawable.shape_vip_meal_checked_bottom_bg else R.drawable.shape_vip_meal_default_bottom_bg)
        holder.setBackgroundResource(R.id.layout_content, if (item.checked) R.drawable.shape_vip_meal_check_true else R.drawable.shape_vip_meal_check_false)

        holder.getView<TextView>(R.id.tv_tag).setTextSize(TypedValue.COMPLEX_UNIT_SP, if (item.checked) 12f else 10f)
        holder.getView<TextView>(R.id.tv_goods_name).setTextSize(TypedValue.COMPLEX_UNIT_SP, if (item.checked) 15f else 12f)
        holder.getView<TextView>(R.id.tv_origin_price).setTextSize(TypedValue.COMPLEX_UNIT_SP, if (item.checked) 14f else 10f)
        holder.getView<TextView>(R.id.tv_save_price).setTextSize(TypedValue.COMPLEX_UNIT_SP, if (item.checked) 12f else 10f)

        if (item.checked) {
            holder.itemView.setPadding(0)
            holder.getView<ViewGroup>(R.id.layout_content).setPadding(0, DensityUtils.dp2px(9f),0, 0)
            holder.getView<TextView>(R.id.tv_tag).setPadding(DensityUtils.dp2px(6f), DensityUtils.dp2px(2f), DensityUtils.dp2px(6f), DensityUtils.dp2px(2f))
            holder.getView<TextView>(R.id.tv_save_price).setPadding(0, DensityUtils.dp2px(6f),0, DensityUtils.dp2px(6f))

            holder.setTextColor(R.id.tv_goods_name, getColor(R.color.color_54230c))
            holder.setTextColor(R.id.tv_price, getColor(R.color.color_f94747))
            holder.setTextColor(R.id.tv_origin_price, getColor(R.color.color_896451))
            holder.setTextColor(R.id.tv_save_price, getColor(R.color.color_54220b))
        } else {
            holder.itemView.setPadding(DensityUtils.dp2px(5f), 0, DensityUtils.dp2px(5f),0)
            holder.getView<TextView>(R.id.tv_tag).setPadding(DensityUtils.dp2px(4f), DensityUtils.dp2px(1f), DensityUtils.dp2px(4f), DensityUtils.dp2px(1f))
            holder.getView<ViewGroup>(R.id.layout_content).setPadding(0, DensityUtils.dp2px(7f),0, 0)
            holder.getView<TextView>(R.id.tv_save_price).setPadding(0, DensityUtils.dp2px(4f),0, DensityUtils.dp2px(4f))

            holder.setTextColor(R.id.tv_goods_name, Color.parseColor("#ABABAB"))
            holder.setTextColor(R.id.tv_price, Color.parseColor("#EAEAEA"))
            holder.setTextColor(R.id.tv_origin_price, Color.parseColor("#6B6B6B"))
            holder.setTextColor(R.id.tv_save_price, getColor(R.color.color_80ffffff))
        }

        if (recyclerView.layoutManager is GridLayoutManager) {
            holder.itemView.layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        } else if (recyclerView.layoutManager is LinearLayoutManager) {
            holder.itemView.layoutParams =
                ViewGroup.LayoutParams(DensityUtils.dp2px(if (item.checked) 125f else 110f), ViewGroup.LayoutParams.MATCH_PARENT)
        }
    }
}