package com.base.filerecoveryrecyclebin.activity

import android.content.Intent
import android.graphics.Color
import android.graphics.Typeface
import androidx.activity.addCallback
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.filerecoveryrecyclebin.ads.admob.AdmobBannerUtils
import com.base.filerecoveryrecyclebin.databinding.ActivityMainBinding
import com.base.filerecoveryrecyclebin.fragment.HomeFragment
import com.base.filerecoveryrecyclebin.fragment.RecycleBinFragment
import com.base.filerecoveryrecyclebin.help.BaseActivity
import com.base.filerecoveryrecyclebin.help.PermissionHelp.checkStorePermission
import com.base.filerecoveryrecyclebin.service.StayNotificationService
import com.base.filerecoveryrecyclebin.utils.BarUtils
import com.base.filerecoveryrecyclebin.view.DialogViews.showExitDialog
import com.base.filerecoveryrecyclebin.view.PermissionDialog.showPermissionBottomSheet
import com.base.filerecoveryrecyclebin.view.RateStarPop.showRateStarPopDialog
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

class MainActivity : BaseActivity<ActivityMainBinding>() {
    private val TAG = "MainActivity"
    override val binding: ActivityMainBinding by lazy {
        ActivityMainBinding.inflate(layoutInflater)
    }

    private val homeFragment by lazy {
        HomeFragment()
    }
    private val recycleBinFragment by lazy {
        RecycleBinFragment()
    }
    private val fragments by lazy {
        mutableListOf(homeFragment, recycleBinFragment)
    }


    override fun initView() {
        BarUtils.setStatusBarLightMode(this, true)
        BarUtils.setStatusBarColor(this, Color.parseColor("#F2F3F7"))
        binding.root.updatePadding(top = BarUtils.getStatusBarHeight())

        if (!checkStorePermission()) {
            if (dialog == null) {
                dialog = showPermissionBottomSheet(launcher) {
                    AdmobBannerUtils.showCollapsibleBannerAd(this, binding.flBanner)
                }
            }
        } else {
            AdmobBannerUtils.showCollapsibleBannerAd(this, binding.flBanner)
        }

        binding.viewpager2.run {
            adapter = object : FragmentStateAdapter(this@MainActivity) {
                override fun getItemCount(): Int {
                    return fragments.size
                }

                override fun createFragment(position: Int): Fragment {
                    return fragments[position]
                }
            }
        }

        binding.viewpager2.registerOnPageChangeCallback(object :
            ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                setPageIndicate(position)
            }
        })

        binding.ll1.setOnClickListener {
            binding.viewpager2.currentItem = 0
            BarUtils.setStatusBarColor(this, Color.parseColor("#F2F3F7"))
        }

        binding.ll2.setOnClickListener {
            binding.viewpager2.currentItem = 1
            BarUtils.setStatusBarColor(this, Color.TRANSPARENT)
        }


    }

    private fun initStayServiceObserver() {
        startService(Intent(this, StayNotificationService::class.java).apply {
            this.action = StayNotificationService.MEDIA_OBSERVER_ACTION
        })
        startService(Intent(this, StayNotificationService::class.java).apply {
            this.action = StayNotificationService.FILE_OBSERVER_ACTION
            putExtra("isStart", true)
        })
    }

    private fun setPageIndicate(p: Int) {
        binding.ll1.isSelected = false
        binding.ll2.isSelected = false
        binding.tv1.typeface = Typeface.DEFAULT
        binding.tv2.typeface = Typeface.DEFAULT
        when (p) {
            0 -> {
                binding.ll1.isSelected = true
                binding.tv1.typeface = Typeface.DEFAULT_BOLD
            }

            1 -> {
                binding.ll2.isSelected = true
                binding.tv2.typeface = Typeface.DEFAULT_BOLD
            }
        }
    }
     fun setPage(p:Int){
         binding.viewpager2.currentItem = p
    }

    override fun initListener() {
        super.initListener()
        onBackPressedDispatcher.addCallback {
            showExitDialog()
        }
    }

    override fun onResume() {
        super.onResume()

        if (checkStorePermission()) {
            initStayServiceObserver()
        }

        lifecycleScope.launch(Dispatchers.Main) {
            delay(1000)
            showRateStarPopDialog()
        }
    }
}