/**
 * MainActivity.kt
 * SkyLink Android 端 - 主界面容器
 *
 * 使用 Navigation 管理多个 Fragment，包括首页和设置
 *
 * @author flowzl
 * @date 2026-01-19
 */

package com.skylink.app

import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import androidx.appcompat.app.AppCompatActivity
import androidx.navigation.NavController
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.setupActionBarWithNavController
import androidx.navigation.ui.setupWithNavController
import com.google.android.material.bottomnavigation.BottomNavigationView
import com.skylink.app.navigation.NavigationHelper
import com.skylink.app.navigation.Routes
import com.skylink.app.navigation.navigateToSettings

class MainActivity : AppCompatActivity() {

    companion object {
        private const val TAG = "MainActivity"
    }

    // ========================================================================
    // Navigation 组件
    // ========================================================================

    private lateinit var navController: NavController
    private lateinit var appBarConfiguration: AppBarConfiguration
    private lateinit var navigationHelper: NavigationHelper

    // ========================================================================
    // 生命周期方法
    // ========================================================================

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        setupNavigation()
    }

    override fun onSupportNavigateUp(): Boolean {
        return navController.navigateUp() || super.onSupportNavigateUp()
    }

    override fun onBackPressed() {
        if (!navigationHelper.handleBackPressed()) {
            @Suppress("DEPRECATION")
            super.onBackPressed()
        }
    }

    // ========================================================================
    // Navigation 初始化
    // ========================================================================

    private fun setupNavigation() {
        // 获取 NavHostFragment
        val navHostFragment = supportFragmentManager
            .findFragmentById(R.id.nav_host_fragment) as? NavHostFragment
            ?: return

        navController = navHostFragment.navController
        navigationHelper = NavigationHelper(navController)

        // 设置 AppBar 配置，指定顶级目的地
        appBarConfiguration = AppBarConfiguration(
            setOf(
                R.id.homeFragment,
                R.id.settingsFragment
            )
        )

        // 设置 ActionBar 与 Navigation 联动
        setupActionBarWithNavController(navController, appBarConfiguration)

        // 设置 BottomNavigationView
        val bottomNav: BottomNavigationView = findViewById(R.id.bottom_navigation)
        bottomNav.setupWithNavController(navController)
    }

    // ========================================================================
    // Menu 处理
    // ========================================================================

    override fun onCreateOptionsMenu(outMenu: Menu?): Boolean {
        if (outMenu != null) {
            menuInflater.inflate(R.menu.activity_main_menu, outMenu)
        }
        return super.onCreateOptionsMenu(outMenu)
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        return when (item.itemId) {
            R.id.action_settings -> {
                navController.navigateToSettings()
                true
            }
            else -> super.onOptionsItemSelected(item)
        }
    }
}
