package xyz.cottongin.radio247

import android.Manifest
import android.content.pm.PackageManager
import android.os.Build
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.result.contract.ActivityResultContracts
import androidx.activity.enableEdgeToEdge
import androidx.core.content.ContextCompat
import androidx.activity.compose.BackHandler
import androidx.activity.compose.setContent
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.platform.LocalContext
import xyz.cottongin.radio247.service.PlaybackState
import xyz.cottongin.radio247.ui.navigation.Screen
import xyz.cottongin.radio247.ui.screens.nowplaying.NowPlayingScreen
import xyz.cottongin.radio247.ui.screens.settings.SettingsScreen
import xyz.cottongin.radio247.ui.screens.stationlist.StationListScreen
import xyz.cottongin.radio247.ui.theme.Radio247Theme

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        enableEdgeToEdge()
        super.onCreate(savedInstanceState)
        setContent {
            val requestNotificationPermission = rememberLauncherForActivityResult(
                contract = ActivityResultContracts.RequestPermission()
            ) { _ -> }
            val context = LocalContext.current
            LaunchedEffect(Unit) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU &&
                    ContextCompat.checkSelfPermission(context, Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED
                ) {
                    requestNotificationPermission.launch(Manifest.permission.POST_NOTIFICATIONS)
                }
            }
            Radio247Theme {
                val app = application as RadioApplication
                val playbackState by app.controller.state.collectAsState()
                var currentScreen by remember { mutableStateOf<Screen>(Screen.StationList) }
                var wasActive by remember { mutableStateOf(false) }

                LaunchedEffect(playbackState) {
                    val isActive = playbackState !is PlaybackState.Idle
                    if (isActive && !wasActive) {
                        currentScreen = Screen.NowPlaying
                    } else if (!isActive && wasActive && currentScreen == Screen.NowPlaying) {
                        currentScreen = Screen.StationList
                    }
                    wasActive = isActive
                }

                BackHandler(enabled = currentScreen != Screen.StationList) {
                    currentScreen = Screen.StationList
                }

                when (currentScreen) {
                    Screen.StationList -> StationListScreen(
                        onNavigateToNowPlaying = { currentScreen = Screen.NowPlaying },
                        onNavigateToSettings = { currentScreen = Screen.Settings }
                    )
                    Screen.NowPlaying -> NowPlayingScreen(
                        onBack = { currentScreen = Screen.StationList }
                    )
                    Screen.Settings -> SettingsScreen(
                        onBack = { currentScreen = Screen.StationList }
                    )
                }
            }
        }
    }
}
