package com.gamedog.vididin.main.fragments

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.updatePadding
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentManager.FragmentLifecycleCallbacks
import androidx.fragment.app.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.viewpager2.widget.ViewPager2
import com.ama.core.architecture.appBase.AppViewsFragment
import com.ama.core.architecture.appBase.OnFragmentBackgroundListener
import com.ama.core.architecture.util.setStatusBarDarkFont
import com.ama.core.common.util.asSafe
import com.gamedog.vididin.main.fragments.home.HomeFragmentStateAdapter
import com.gamedog.vididin.main.fragments.home.fragment.HomeItemFragment
import com.gamedog.vididin.main.interfaces.OnSwitchTabListener
import com.gamedog.vididin.main.interfaces.OnTabStyleListener
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch
import kotlin.getValue
import com.gamedog.vididin.databinding.VididinappFeatureHomeFragmentHomeBinding as ViewBinding
import com.gamedog.vididin.main.fragments.home.YoutubeViewModel as ViewModel
import com.gamedog.vididin.main.fragments.home.YoutubeUiState as UiState



@AndroidEntryPoint
class HomeFragment : AppViewsFragment<ViewBinding, UiState, ViewModel>(), OnSwitchTabListener, OnFragmentBackgroundListener {

    override val mViewModel: ViewModel by viewModels()
    override var isBackgroundBright: Boolean = true

    private val mViewPagerAdapter by lazy { HomeFragmentStateAdapter(this) }

    override fun inflateViewBinding(
        inflater: LayoutInflater,
        container: ViewGroup?,
    ) = ViewBinding.inflate(inflater, container, false)

    override fun ViewBinding.initWindowInsets() {
        ViewCompat.setOnApplyWindowInsetsListener(viewPager2) { v, insets ->
            val systemBars =
                insets.getInsets(WindowInsetsCompat.Type.systemBars() or WindowInsetsCompat.Type.displayCutout())
            v.updatePadding(top = systemBars.top)
            insets
        }
    }

    override fun ViewBinding.initViews() {
        viewPager2.setPageTransformer { _, _ -> }
        viewPager2.offscreenPageLimit = 1
        viewPager2.adapter = mViewPagerAdapter
    }

    override fun ViewBinding.initListeners() {
        viewPager2.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
                setHomeTabStyle(mViewPagerAdapter.getFragmentByIndex(position))
                val curFragment: HomeItemFragment = mViewPagerAdapter.getFragmentByIndex(position) as HomeItemFragment
                curFragment.loadVideo()

                // load more
                if (mViewPagerAdapter.itemCount > 0 && position == mViewPagerAdapter.itemCount - 2) {
                    lifecycleScope.launch {
                        mViewModel.loadVideoPage()
                    }
                }
            }

            override fun onPageScrollStateChanged(state: Int) {
                super.onPageScrollStateChanged(state)
            }
        })

        childFragmentManager.registerFragmentLifecycleCallbacks(object :
            FragmentLifecycleCallbacks() {
            override fun onFragmentResumed(fm: FragmentManager, fra: Fragment) {
                super.onFragmentResumed(fm, fra)
                setHomeTabStyle(fra)
            }
        }, false)
    }

    override fun ViewBinding.initObservers() {

    }

    override fun ViewBinding.onUiStateCollect(uiState: UiState) {
        mViewPagerAdapter.submitList(uiState.playLists)
        //viewPager2.setDataOrAdapter(uiState.playLists, 1) { mViewPagerAdapter }
    }

    override fun onResume() {
        super.onResume()
        setStatusBarDarkFont(isDarkFont = isBackgroundBright)
    }

    override fun onPause() {
        super.onPause()
    }

    override fun onSwitchTab(index: Int, smoothScroll: Boolean) {
        var item = index
        if (index < 0 || index >= mViewPagerAdapter.itemCount) {
            item = mViewPagerAdapter.itemCount - 1
        }
        binding?.viewPager2?.setCurrentItem(item, smoothScroll)
    }



    private fun setHomeTabStyle(f: Fragment?) {
        isBackgroundBright = f.asSafe<OnFragmentBackgroundListener>()?.isBackgroundBright ?: return
        activity.asSafe<OnTabStyleListener>()?.onTabIsDarkFont(isBackgroundBright)
    }

    companion object {
        internal fun newInstance() = HomeFragment()
    }

}