package rowedto.jockeyedto.sheetedto.atticedto

import android.Manifest
import android.app.NotificationManager
import android.content.Intent
import android.graphics.Color
import android.graphics.Typeface
import android.os.Build
import android.provider.Settings
import android.view.View
import androidx.activity.addCallback
import androidx.activity.result.contract.ActivityResultContracts
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.databinding.HouseedtoBinding
import com.facebook.appevents.AppEventsLogger
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import rowedto.jockeyedto.sheetedto.balsamedto.formaledto.orientationedto.Marketedto.showPermissionBottomSheet
import rowedto.jockeyedto.sheetedto.balsamedto.formaledto.orientationedto.Patedto.showExitDialog
import rowedto.jockeyedto.sheetedto.balsamedto.formaledto.orientationedto.Prematureedto.showRateStarPopDialog
import rowedto.jockeyedto.sheetedto.balsamedto.formaledto.orientationedto.Tractionedto
import rowedto.jockeyedto.sheetedto.knewedto.economicedto.copyrightedto.Crossingedto
import rowedto.jockeyedto.sheetedto.knewedto.economicedto.copyrightedto.Studiededto.checkStorePermission
import rowedto.jockeyedto.sheetedto.raceedto.pavementedto.Spareedto
import rowedto.jockeyedto.sheetedto.raceedto.pavementedto.Stewardedto
import rowedto.jockeyedto.sheetedto.slideedto.Awareedto
import rowedto.jockeyedto.sheetedto.slideedto.Shakeedto
import rowedto.jockeyedto.sheetedto.strangeedto.pyramidedto.particlesedto.bounceedto.auditoredto.AdmobBannerUtils

class Moneyedto : Crossingedto<HouseedtoBinding>() {
    private val requestPermission =
        registerForActivityResult(ActivityResultContracts.RequestPermission()) {
            if (!it) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    val intent = Intent()
                    intent.action = Settings.ACTION_APP_NOTIFICATION_SETTINGS
                    intent.putExtra(Settings.EXTRA_APP_PACKAGE, this.packageName)
                    startActivity(intent)
                }
            }

        }
    override val binding: HouseedtoBinding by lazy {
        HouseedtoBinding.inflate(layoutInflater)
    }
    private val homeFragment by lazy {
        Awareedto()
    }
    private val recoveryFragment by lazy {
        Shakeedto()
    }

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

    override fun initView() {
        Spareedto.setStatusBarLightMode(this, true)
        Spareedto.setStatusBarColor(this, Color.TRANSPARENT)
        binding.root.updatePadding(top = Spareedto.getStatusBarHeight())
        if (!checkStorePermission()) {
            if (dialog == null) {
                dialog = showPermissionBottomSheet(launcher) {
                    binding.depositedto.visibility = View.VISIBLE
                    AdmobBannerUtils.showCollapsibleBannerAd(this, binding.depositedto)
                }
            }
        } else {
            binding.depositedto.visibility = View.VISIBLE
            AdmobBannerUtils.showCollapsibleBannerAd(this, binding.depositedto)
        }
        binding.guiltedto.run {
            adapter = object : FragmentStateAdapter(this@Moneyedto) {
                override fun getItemCount(): Int {
                    return fragments.size
                }

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


        val fbhome = Stewardedto.getInstance().getInt("fb_home", 0)
        if (fbhome == 0) {
            val logger = AppEventsLogger.newLogger(this)
            logger.logEvent("home")
            Stewardedto.getInstance().put("fb_home", 1)
        }
        if (!requestNotifPermisson()) {
            Tractionedto(this) {
                if (Build.VERSION.SDK_INT >= 33) {
                    requestPermission.launch(Manifest.permission.POST_NOTIFICATIONS)
                } else {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                        val intent = Intent()
                        intent.action = Settings.ACTION_APP_NOTIFICATION_SETTINGS
                        intent.putExtra(Settings.EXTRA_APP_PACKAGE, this.packageName)
                        startActivity(intent)
                    }
                }

            }.show()
        }
    }

    private fun setPageIndicate(p: Int) {
        binding.campaignedto.isSelected = false
        binding.wardrobeedto.isSelected = false
        binding.circumstanceedto.typeface = Typeface.DEFAULT
        binding.fragmentedto.typeface = Typeface.DEFAULT
        when (p) {
            0 -> {
                binding.campaignedto.isSelected = true
                binding.circumstanceedto.typeface = Typeface.DEFAULT_BOLD
            }

            1 -> {
                binding.wardrobeedto.isSelected = true
                binding.fragmentedto.typeface = Typeface.DEFAULT_BOLD
            }
        }
    }

    override fun initListener() {
        super.initListener()
        onBackPressedDispatcher.addCallback {
            showExitDialog()
        }
        binding.campaignedto.setOnClickListener {
            //            Spareedto.setStatusBarColor(this, Color.parseColor("#F4F5F9"))
            binding.guiltedto.currentItem = 0
        }
        binding.wardrobeedto.setOnClickListener {
//            Spareedto.setStatusBarColor(this, Color.parseColor("#F4F5F9"))
            binding.guiltedto.currentItem = 1
        }

    }

    override fun onResume() {
        super.onResume()

        lifecycleScope.launch(Dispatchers.Main) {
            delay(1000)
            showRateStarPopDialog()
        }
    }
    private fun requestNotifPermisson(): Boolean {
        val mNotificationManager =
            this.getSystemService(NOTIFICATION_SERVICE) as NotificationManager
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            return mNotificationManager.importance != NotificationManager.IMPORTANCE_NONE
        } else {
            return true
        }
    }

}