package com.remax.visualnovel.extension

import android.content.Context
import androidx.core.view.doOnPreDraw
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.fragment.app.FragmentPagerAdapter
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.remax.visualnovel.app.base.BaseCommonNavigatorAdapter
import com.remax.visualnovel.widget.indicator.ViewPager2Helper
import net.lucode.hackware.magicindicator.MagicIndicator
import net.lucode.hackware.magicindicator.ViewPagerHelper
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator

/**
 * Created by HJW on 2023/7/18
 */


/**
 * 快速设置MagicIndicator
 * @receiver Context  当前上下文
 * @param fragments List<Fragment>
 * @param navigationAdapter CommonNavigatorAdapter 传入具体子类实现UI样式
 * @param magicIndicator MagicIndicator
 * @param leftPadding Int
 * @param rightPadding Int
 * @param isAdjustMode Boolean
 * @param viewPager ViewPager? 与viewPager绑定
 * @param viewPager2 ViewPager2? 与viewPager2绑定
 * @param currentItem Int
 * @param viewPager2PageSelected Function1<Int, Unit>?
 */
fun Fragment.setMagicIndicator(
    fragments: List<Fragment>,
    navigationAdapter: BaseCommonNavigatorAdapter,
    magicIndicator: MagicIndicator,
    leftPadding: Int = 0,
    rightPadding: Int = 0,
    isAdjustMode: Boolean = false,
    currentItem: Int = 0,
    isUserInputEnabled: Boolean = true,
    viewPager2PageSelected: ((Int) -> Unit)? = null
) {
    requireContext().setMagicIndicatorCommon(
        fragments,
        navigationAdapter,
        magicIndicator,
        leftPadding,
        rightPadding,
        isAdjustMode,
        currentItem,
        isUserInputEnabled,
        viewPager2PageSelected
    )

    navigationAdapter.viewPager2?.adapter = object : FragmentStateAdapter(this) {
        override fun getItemCount() = navigationAdapter.count
        override fun createFragment(position: Int): Fragment = fragments[position]
    }

    navigationAdapter.viewPager?.adapter = object : FragmentPagerAdapter(childFragmentManager, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {
        override fun getItem(position: Int): Fragment = fragments[position]
        override fun getCount() = fragments.size
    }

}

fun FragmentActivity.setMagicIndicator(
    fragments: List<Fragment>,
    navigationAdapter: BaseCommonNavigatorAdapter,
    magicIndicator: MagicIndicator,
    leftPadding: Int = 0,
    rightPadding: Int = 0,
    isAdjustMode: Boolean = false,
    currentItem: Int = 0,
    isUserInputEnabled: Boolean = true,
    viewPager2PageSelected: ((Int) -> Unit)? = null
) {
    navigationAdapter.viewPager2?.adapter = object : FragmentStateAdapter(this) {
        override fun getItemCount() = navigationAdapter.count
        override fun createFragment(position: Int): Fragment = fragments[position]
    }

    navigationAdapter.viewPager?.adapter = object : FragmentPagerAdapter(supportFragmentManager, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {
        override fun getItem(position: Int): Fragment = fragments[position]
        override fun getCount() = fragments.size
    }

    setMagicIndicatorCommon(
        fragments,
        navigationAdapter,
        magicIndicator,
        leftPadding,
        rightPadding,
        isAdjustMode,
        currentItem,
        isUserInputEnabled,
        viewPager2PageSelected
    )

}

private fun Context.setMagicIndicatorCommon(
    fragments: List<Fragment>,
    navigationAdapter: BaseCommonNavigatorAdapter,
    magicIndicator: MagicIndicator,
    leftPadding: Int = 0,
    rightPadding: Int = 0,
    isAdjustMode: Boolean = false,
    currentItem: Int = 0,
    isUserInputEnabled: Boolean = true,
    viewPager2PageSelected: ((Int) -> Unit)? = null
) {
    val commonNavigator = CommonNavigator(this).apply {
        adapter = navigationAdapter
        this.isAdjustMode = isAdjustMode
        this.leftPadding = leftPadding
        this.rightPadding = rightPadding
    }
    magicIndicator.navigator = commonNavigator

    navigationAdapter.viewPager?.let {
        ViewPagerHelper.bind(magicIndicator, it)
        if (fragments.isNotEmpty()) {
            it.offscreenPageLimit = fragments.size
        }
        if (currentItem < fragments.size) {
            it.doOnPreDraw { _ ->
                it.currentItem = currentItem
            }
        }
    }
    navigationAdapter.viewPager2?.let {
        ViewPager2Helper.bind(magicIndicator, it) { position ->
            viewPager2PageSelected?.invoke(position)
        }
        if (fragments.isNotEmpty()) {
            it.offscreenPageLimit = fragments.size
        }
        if (currentItem < fragments.size) {
            it.doOnPreDraw { _ ->
                it.currentItem = currentItem
            }
        }
        it.isUserInputEnabled = isUserInputEnabled
    }
}