package com.test.basd.onecleanmaster.activity

import android.graphics.Color
import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.blankj.utilcode.util.BarUtils
import com.test.basd.onecleanmaster.databinding.ActivityMainBinding
import com.test.basd.onecleanmaster.fragment.HomeFragment
import com.test.basd.onecleanmaster.fragment.ToolsFragment
import com.test.basd.onecleanmaster.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 fragments by lazy {
        mutableListOf(homeFragment,toolsFragment)
    }

    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
        }
    }

    private fun setPage(p: Int) {
        binding.idBottom1.isSelected = false
        binding.idBottom2.isSelected = false
        when (p) {
            0 -> binding.idBottom1.isSelected = true
          1 -> binding.idBottom2.isSelected = true
        }
    }

}