package com.abbidot.tracker.adapter

import android.content.Context
import android.content.Intent
import android.view.View
import androidx.recyclerview.widget.RecyclerView
import com.abbidot.baselibrary.list.BaseRecyclerAdapter
import com.abbidot.baselibrary.list.RecyclerViewHolder
import com.abbidot.baselibrary.util.AppUtils
import com.abbidot.tracker.R
import com.abbidot.tracker.base.BaseActivity
import com.abbidot.tracker.bean.ParentBadgeBean
import com.abbidot.tracker.constant.ConstantString
import com.abbidot.tracker.ui.activity.pet.BadgeDetailActivity
import com.abbidot.tracker.util.ViewUtil
import com.abbidot.tracker.widget.TypefaceButton

/**
 *Created  by .yzq on 2021/12/30/030.
 * @link
 * @description:
 */
class PetsParentBadgesAdapter(ctx: Context, list: MutableList<ParentBadgeBean>?) :
    BaseRecyclerAdapter<ParentBadgeBean>(ctx, list) {

    //默认显示的勋章个数
    private val defaultShowCount = 9

    override fun getEmptyLayoutId(viewType: Int) = 0

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

    override fun bindData(holder: RecyclerViewHolder?, position: Int, item: ParentBadgeBean) {
        val recyclerView = holder!!.getView(R.id.rv_pet_badges_list) as RecyclerView
        holder.setText(R.id.tv_pets_parent_badges_name, item.parentName)
        val btn = (holder.getView(R.id.btn_parent_badges_expand_fold) as TypefaceButton).apply {
            visibility = View.GONE
            if (item.isExpand) {
                setText(R.string.txt_view_less)
            } else {
                setText(R.string.txt_see_all)
            }
            setOnClickListener {
                item.isExpand = !item.isExpand
                notifyItemChanged(position)
            }
        }
        item.badgeList?.apply {
            if (size > defaultShowCount) {
                btn.visibility = View.VISIBLE
            } else {
                btn.visibility = View.GONE
            }
        }

        val list = if (item.isExpand || null == item.badgeList || item.badgeList!!.size < 4) {
            item.badgeList
        } else {
            item.badgeList!!.subList(0, defaultShowCount)
        }
        if (null == recyclerView.adapter) {
            val petsBadgesAdapter = PetsBadgesAdapter(mContext, list).apply {
                setOnItemClickListener(object : OnItemClickListener {
                    override fun onItemClick(itemView: View?, pos: Int) {
                        val activity = mContext as BaseActivity<*>
                        Intent(mContext, BadgeDetailActivity::class.java).let {
                            it.putExtra(ConstantString.PetLabels, list!![pos])
                            activity.startActivity(it)
                        }
                    }
                })
            }

            ViewUtil.instance.setRecyclerViewGridLayout(
                mContext,
                recyclerView,
                petsBadgesAdapter,
                3,
                AppUtils.dpToPx(80),
                top = AppUtils.dpToPx(16),
                bottom = AppUtils.dpToPx(18),
                offset = AppUtils.dpToPx(32)
            )
        } else {
            val adapter = recyclerView.adapter as PetsBadgesAdapter
            adapter.setData(list, true)
        }
    }
}