package com.abbidot.tracker.adapter

import android.content.Context
import android.content.res.ColorStateList
import android.text.TextUtils
import android.view.View
import androidx.core.content.ContextCompat
import androidx.core.graphics.toColorInt
import com.abbidot.baselibrary.list.BaseRecyclerAdapter
import com.abbidot.baselibrary.list.RecyclerViewHolder
import com.abbidot.baselibrary.util.Utils
import com.abbidot.tracker.R
import com.abbidot.tracker.bean.PackageBean
import com.abbidot.tracker.constant.ConstantInt
import com.abbidot.tracker.constant.ConstantString
import com.qmuiteam.qmui.widget.roundwidget.QMUIRoundButtonDrawable
import com.qmuiteam.qmui.widget.roundwidget.QMUIRoundRelativeLayout

/**
 *Created  by .yzq on 2024/8/6/006.
 * @link
 * @description:
 */
class ChoosePlanAdapter(
    ctx: Context, list: MutableList<PackageBean>?
) : BaseRecyclerAdapter<PackageBean>(ctx, list) {
    override fun getEmptyLayoutId(viewType: Int) = 0

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

    override fun bindData(holder: RecyclerViewHolder?, position: Int, item: PackageBean) {
        holder?.apply {
            getView(R.id.rl_choose_plan_shade_item).let {
                if (item.selected) it.setBackgroundResource(R.drawable.shape20_yellow_border_cyan_bg)
                else it.setBackgroundResource(0)
            }

            setText(R.id.tv_choose_plan_item_name, item.planName)

//            val price = if (item.discountedPrice > 0) item.discountedPrice else item.planPrice
            getTextView(R.id.tv_choose_plan_item_money).apply {
                text = String.format(
                    mContext.getString(R.string.txt_money_unit),
                    Utils.formatDecimal(item.planPrice / item.planTimeMonthsCount, 2)
                )
            }

            getTextView(R.id.tv_choose_plan_item_plan_money).text = String.format(
                mContext.getString(R.string.txt_money_unit), Utils.formatDecimal(item.planPrice, 2)
            )

            getTextView(R.id.tv_choose_plan_item_unit).text =
                if (item.mealUnit == ConstantString.PackageUnitDay) mContext.getString(R.string.txt_per_day)
                else mContext.getString(R.string.txt_per_month)

            getTextView(R.id.tv_choose_plan_item_billed_money).text = item.billedMode


            getView(R.id.ll_choose_plan_item_discount_percent).let {
                it.visibility = if (item.popularType == ConstantInt.Type1) View.INVISIBLE
                else View.INVISIBLE
            }
            getView(R.id.iv_choose_plan_item_popular_image).let {
                it.visibility = if (item.popularType == ConstantInt.Type1) View.VISIBLE
                else View.INVISIBLE
            }
//            getTextView(R.id.tv_choose_plan_item_discount_percent).let {
//                it.text = "${item.discount}%"
//            }

            getView(R.id.cv_choose_plan_item_popular).let {
                if (item.popularType == ConstantInt.Type1) it.visibility = View.GONE
                else it.visibility = View.GONE
            }

            (getView(R.id.rl_choose_plan_item_price) as QMUIRoundRelativeLayout).let {
                (it.background as QMUIRoundButtonDrawable).let { drawable ->
                    val bgColor = if (TextUtils.isEmpty(item.bottomBgColor)) {
                        ContextCompat.getColor(mContext, R.color.tracker_manage_bg_color)
                    } else {
                        item.bottomBgColor.toColorInt()
                    }
                    drawable.setBgData(ColorStateList.valueOf(bgColor))
                }
            }

            getTextView(R.id.tv_choose_plan_item_power).let {
                it.visibility = if (item.list.isNullOrEmpty()) View.GONE
                else {
                    var str = getManyLanguageLimitDec(item.listTitleCode)
                    if (TextUtils.isEmpty(str)) {
                        for (i in 0 until item.list!!.size) {
                            val packageDec = item.list!![i]
                            str += "${i + 1}.${packageDec.title}"
                            if (i != item.list!!.size - 1) {
                                str += "\n"
                            }
                        }
                    }
                    it.text = str
                    View.VISIBLE
                }
            }
        }
    }

    private fun getManyLanguageLimitDec(nameCode: Int): String {
        return when (nameCode) {
            ConstantInt.Type1 -> mContext.getString(R.string.txt_package_permission1)
            ConstantInt.Type2 -> mContext.getString(R.string.txt_package_permission2)
            ConstantInt.Type3 -> mContext.getString(R.string.txt_package_permission3)
            else -> ""
        }
    }
}