package com.gamedog.vididin.main



import android.content.Intent
import android.content.IntentFilter
import com.ama.core.common.util.asSafe
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentManager.FragmentLifecycleCallbacks
import androidx.viewpager2.widget.ViewPager2
import android.view.LayoutInflater
import androidx.activity.addCallback
import androidx.activity.viewModels
import com.ama.core.architecture.appBase.AppViewsActivity
import com.ama.core.architecture.appBase.OnFragmentBackgroundListener
import com.ama.core.architecture.ext.toast
import com.ama.core.architecture.util.bindViewPager2
import com.ama.core.architecture.util.setCommonNavigator
import com.ama.core.architecture.util.setDataOrAdapter
import com.gamedog.vididin.R
import com.gamedog.vididin.VidiConst
import com.gamedog.vididin.VididinEvents
import com.gamedog.vididin.adapter.MainTabsAdapter
import com.gamedog.vididin.adapter.MainViewPagerAdapter
import com.gamedog.vididin.main.fragments.task.DailySignSuccessDialog
import com.gamedog.vididin.main.interfaces.OnTabStyleListener
import com.gamedog.vididin.manager.DateChangeReceiver
import dagger.hilt.android.AndroidEntryPoint
import kotlin.getValue
import com.gamedog.vididin.databinding.ActivityMainBinding as ViewBinding
import com.gamedog.vididin.main.MainUiState as UiState
import com.gamedog.vididin.main.MainViewModel as ViewModel




@AndroidEntryPoint
class MainActivity : AppViewsActivity<ViewBinding, UiState, ViewModel>(), OnTabStyleListener {

    override val mViewModel: ViewModel by viewModels()
    private lateinit var navigatorAdapter: MainTabsAdapter
    private val fragmentStateAdapter by lazy { MainViewPagerAdapter(this) }
    private lateinit var mDateChangeReceiver: DateChangeReceiver



    override fun inflateViewBinding(inflater: LayoutInflater) = ViewBinding.inflate(inflater)

    override fun ViewBinding.initWindowInsets() {
        ViewCompat.setOnApplyWindowInsetsListener(magicIndicator) { v, insets ->
            val systemBars =
                insets.getInsets(WindowInsetsCompat.Type.navigationBars() or WindowInsetsCompat.Type.displayCutout())
            v.setPadding(
                systemBars.left,
                36,
                systemBars.right,
                systemBars.bottom + 36
            )
            insets
        }
    }


    override fun ViewBinding.initViews() {
        content.foreground.alpha = 0
        navigatorAdapter = MainTabsAdapter(
            this@MainActivity, viewPager2, fragmentStateAdapter, magicIndicator
        )
        magicIndicator.setCommonNavigator { isAdjustMode = true }
        magicIndicator.bindViewPager2(viewPager2)

        viewPager2.isUserInputEnabled = false
        viewPager2.setPageTransformer { _, _ -> }
        viewPager2.offscreenPageLimit


        BeginnerGiftDialog(this@MainActivity).show()
    }

    override fun ViewBinding.initListeners() {
        mDateChangeReceiver = DateChangeReceiver()
        val filter = IntentFilter(Intent.ACTION_DATE_CHANGED)
        registerReceiver(mDateChangeReceiver, filter)

        onBackPressedDispatcher.addCallback(this@MainActivity) {
            if (mViewModel.canBack) {
                finish()
            } else {
                toast(R.string.close_app_hint)
                mViewModel.startCanBackTimer()
            }
        }

        viewPager2.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
            }
        })

        supportFragmentManager.registerFragmentLifecycleCallbacks(object :
            FragmentLifecycleCallbacks() {
            override fun onFragmentResumed(fm: FragmentManager, f: Fragment) {
                super.onFragmentResumed(fm, f)
                val isBackgroundBright =
                    f.asSafe<OnFragmentBackgroundListener>()?.isBackgroundBright
                        ?: return

                onTabIsDarkFont(isBackgroundBright)
            }
        }, false)

        registerEvents( { data->
            when (data?.mEventType) {
                VididinEvents.Event_AD_TASK_TYPE_Watch_Ad_Earn_Gold-> {
                    DailySignSuccessDialog(this@MainActivity).initData(VidiConst.WATCH_AD_REWARD_GOLD.toInt(), false).show()
                }
            }

        }, VididinEvents.Event_AD_TASK_TYPE_Watch_Ad_Earn_Gold)
    }

    override fun ViewBinding.initObservers() {

    }


    override fun ViewBinding.onUiStateCollect(uiState: UiState) {
        magicIndicator.setDataOrAdapter(uiState.tabs) { navigatorAdapter }
        viewPager2.setDataOrAdapter(uiState.tabs, Int.MAX_VALUE) { fragmentStateAdapter }
    }


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

    override fun onDestroy() {
        super.onDestroy()
        unregisterReceiver(mDateChangeReceiver)
    }

    override fun onTabIsDarkFont(isDarkFont: Boolean) {
        navigatorAdapter.setIsDarkFont(isDarkFont)
    }

    fun switchTab(itemIndex: Int) {
        binding.viewPager2.setCurrentItem(itemIndex, false)
    }




}