package com.remax.visualnovel.widget.indicator

import android.content.Context
import androidx.viewpager.widget.ViewPager
import androidx.viewpager2.widget.ViewPager2
import com.remax.visualnovel.app.base.BaseCommonNavigatorAdapter
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 SecondaryNavigatorAdapter(
    private val mTitleList: List<String>,
    override val viewPager2: ViewPager2? = null,
    override val viewPager: ViewPager? = null,
    private val lockList: List<Boolean>? = null,
) : BaseCommonNavigatorAdapter(viewPager2, viewPager) {

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

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

    override fun getTitleView(context: Context, index: Int): IPagerTitleView {
        val secondaryPagerTitleView =
            if (lockList == null) {
                SecondaryPagerTitleView(context)
            } else {
                SecondaryPagerLockedTitleView(context)
            }


        (secondaryPagerTitleView as? SecondaryPagerLockedTitleView)?.let {
            val isLock = lockList?.getOrNull(index) == true
            it.setIcon(isLock)
            it.getTitleTextView().text = mTitleList[index]
        }

        (secondaryPagerTitleView as? SecondaryPagerMoreTitleView)?.let {
            it.getTitleTextView().text = mTitleList[index]
        }

        (secondaryPagerTitleView as? SecondaryPagerTitleView)?.let {
            it.getTitleTextView().text = mTitleList[index]
        }

        setOnClick(secondaryPagerTitleView) {
            indexClickBlock?.invoke(index)
            viewPager2?.let {
                it.currentItem = index
            }
            viewPager?.let {
                it.currentItem = index
            }
        }


        return secondaryPagerTitleView
    }

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