package org.thoughtcrime.securesms.components.settings

import android.content.res.Configuration
import android.os.Build
import android.os.Bundle
import androidx.activity.OnBackPressedCallback
import androidx.activity.SystemBarStyle
import androidx.activity.enableEdgeToEdge
import androidx.core.content.ContextCompat
import androidx.navigation.NavController
import androidx.navigation.Navigation
import androidx.navigation.fragment.NavHostFragment
import org.thoughtcrime.securesms.PassphraseRequiredActivity
import org.thoughtcrime.securesms.R
import org.thoughtcrime.securesms.util.DynamicNoActionBarTheme
import org.thoughtcrime.securesms.util.DynamicTheme
import org.signal.core.ui.R as CoreUiR

/**
 * The DSL API can be completely replaced by compose.
 * See ComposeFragment or ComposeBottomSheetFragment for an alternative to this API"
 */
open class DSLSettingsActivity : PassphraseRequiredActivity() {

  protected open val dynamicTheme: DynamicTheme = DynamicNoActionBarTheme()

  protected lateinit var navController: NavController
    private set

  override fun onCreate(savedInstanceState: Bundle?, ready: Boolean) {
    enableSettingsEdgeToEdge()

    setContentView(R.layout.dsl_settings_activity)

    if (savedInstanceState == null) {
      val navGraphId = resolveNavGraphId()
      if (navGraphId == -1) {
        throw IllegalStateException("No navgraph id was passed to activity")
      }

      val fragment: NavHostFragment = NavHostFragment.create(navGraphId, resolveStartBundle())

      supportFragmentManager.beginTransaction()
        .replace(R.id.nav_host_fragment, fragment)
        .commitNow()

      navController = fragment.navController
    } else {
      val fragment = supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
      navController = fragment.navController
    }

    dynamicTheme.onCreate(this)

    onBackPressedDispatcher.addCallback(this, OnBackPressed())
  }

  override fun onResume() {
    super.onResume()
    dynamicTheme.onResume(this)
  }

  override fun onNavigateUp(): Boolean {
    return if (!Navigation.findNavController(this, R.id.nav_host_fragment).popBackStack()) {
      onWillFinish()
      finish()
      true
    } else {
      false
    }
  }

  private fun enableSettingsEdgeToEdge() {
    val navBarColor = ContextCompat.getColor(this, CoreUiR.color.signal_colorSurface2)
    val isDark = resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK == Configuration.UI_MODE_NIGHT_YES

    if (Build.VERSION.SDK_INT >= 26) {
      enableEdgeToEdge(
        navigationBarStyle = if (isDark) {
          SystemBarStyle.dark(navBarColor)
        } else {
          SystemBarStyle.light(navBarColor, navBarColor)
        }
      )
    } else {
      enableEdgeToEdge()
    }
  }

  protected open fun onWillFinish() {}

  protected open fun resolveNavGraphId(): Int = intent.getIntExtra(ARG_NAV_GRAPH, -1)

  protected open fun resolveStartBundle(): Bundle? = intent.getBundleExtra(ARG_START_BUNDLE)

  companion object {
    const val ARG_NAV_GRAPH = "nav_graph"
    const val ARG_START_BUNDLE = "start_bundle"
  }

  private inner class OnBackPressed : OnBackPressedCallback(true) {
    override fun handleOnBackPressed() {
      onNavigateUp()
    }
  }
}
