package com.zxhy.solarmasterace

import android.os.Bundle
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.findNavController
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)

        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 = {})

    }
}