package waific.Teflonic.therapistic

import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.easy.clean.R
import tooic.fatalityic.Runawayic.mainPageCount
import tooic.fatalityic.jittersic.Reinstateic
import tooic.fatalityic.corroborateic.Protegeic.Companion.JUNK_CLEAN
import tooic.fatalityic.corroborateic.Fineic
import tooic.fatalityic.corroborateic.indexic.Lurkic
import autonomyic.polemicalic.congestionic.Unwindic
import com.easy.clean.databinding.RummyicBinding
import com.easy.clean.databinding.ElfinicBinding
import rememberic.Hereditaryic
import rememberic.Euphoricic
import rememberic.Subsequentic
import rememberic.Categoricallyic
import rememberic.permissionStorageJump
import overthrewic.dirtyic.coneic.Curbic
import tooic.fatalityic.redic.speckledic.hankeringic.Oustic.requestNotificationPermission
import tooic.fatalityic.redic.speckledic.hankeringic.Oustic.requestStoragePermission
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayout.OnTabSelectedListener
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch

class Hauntingic : Reinstateic<RummyicBinding>(RummyicBinding::inflate) {

    val viewModel by lazy { ViewModelProvider(this)[Transitionalic::class.java] }

    lateinit var exitDialog: Hereditaryic
    private val fragmentList = listOf(
//        Mussic(),
        Rotundaic(),
        Curbic(),
//        Superstaric()
    )

    private val viewPagerAdapter by lazy {
        object : FragmentStateAdapter(this) {
            override fun getItemCount(): Int = fragmentList.size

            override fun createFragment(position: Int): Fragment = fragmentList[position]
        }
    }

    override fun initView() {
        super.initView()

        initTab()
        initViewPager2()
        initDialogFlow()
        Unwindic.showBanner(binding.formalityic, false)
        exitDialog = Hereditaryic(this)
        exitDialog.init()
        exitDialog.clean = {
            permissionStorageJump(JUNK_CLEAN)
        }
        setStatusBarColor(getColor(R.color.robic))

        val lp = binding.hypersensitiveic.layoutParams as ConstraintLayout.LayoutParams
        if (!Lurkic.adsConfigBean.isAdShow) {
            lp.setMargins(0, 0, 0, 0)
        } else {
            lp.setMargins(0, 0, 0, resources.getDimensionPixelSize(R.dimen.nudeic))
        }
        binding.hypersensitiveic.layoutParams = lp
    }

    private fun initDialogFlow() {
        lifecycleScope.launch {
            viewModel.dialogFlow.collectLatest { dialogType ->

                when (dialogType) {
                    "StoreDialog" -> {
                        val dialog = Categoricallyic(this@Hauntingic)
                        dialog.action = {
                            requestStoragePermission(launcher) {
                                viewModel.nextShowDialog = false
                            }
                        }
                        dialog.showDialog()
                    }

                    "NotificationDialog" -> {
                        val dialog = Euphoricic(this@Hauntingic)
                        dialog.action = {
                            requestNotificationPermission(launcher) {
                                viewModel.nextShowDialog = false
                            }
                        }
                        dialog.showDialog()
                    }

                    "RateDialog" -> {
                        val dialog = Subsequentic(this@Hauntingic)
                        dialog.showDialog(false)
                    }
                }
            }
        }
    }

    private fun initViewPager2() {
        binding.regalic.adapter = viewPagerAdapter
        binding.regalic.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                val defaultTab = binding.nationalityic.getTabAt(position)
                defaultTab?.select()
                defaultTab?.setSelected()
            }
        })
    }

    private fun initTab() {
        arrayOf(
            Fineic(R.drawable.pidginic, getString(R.string.infractionic)),
//            Fineic(R.drawable.tab_recovery_selector, getString(R.string.chromiumic)),
            Fineic(R.drawable.cleaningic, getString(R.string.pulloveric)),
        ).forEachIndexed { index, homeTab ->
            val tab = binding.nationalityic.newTab()
            val view = LayoutInflater.from(this).inflate(R.layout.elfinic, null)
            val tabBinding = ElfinicBinding.bind(view)
            tabBinding.slutic.setImageResource(homeTab.icon)
            tabBinding.waysideic.text = homeTab.tab
//            tabBinding.waysideic.setTextColor(ContextCompat.getColor(this, R.color.robic))
            tab.customView = tabBinding.root
            tab.id = index
            if (index == 1) {
                tabBinding.needsic.visibility = View.VISIBLE
            } else {
                tabBinding.needsic.visibility = View.GONE
            }
            binding.nationalityic.addTab(tab)
        }
        binding.nationalityic.addOnTabSelectedListener(object : OnTabSelectedListener {
            override fun onTabSelected(tab: TabLayout.Tab?) {
                tab?.let {
                    if (binding.regalic.currentItem == tab.id) return
                    binding.regalic.setCurrentItem(tab.id, false)
                    tab.setSelected()
                }
            }

            override fun onTabUnselected(tab: TabLayout.Tab?) {
                tab?.setUnselected()
            }

            override fun onTabReselected(tab: TabLayout.Tab?) {

            }
        })
    }

    fun TabLayout.Tab.setSelected() {
        val textView = customView?.findViewById<TextView>(R.id.waysideic)
        textView?.setTextColor(
            ContextCompat.getColor(this@Hauntingic, R.color.robic)
        )
    }

    fun TabLayout.Tab.setUnselected() {
        val textView = customView?.findViewById<TextView>(R.id.waysideic)
        textView?.setTextColor(Color.parseColor("#999999"))
    }

    override fun onResumeOneShoot() {
        super.onResumeOneShoot()
        mainPageCount = mainPageCount + 1

        viewModel.checkDialog(this)
        viewModel.refreshRecycleBin()
    }

    override fun handleBackCallBack() {
        exitDialog.show()
    }
}