package com.zxhyis.tools.ui.home


import androidx.activity.OnBackPressedCallback
import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.zxhyis.tools.R
import com.zxhyis.tools.base.TopActivity
import com.zxhyis.tools.databinding.ActivityMainBinding
import com.zxhyis.tools.logic.ActivityCollector
import com.zxhyis.tools.logic.testLog
import com.zxhyis.tools.ui.dialog.ExitDialog

class MainActivity : TopActivity<ActivityMainBinding>() {

    private lateinit var f1: HomeFragment
    private lateinit var f2: ViewFragment
    private lateinit var f3: ToolFragment
    protected var dialog: ExitDialog? = null


    override fun getViewBinding1(): ActivityMainBinding {
        return ActivityMainBinding.inflate(layoutInflater)
    }

    override fun init() {
        initBavAndVp2()

        onBackPressedDispatcher.addCallback(object : OnBackPressedCallback(true) {
            override fun handleOnBackPressed() {
                testLog("back")
                dialog= ExitDialog()
                dialog?.showNow(supportFragmentManager,"exit_app")
                dialog?.isCancelable=false
            }
        })



    }

    private fun initBavAndVp2() {
        f1 = HomeFragment()
        f2 = ViewFragment()
        f3 = ToolFragment()
        val fList = listOf(f1, f2, f3)

        //配置vp2
        vb.vp2.adapter = object : FragmentStateAdapter(this) {
            override fun getItemCount(): Int = fList.size

            override fun createFragment(position: Int): Fragment = fList[position]

        }

        vb.vp2.apply {
            isUserInputEnabled = false
            offscreenPageLimit = fList.size
            currentItem = 0
            registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
                override fun onPageSelected(position: Int) {
                    super.onPageSelected(position)
                    when (position) {
                        0 -> {
                            vb.bav.selectedItemId = R.id.bav1
                        }

                        1 -> {
                            vb.bav.selectedItemId = R.id.bav2
                        }

                        2 -> {
                            vb.bav.selectedItemId = R.id.bav3
                        }
                    }
                }
            })
        }

        vb.bav.setOnItemSelectedListener { item ->
            when (item.itemId) {
                R.id.bav1 -> {
                    vb.vp2.setCurrentItem(0, false)
                    true
                }

                R.id.bav2 -> {
                    vb.vp2.setCurrentItem(1, false)
                    true
                }

                R.id.bav3 -> {
                    vb.vp2.setCurrentItem(2, false)
                    true
                }

                else -> false
            }
        }

    }

    override fun onDestroy() {
        super.onDestroy()
        if (dialog!=null){
            dialog?.dismiss()
        }
    }

}