package com.base.superfilemanager.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.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.blankj.utilcode.util.BarUtils
import com.base.superfilemanager.databinding.ActivityMainBinding
import com.base.superfilemanager.fragment.FileCategoryFragment
import com.base.superfilemanager.fragment.HomeFragment
import com.base.superfilemanager.fragment.ToolsFragment
import com.base.superfilemanager.helps.BaseActivity

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 fileCategoryFragment by lazy {
        FileCategoryFragment()
    }
    private val fragments by lazy {
        mutableListOf(homeFragment, toolsFragment, fileCategoryFragment)
    }
    private lateinit var batteryReceiver: BatteryReceiver

    override fun initView() {
        BarUtils.setStatusBarColor(this, Color.TRANSPARENT)
        // 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
        }
        binding.idBottom3.setOnClickListener {
            binding.idVp.currentItem = 2
        }
        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.idBottom3.isSelected = false
        binding.idTvHome.typeface = Typeface.DEFAULT
        binding.idTvTools.typeface = Typeface.DEFAULT
        binding.idTvManager.typeface = Typeface.DEFAULT
        when (p) {
            0 -> {
                binding.idBottom1.isSelected = true
                binding.idTvHome.typeface = Typeface.DEFAULT_BOLD
            }

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

            2 -> {
                binding.idBottom3.isSelected = true
                binding.idTvManager.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)
    }
}