package com.tgv.malware.ui.main

import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import com.tgv.malware.R
import com.tgv.malware.SpConstObject.mainPageCount
import com.tgv.malware.base.BaseActivity
import com.tgv.malware.bean.FeatureBean.Companion.JUNK_CLEAN
import com.tgv.malware.bean.config.AdConfigBean
import com.tgv.malware.business.ads.AdsMgr
import com.tgv.malware.databinding.ActivityMainBinding
import com.tgv.malware.ui.dialog.AppExitDialog
import com.tgv.malware.ui.dialog.NotificationTurnOnDialog
import com.tgv.malware.ui.dialog.RateDialog
import com.tgv.malware.ui.dialog.StoragePermissionDialog
import com.tgv.malware.ui.dialog.permissionStorageJump
import com.tgv.malware.utils.PermissionUtils.requestNotificationPermission
import com.tgv.malware.utils.PermissionUtils.requestStoragePermission
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch

class MainActivity : BaseActivity<ActivityMainBinding>(ActivityMainBinding::inflate) {

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

    override fun useDefaultImmersive() {
        ViewCompat.setOnApplyWindowInsetsListener(binding.root) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, 0, systemBars.right, systemBars.bottom)
            insetsCallBack?.invoke(systemBars)
            insets
        }

    }

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

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

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

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

    override fun initView() {
        super.initView()
        initDialogFlow()
        AdsMgr.showBanner(binding.flBanner, false)

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

    override fun initListener() {
        super.initListener()
    }

    override fun handleBackCallBack() {
        val dialog = AppExitDialog(this)
        dialog.action = {
            permissionStorageJump(JUNK_CLEAN)
        }
        dialog.showDialog()
    }

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

        viewModel.checkDialog(this)
    }


}