package com.hongyegroup.cpt_parttime.adapter

import android.view.View
import com.chad.library.adapter.base.entity.node.BaseNode
import com.chad.library.adapter.base.provider.BaseNodeProvider
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.guadou.lib_baselib.utils.CommUtils
import com.hongyegroup.cpt_parttime.R
import com.hongyegroup.cpt_parttime.bean.PartDetailQuestion

internal class QuestionNodeProvider : BaseNodeProvider() {
    override val itemViewType: Int = 0

    override val layoutId: Int = R.layout.item_part_detail_qa_q

    override fun convert(helper: BaseViewHolder, data: BaseNode) {
        val pos = helper.layoutPosition

        // 数据类型需要自己强转
        val question = data as PartDetailQuestion
        helper.setText(R.id.tv_detail_qa_q, question.question)
            .setImageResource(
                R.id.iv_arraw_icon,
                if (question.isExpanded) R.drawable.expand_down_icon else R.drawable.expand_right_icon
            )
            .setTextColor(
                R.id.tv_detail_qa_q,
                if (question.isExpanded) CommUtils.getColor(R.color.black) else CommUtils.getColor(R.color.dark_blue)
            )
            .setGone(R.id.view_bottom_line, !question.isExpanded)
            .setGone(R.id.view_top_line, pos == 0)   //第1个Item隐藏掉顶部线

        val llCategoryBox = helper.getView<View>(R.id.ll_item_q)

        llCategoryBox.setOnClickListener {
            if (question.isExpanded) {
                //当前展开-需要收起
                helper.setImageResource(R.id.iv_arraw_icon, R.drawable.expand_right_icon)
                    .setTextColor(R.id.tv_detail_qa_q, CommUtils.getColor(R.color.dark_blue))
                    .setGone(R.id.view_bottom_line, true)
            } else {
                //当前收起-需要展开
                helper.setImageResource(R.id.iv_arraw_icon, R.drawable.expand_down_icon)
                    .setTextColor(R.id.tv_detail_qa_q, CommUtils.getColor(R.color.black))
                    .setGone(R.id.view_bottom_line, false)
            }

            getAdapter()?.expandOrCollapse(pos)
        }
    }

}