package com.zxhy.fastfilemanagerpro

import android.os.Bundle
import android.view.MenuItem
import android.view.View
import android.widget.AdapterView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.navigation.findNavController
import androidx.navigation.fragment.NavHostFragment
import com.google.android.material.navigation.NavigationBarView
import com.zxhy.fastfilemanagerpro.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {
    private lateinit var binding: ActivityMainBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
        //关键去除itemIconTint影响
        binding.bnv.itemIconTintList = null
        binding.bnv.setItemIconSizeRes(R.dimen.dp_24)

        val navHostFragment =
            supportFragmentManager.findFragmentById(R.id.container) as NavHostFragment

        navHostFragment.navController.addOnDestinationChangedListener { controller, destination, arguments ->
            when (destination.id) {
                R.id.koManagerkoFragment -> {
                    binding.bnv.selectedItemId = R.id.nav_manager
                }

                R.id.koHomekoFragment -> {
                    binding.bnv.selectedItemId = R.id.nav_home
                }
            }
        }

        binding.bnv.setOnItemSelectedListener(object : AdapterView.OnItemSelectedListener,
            NavigationBarView.OnItemSelectedListener {
            override fun onItemSelected(
                parent: AdapterView<*>?,
                view: View?,
                position: Int,
                id: Long
            ) {

            }

            override fun onNothingSelected(parent: AdapterView<*>?) {
            }

            override fun onNavigationItemSelected(item: MenuItem): Boolean {

                when (item.itemId) {
                    R.id.nav_manager -> {
                        runCatching {
                            findNavController(R.id.container).navigate(R.id.action_koHomekoFragment_to_koManagerkoFragment)
                        }
                    }

                    R.id.nav_home -> {
                        runCatching {
                            findNavController(R.id.container).navigate(R.id.action_koManagerkoFragment_to_koHomekoFragment)
                        }
                    }
                }
                return true
            }
        })
    }
}