package com.zxhy.solarmasterace

import android.os.Bundle
import android.util.Log
import android.view.MenuItem
import android.view.View
import android.widget.AdapterView
import android.widget.AdapterView.OnItemSelectedListener
import androidx.activity.enableEdgeToEdge
import androidx.activity.result.ActivityResultLauncher
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.navigation.NavController
import androidx.navigation.NavDestination
import androidx.navigation.findNavController
import androidx.navigation.fragment.NavHostFragment
import com.google.android.material.navigation.NavigationBarView
import com.zxhy.solarmasterace.databinding.ActivityMainBinding
import com.zxhy.solarmasterace.permission.requestStoreFollow
import com.zxhy.solarmasterace.quicktools.ActivityLauncher

class MainActivity : AppCompatActivity() {

    private lateinit var binding: ActivityMainBinding
    private lateinit var launcher: ActivityLauncher

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        launcher = ActivityLauncher(this)
        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
        }
        window.statusBarColor = ContextCompat.getColor(this, R.color.color_FF5C67E5)

        //关键去除itemIconTint影响
        binding.bNav.itemIconTintList = null
        binding.bNav.setItemIconSizeRes(R.dimen.dp_24)

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

        navHostFragment.navController.addOnDestinationChangedListener { controller, destination, arguments ->
            when (destination.id) {
                R.id.managerFragment -> {
                    window.statusBarColor =
                        ContextCompat.getColor(this@MainActivity, R.color.color_FF5C67E5)
                    binding.bNav.selectedItemId = R.id.nav_manager
                }

                R.id.toolsFragment -> {
                    window.statusBarColor =
                        ContextCompat.getColor(this@MainActivity, R.color.white)
                    binding.bNav.selectedItemId = R.id.nav_tools
                }
            }
        }
        binding.bNav.setOnItemSelectedListener(object : 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 -> {
                        window.statusBarColor =
                            ContextCompat.getColor(this@MainActivity, R.color.color_FF5C67E5)
                        runCatching {
                            findNavController(R.id.fragment_container_view).navigate(R.id.action_toolsFragment_to_managerFragment)
                        }
                    }

                    R.id.nav_tools -> {
                        window.statusBarColor =
                            ContextCompat.getColor(this@MainActivity, R.color.white)
                        runCatching {
                            findNavController(R.id.fragment_container_view).navigate(R.id.action_managerFragment_to_toolsFragment)
                        }
                    }
                }
                return true
            }
        })

        requestStoreFollow(launcher, disAgreeAction = {}, agreeAction = {})
    }
}