package importantedto.appliededto.surgeonedto.promiseedto

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.PipeedtoBinding
import com.facebook.appevents.AppEventsLogger
import importantedto.admireedto.sweptedto.testedto.huntedto.storageedto.AdmobBannerUtils
import importantedto.afteredto.constitutionaledto.Murderedto
import importantedto.afteredto.constitutionaledto.Rackedto.checkStorePermission
import importantedto.candidateedto.Conscienceedto
import importantedto.candidateedto.Problemedto
import importantedto.gallonedto.Transcendedto
import importantedto.gallonedto.Waitedto
import importantedto.maidedto.witnessedto.nationalismedto.Coveredto.showPermissionBottomSheet
import importantedto.maidedto.witnessedto.nationalismedto.Photocopyedto.showExitDialog
import importantedto.maidedto.witnessedto.nationalismedto.Wrestleedto
import importantedto.maidedto.witnessedto.nationalismedto.Yellowedto.showRateStarPopDialog
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

class Byedto : Murderedto<PipeedtoBinding>() {
    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: PipeedtoBinding by lazy {
        PipeedtoBinding.inflate(layoutInflater)
    }
    private val homeFragment by lazy {
        Problemedto()
    }
    private val recoveryFragment by lazy {
        Conscienceedto()
    }

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

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

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


        val fbhome = Waitedto.getInstance().getInt("fb_home", 0)
        if (fbhome == 0) {
            val logger = AppEventsLogger.newLogger(this)
            logger.logEvent("home")
            Waitedto.getInstance().put("fb_home", 1)
        }
        if (!requestNotifPermisson()) {
            Wrestleedto(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.islandedto.isSelected = false
        binding.driveredto.isSelected = false
        binding.cupedto.typeface = Typeface.DEFAULT
        binding.vehicleedto.typeface = Typeface.DEFAULT
        when (p) {
            0 -> {
                binding.islandedto.isSelected = true
                binding.cupedto.typeface = Typeface.DEFAULT_BOLD
            }

            1 -> {
                binding.driveredto.isSelected = true
                binding.vehicleedto.typeface = Typeface.DEFAULT_BOLD
            }
        }
    }

    override fun initListener() {
        super.initListener()
        onBackPressedDispatcher.addCallback {
            showExitDialog()
        }
        binding.islandedto.setOnClickListener {
            //            Transcendedto.setStatusBarColor(this, Color.parseColor("#F4F5F9"))
            binding.relatededto.currentItem = 0
        }
        binding.driveredto.setOnClickListener {
//            Transcendedto.setStatusBarColor(this, Color.parseColor("#F4F5F9"))
            binding.relatededto.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
        }
    }

}