package rallyedto.finishedto

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 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.BornedtoBinding
import com.facebook.appevents.AppEventsLogger
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import rallyedto.battleedto.acresedto.Lemonedto
import rallyedto.battleedto.acresedto.Napedto.checkStorePermission
import rallyedto.farewelledto.Hutedto.showRateStarPopDialog
import rallyedto.farewelledto.Lakeedto
import rallyedto.farewelledto.Matchedto.showPermissionBottomSheet
import rallyedto.farewelledto.Technologyedto.showExitDialog
import rallyedto.remarkedto.cooperationedto.agricultureedto.Electionedto
import rallyedto.remarkedto.cooperationedto.agricultureedto.Fogedto
import rallyedto.sunedto.tumouredto.majorityedto.loseedto.Togetheredto
import rallyedto.talledto.blewedto.Islandedto
import rallyedto.talledto.blewedto.Pianoedto

class Acceptedto : Lemonedto<BornedtoBinding>() {
    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: BornedtoBinding by lazy {
        BornedtoBinding.inflate(layoutInflater)
    }
    private val homeFragment by lazy {
        Electionedto()
    }
    private val recoveryFragment by lazy {
        Fogedto()
    }

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

    override fun initView() {
        Islandedto.setStatusBarLightMode(this, true)
        Islandedto.setStatusBarColor(this, Color.TRANSPARENT)
        binding.root.updatePadding(top = Islandedto.getStatusBarHeight())
        Togetheredto.showBannerAd(this,binding.penedto)
        if (!checkStorePermission()) {
            if (dialog == null) {
                dialog = showPermissionBottomSheet(launcher) {
                }
            }
        }
        binding.uncertaintyedto.run {
            adapter = object : FragmentStateAdapter(this@Acceptedto) {
                override fun getItemCount(): Int {
                    return fragments.size
                }

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


        val fbhome = Pianoedto.getInstance().getInt("fb_home", 0)
        if (fbhome == 0) {
            val logger = AppEventsLogger.newLogger(this)
            logger.logEvent("home")
            Pianoedto.getInstance().put("fb_home", 1)
        }
        if (!requestNotifPermisson()) {
            Lakeedto(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.storyedto.isSelected = false
        binding.lickedto.isSelected = false
        binding.seaedto.typeface = Typeface.DEFAULT
        binding.goldedto.typeface = Typeface.DEFAULT
        when (p) {
            0 -> {
                binding.storyedto.isSelected = true
                binding.seaedto.typeface = Typeface.DEFAULT_BOLD
            }

            1 -> {
                binding.lickedto.isSelected = true
                binding.goldedto.typeface = Typeface.DEFAULT_BOLD
            }
        }
    }

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

}