package com.ama.core.architecture.util

import android.view.animation.OvershootInterpolator
import androidx.viewpager2.widget.ViewPager2
import com.ama.core.architecture.appBase.adapter.AppNavigatorAdapter
import com.ama.core.common.util.asSafe
import com.ama.core.common.util.asUnsafe
import net.lucode.hackware.magicindicator.FragmentContainerHelper
import net.lucode.hackware.magicindicator.MagicIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator
import kotlin.apply


fun MagicIndicator.bindViewPager2(
    viewPager2: ViewPager2,
    useFragmentContainerHelper: Boolean = false,
    fragmentContainerHelperConfig: (FragmentContainerHelper.() -> Unit)? = {
        setInterpolator(OvershootInterpolator(1.0f))
        setDuration(200)
    },
) = apply {
    var fragmentContainerHelper: FragmentContainerHelper? = null
    if (useFragmentContainerHelper) {
        fragmentContainerHelper = FragmentContainerHelper(this)
        fragmentContainerHelperConfig?.invoke(fragmentContainerHelper)
    }

    viewPager2.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
        override fun onPageScrolled(
            position: Int,
            positionOffset: Float,
            positionOffsetPixels: Int,
        ) {
            super.onPageScrolled(position, positionOffset, positionOffsetPixels)
            if (!useFragmentContainerHelper) {
                // 用ViewPagerHelper的处理
                this@bindViewPager2.onPageScrolled(position, positionOffset, positionOffsetPixels)
            }
        }

        override fun onPageSelected(position: Int) {
            super.onPageSelected(position)
            if (!useFragmentContainerHelper) {
                this@bindViewPager2.onPageSelected(position)
                this@bindViewPager2.onPageScrolled(position, 0f, 0)
            } else {
                fragmentContainerHelper?.handlePageSelected(position)
            }
        }

        override fun onPageScrollStateChanged(state: Int) {
            super.onPageScrollStateChanged(state)
            if (!useFragmentContainerHelper) {
                this@bindViewPager2.onPageScrollStateChanged(state)
            }
        }
    })
}


fun MagicIndicator.setCommonNavigator(
    commonNavigatorInit: (CommonNavigator.() -> Unit)? = null,
) =
    CommonNavigator(context).apply {
        commonNavigatorInit?.invoke(this)
        setNavigator(this)
    }


fun <T> MagicIndicator.setDataOrAdapter(
    list: List<T>?,
    createNavigatorAdapter: () -> AppNavigatorAdapter<T>,
) {
    var commonNavigator = navigator.asSafe<CommonNavigator>()
    if (commonNavigator == null) {
        commonNavigator = CommonNavigator(context)
        setNavigator(commonNavigator)
    }
    val commonNavigatorAdapter = commonNavigator.adapter
    if (commonNavigatorAdapter == null) {
        // set adapter
        val adapter = createNavigatorAdapter().apply { submitList(list) }
        commonNavigator.adapter = adapter
    } else {
        // update adapter
        commonNavigatorAdapter.asUnsafe<AppNavigatorAdapter<T>>().submitList(list)
    }
}