package com.gamedog.vididin.adapter

import android.content.Context
import android.view.LayoutInflater
import android.widget.ImageView
import android.widget.TextView
import androidx.core.view.isVisible
import androidx.fragment.app.FragmentActivity
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.getDataFromThemeAttr
import com.gamedog.vididin.R
import com.gamedog.vididin.beans.MainTabsItem
import com.gamedog.vididin.main.interfaces.OnTabClickAgainListener
import com.gamedog.vididin.main.interfaces.OnTabClickRefreshFinishListener
import com.gamedog.vididin.main.interfaces.OnTabClickRefreshListener
import net.lucode.hackware.magicindicator.MagicIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.CommonPagerTitleView
import kotlin.ranges.until
import com.gamedog.vididin.databinding.ItemActivityMainTabBinding as ViewBinding
import com.google.android.material.R as materialR



class MainTabsAdapter(
    private val activity: FragmentActivity,
    private val viewPager2: ViewPager2,
    private val mainFragmentStateAdapter: MainViewPagerAdapter,
    private val magicIndicator: MagicIndicator,
) : AppNavigatorAdapter<MainTabsItem>() {

    companion object {
        val TEXT_TAB = 1
        val IMAGE_TAB = 2
    }


    private var isDarkFont: Boolean = true

    override fun getTabView(context: Context, index: Int, item: MainTabsItem): IPagerTitleView {
        val tabView = CommonPagerTitleView(context)

        val binding = ViewBinding.inflate(LayoutInflater.from(context))
        binding.setTabStyle(IMAGE_TAB)
        tabView.setContentView(binding.root)
        tabView.setColor(isDarkFont, index == viewPager2.currentItem)
        tabView.updateImageSrc(false, item)


        tabView.setOnClickListener {
            binding.onTabClickHandle(index, item)
        }
        tabView.onPagerTitleChangeListener = object: CommonPagerTitleView.OnPagerTitleChangeListener {
            override fun onSelected(index: Int, totalCount: Int) {
                tabView.updateImageSrc(true, item)
            }

            override fun onDeselected(index: Int, totalCount: Int) {
                tabView.updateImageSrc(false, item)
            }

            override fun onLeave(
                index: Int,
                totalCount: Int,
                leavePercent: Float,
                leftToRight: Boolean
            ) {

            }

            override fun onEnter(
                index: Int,
                totalCount: Int,
                enterPercent: Float,
                leftToRight: Boolean
            ) {

            }
        }

        return tabView
    }


    override fun getIndicator(context: Context): IPagerIndicator? = null

    private fun ViewBinding.onTabClickHandle(index: Int, item: MainTabsItem) {
        if (viewPager2.currentItem == index) {
            onTabClickAgainHandle(index)
        } else {
            val isNeedLogin = false
            if (!isNeedLogin) {
                viewPager2.setCurrentItem(index, false)
            } else {
                /*LoginManager.checkLogin(activity) {
                    viewPager2.setCurrentItem(index, false)
                }*/
            }
        }
    }


    private fun ViewBinding.onTabClickAgainHandle(index: Int) {
        val currentFragment = mainFragmentStateAdapter.getFragmentByIndex(index)
        if (currentFragment is OnTabClickRefreshListener) {
            currentFragment.onTabClickRefresh(object : OnTabClickRefreshFinishListener {
                override fun onTabClickRefreshFinish() {
                    setTabStyle(1)
                }
            })
        } else if (currentFragment is OnTabClickAgainListener) {
            currentFragment.onTabClickAgain()
        }
    }


    private fun ViewBinding.setTabStyle(type: Int) {
        when (type) {
            TEXT_TAB -> {
                title.isVisible = true
                image.isVisible = false
            }

            IMAGE_TAB -> {
                title.isVisible = false
                image.isVisible = true
            }
        }
    }

    fun setIsDarkFont(isDarkFont: Boolean) {
        this.isDarkFont = isDarkFont
        setTitleColor(isDarkFont)
        setBackgroundColor(isDarkFont)
    }

    private fun setTitleColor(isDarkFont: Boolean) {
        val titleContainer =
            magicIndicator.navigator.asSafe<CommonNavigator>()?.titleContainer ?: return
        for (i in 0 until titleContainer.childCount) {
            titleContainer.getChildAt(i).asSafe<CommonPagerTitleView>()
                ?.setColor(isDarkFont, i == viewPager2.currentItem)
        }
    }

    private fun CommonPagerTitleView.setColor(
        isDarkFont: Boolean,
        selected: Boolean,
    ) {
        val title = findViewById<TextView>(R.id.title)
        title.alpha = if (selected) 1f else 0.7f
        title.setTextColor(context.getDataFromThemeAttr(if (isDarkFont) materialR.attr.colorOnSurface else materialR.attr.colorOnSurfaceInverse))
    }

    private fun CommonPagerTitleView.updateImageSrc(
        selected: Boolean,
        tabItem: MainTabsItem
    ) {
        val image = findViewById<ImageView>(R.id.image)
        image.setImageResource(if (selected) tabItem.tabIconSelected else tabItem.tabIconNormal)
    }

    private fun setBackgroundColor(
        isDarkFont: Boolean,
    ) {
        val selectedBackgroundColor =
            magicIndicator.context.getDataFromThemeAttr(if (isDarkFont) materialR.attr.colorSurface else materialR.attr.colorSurfaceInverse)
        magicIndicator.setBackgroundColor(selectedBackgroundColor)
    }
}