package com.test.basd.smartjunkcleaner.activity

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.graphics.Color
import android.graphics.Typeface
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.blankj.utilcode.util.AppUtils
import com.blankj.utilcode.util.BarUtils
import com.test.basd.smartjunkcleaner.databinding.ActivityMainBinding
import com.test.basd.smartjunkcleaner.fragment.HomeFragment
import com.test.basd.smartjunkcleaner.fragment.ToolsFragment
import com.test.basd.smartjunkcleaner.helps.BaseActivity
import com.test.basd.smartjunkcleaner.helps.ads.AdmobUtils
import com.test.basd.smartjunkcleaner.view.RateStarPop
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

class MainActivity : BaseActivity<ActivityMainBinding>() {
    override val isLightMode = true
    override val binding: ActivityMainBinding by lazy {
        ActivityMainBinding.inflate(layoutInflater)
    }
    private val homeFragment by lazy {
        HomeFragment()
    }


    private val toolsFragment by lazy {
        ToolsFragment()
    }

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

    private lateinit var batteryReceiver: BatteryReceiver

    override fun initView() {
        BarUtils.setStatusBarColor(this, Color.TRANSPARENT)
        AdmobUtils.loadNativeAd()
        // binding.root.updatePadding(top = BarUtils.getStatusBarHeight())
        binding.idVp.run {
            adapter = object : FragmentStateAdapter(this@MainActivity) {
                override fun getItemCount(): Int {
                    return fragments.size
                }

                override fun createFragment(position: Int): Fragment {
                    return fragments[position]
                }
            }
        }
        binding.idVp.registerOnPageChangeCallback(object :
            ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                setPage(position)
            }
        })
        binding.idBottom1.setOnClickListener {

            binding.idVp.currentItem = 0
        }
        binding.idBottom2.setOnClickListener {
            binding.idVp.currentItem = 1
        }


        batteryReceiver = BatteryReceiver()
        val filter = IntentFilter(Intent.ACTION_BATTERY_CHANGED)
        registerReceiver(batteryReceiver, filter)
    }

    private fun setPage(p: Int) {
        binding.idBottom1.isSelected = false
        binding.idBottom2.isSelected = false
        binding.idTv1.typeface = Typeface.DEFAULT
        binding.idTv2.typeface = Typeface.DEFAULT
        when (p) {
            0 -> {
                binding.idBottom1.isSelected = true
                binding.idTv1.typeface = Typeface.DEFAULT_BOLD
            }

            1 -> {
                binding.idBottom2.isSelected = true
                binding.idTv2.typeface = Typeface.DEFAULT_BOLD
            }
        }
    }

    var level = 0

    private inner class BatteryReceiver : BroadcastReceiver() {

        override fun onReceive(context: Context?, intent: Intent?) {
            val current = intent?.extras!!.getInt("level")
            val total = intent.extras!!.getInt("scale")
            level = current * 100 / total

        }
    }

    override fun onDestroy() {
        super.onDestroy()
        unregisterReceiver(batteryReceiver)
    }

    override fun onResume() {
        super.onResume()
        if (AppUtils.isAppForeground()) {
            lifecycleScope.launch(Dispatchers.Main) {
                delay(1000)
                RateStarPop.show(this@MainActivity)
            }
        }
    }
}