package com.base.appzxhy.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.base.appzxhy.R
import com.base.appzxhy.SpConstObject.mainPageCount
import com.base.appzxhy.base.BaseActivity
import com.base.appzxhy.bean.config.AdConfigBean
import com.base.appzxhy.business.ads.AdsMgr
import com.base.appzxhy.databinding.ActivityMainBinding
import com.base.appzxhy.ui.dialog.AppExitDialog
import com.base.appzxhy.ui.dialog.NotificationTurnOnDialog
import com.base.appzxhy.ui.dialog.RateDialog
import com.base.appzxhy.ui.dialog.StoragePermissionDialog
import com.base.appzxhy.utils.PermissionUtils.requestNotificationPermission
import com.base.appzxhy.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.showDialog()
    }

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

        viewModel.checkDialog(this)
    }


}