# MainActivity.kt

    /*
     * The MIT License
     *
     * Original work sponsored and donated by The Danish Health Data Authority (http://www.sundhedsdatastyrelsen.dk)
     *
     * Copyright (C) 2021 The Danish Health Data Authority (http://www.sundhedsdatastyrelsen.dk)
     *
     *
     * Permission is hereby granted, free of charge, to any person obtaining a copy of
     * this software and associated documentation files (the "Software"), to deal in
     * the Software without restriction, including without limitation the rights to
     * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
     * of the Software, and to permit persons to whom the Software is furnished to do
     * so, subject to the following conditions:
     *
     *
     * The above copyright notice and this permission notice shall be included in all
     * copies or substantial portions of the Software.
     *
     *
     * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
     * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
     * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
     * SOFTWARE.
     */

    package dk.nsp.mingraviditet.ui.main

    import android.os.Bundle
    import android.view.LayoutInflater
    import androidx.lifecycle.LiveData
    import androidx.navigation.NavController
    import com.google.android.material.bottomnavigation.BottomNavigationView
    import dagger.hilt.android.AndroidEntryPoint
    import dk.nsp.mingraviditet.R
    import dk.nsp.mingraviditet.databinding.ActivityMainBinding
    import dk.nsp.mingraviditet.ui.BaseActivity
    import dk.nsp.mingraviditet.util.extensions.setupWithNavController

    @AndroidEntryPoint
    class MainActivity : BaseActivity() {
        private var currentNavController: LiveData<NavController>? = null

        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            val binding = ActivityMainBinding.inflate(LayoutInflater.from(this))
            setContentView(binding.root)

            if (savedInstanceState == null) {
                setupBottomNavigationBar()
            } // else, need to wait for onRestoreInstanceState
        }

        override fun onRestoreInstanceState(savedInstanceState: Bundle) {
            super.onRestoreInstanceState(savedInstanceState)
            // Now that BottomNavigationBar has restored its instance state
            // and its selectedItemId, we can proceed with setting up the
            // BottomNavigationBar with Navigation
            setupBottomNavigationBar()
        }

        fun navigateToKnowledge() {
            val bottomNavigationView = findViewById<BottomNavigationView>(R.id.bottom_bar)
            bottomNavigationView.selectedItemId =
                bottomNavigationView.menu.findItem(R.id.knowledge).itemId
        }

        private fun setupBottomNavigationBar() {
            val bottomNavigationView = findViewById<BottomNavigationView>(R.id.bottom_bar)

            val navGraphIds = listOf(
                R.navigation.nav_overview,
                R.navigation.nav_knowledge,
                R.navigation.nav_appointments,
                R.navigation.nav_journal,
                R.navigation.nav_more
            )

            val controller = bottomNavigationView.setupWithNavController(
                navGraphIds = navGraphIds,
                fragmentManager = supportFragmentManager,
                containerId = R.id.nav_host_fragment,
                intent = intent
            )

            currentNavController = controller
        }

        override fun onSupportNavigateUp(): Boolean {
            return currentNavController?.value?.navigateUp() ?: false
        }
    }