package com.base.datarecovery.activity

import android.graphics.Color
import android.graphics.Typeface
import androidx.activity.addCallback
import androidx.core.view.updatePadding
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.base.datarecovery.ads.admob.AdmobBannerUtils
import com.base.datarecovery.databinding.ActivityMainBinding
import com.base.datarecovery.fragment.HomeFragment
import com.base.datarecovery.fragment.RecoveryFragment
import com.base.datarecovery.help.BaseActivity
import com.base.datarecovery.help.PermissionHelp.checkStorePermission
import com.base.datarecovery.utils.AppPreferences
import com.base.datarecovery.utils.BarUtils
import com.base.datarecovery.view.DialogViews.showExitDialog
import com.base.datarecovery.view.PermissionDialog.showPermissionBottomSheet
import com.base.datarecovery.view.RateStarPop.showRateStarPopDialog
import com.facebook.appevents.AppEventsLogger
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

class MainActivity : BaseActivity<ActivityMainBinding>() {

    override val binding: ActivityMainBinding by lazy {
        ActivityMainBinding.inflate(layoutInflater)
    }
    private val homeFragment by lazy {
        HomeFragment()
    }
    private val recoveryFragment by lazy {
        RecoveryFragment()
    }

    private val fragments by lazy {
        mutableListOf(homeFragment, recoveryFragment)
    }

    override fun initView() {
        BarUtils.setStatusBarLightMode(this, true)
        BarUtils.setStatusBarColor(this, Color.TRANSPARENT)
        binding.root.updatePadding(top = BarUtils.getStatusBarHeight())
        if (!checkStorePermission()) {
            if (dialog == null) {
                dialog = showPermissionBottomSheet(launcher) {
                    AdmobBannerUtils.showCollapsibleBannerAd(this, binding.flBanner)
                }
            }
        } else {
            AdmobBannerUtils.showCollapsibleBannerAd(this, binding.flBanner)
        }
        binding.viewpager2.run {
            adapter = object : FragmentStateAdapter(this@MainActivity) {
                override fun getItemCount(): Int {
                    return fragments.size
                }

                override fun createFragment(position: Int): Fragment {
                    return fragments[position]
                }
            }
        }
        binding.viewpager2.registerOnPageChangeCallback(object :
            ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                setPageIndicate(position)
            }
        })


        val fbhome = AppPreferences.getInstance().getInt("fb_home", 0)
        if (fbhome == 0) {
            val logger = AppEventsLogger.newLogger(this)
            logger.logEvent("home")
            AppPreferences.getInstance().put("fb_home", 1)
        }

    }

    private fun setPageIndicate(p: Int) {
        binding.ll1.isSelected = false
        binding.ll2.isSelected = false
        binding.tv1.typeface = Typeface.DEFAULT
        binding.tv2.typeface = Typeface.DEFAULT
        when (p) {
            0 -> {
                binding.ll1.isSelected = true
                binding.tv1.typeface = Typeface.DEFAULT_BOLD
            }

            1 -> {
                binding.ll2.isSelected = true
                binding.tv2.typeface = Typeface.DEFAULT_BOLD
            }
        }
    }

    override fun initListener() {
        super.initListener()
        onBackPressedDispatcher.addCallback {
            showExitDialog()
        }
        binding.ll1.setOnClickListener {
            //            BarUtils.setStatusBarColor(this, Color.parseColor("#F4F5F9"))
            binding.viewpager2.currentItem = 0
        }
        binding.ll2.setOnClickListener {
//            BarUtils.setStatusBarColor(this, Color.parseColor("#F4F5F9"))
            binding.viewpager2.currentItem = 1
        }

    }

    override fun onResume() {
        super.onResume()

        lifecycleScope.launch(Dispatchers.Main) {
            delay(1000)
            showRateStarPopDialog()
        }
    }

}