package com.remax.visualnovel.widget.indicator

import android.annotation.SuppressLint
import android.content.Context
import com.remax.visualnovel.app.base.BaseCommonNavigatorAdapter
import com.remax.visualnovel.entity.response.AIDictItem
import com.remax.visualnovel.extension.setOnClick
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView

class SecondaryNavigatorDictAdapter(
    private val mTitleList: List<AIDictItem>,
) : BaseCommonNavigatorAdapter(null, null) {

    var indexClickBlock: ((Int) -> Unit)? = null

    override fun getCount(): Int {
        return mTitleList.size
    }

    @SuppressLint("SetTextI18n")
    override fun getTitleView(context: Context, index: Int): IPagerTitleView {
        val secondaryPagerTitleView = SecondaryPagerTitleView(context)
        val currItem = mTitleList[index]
        secondaryPagerTitleView.getTitleTextView().text = "#${currItem.name}"
        if (currItem.select) {
            secondaryPagerTitleView.onSelected(index, mTitleList.size)
        } else {
            secondaryPagerTitleView.onDeselected(index, mTitleList.size)
        }
        setOnClick(secondaryPagerTitleView) {
            mTitleList.forEach { tag ->
                if (tag == currItem) tag.select = !tag.select
            }
            if (currItem.select) {
                secondaryPagerTitleView.onSelected(index, mTitleList.size)
            } else {
                secondaryPagerTitleView.onDeselected(index, mTitleList.size)
            }
            indexClickBlock?.invoke(index)
        }
        return secondaryPagerTitleView
    }

    override fun getIndicator(context: Context): IPagerIndicator? {
        return null
    }
}