package de.jadehs.strassenschadenpro2

import android.app.Application
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.AddCircle
import androidx.compose.material.icons.filled.List
import androidx.compose.material.icons.filled.Place
import androidx.compose.material.icons.filled.Settings
import androidx.compose.material3.Icon
import androidx.compose.material3.NavigationBar
import androidx.compose.material3.NavigationBarItem
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import de.jadehs.strassenschadenpro2.pages.CreatePage
import de.jadehs.strassenschadenpro2.pages.ListPage
import de.jadehs.strassenschadenpro2.pages.MapPage
import de.jadehs.strassenschadenpro2.pages.SettingsPage

@Composable
fun MainScreen(modifier: Modifier = Modifier, application: Application) {

    val navItemList = listOf(
        NavItem("Karte",Icons.Default.Place),
        NavItem("Erstellen",Icons.Default.AddCircle),
        NavItem("Liste",Icons.Default.List),
        NavItem("Einstellungen",Icons.Default.Settings)
    )

    var selectedIndex by remember { mutableStateOf(0) }

    val mapViewModel = remember { MapViewModel(application) }
    val listViewModel = remember { ListViewModel(application) }

    var selectedObjectId by remember { mutableStateOf<Int?>(null) }

    Scaffold(modifier = Modifier.fillMaxSize(),
        bottomBar = {
            NavigationBar {
                navItemList.forEachIndexed { index, item ->
                    NavigationBarItem(
                        selected = selectedIndex == index,
                        onClick = {
                            selectedIndex = index
                        },
                        icon = {Icon(item.icon, "icon")},
                        label = {Text(text=item.label)}
                    )
                }
            }
        }) {
        innerPadding ->
        ContentScreen(modifier = Modifier.padding(innerPadding),
            selectedIndex,
            mapViewModel,
            listViewModel,
            selectedObjectId,
            onFeatureSelected = { objectId ->
                selectedObjectId = objectId
                selectedIndex = 0
            },
            onObjectIdUsed = {
                selectedObjectId = null
            })
    }
}

@Composable
fun ContentScreen(
    modifier: Modifier = Modifier,
    selectedIndex: Int,
    mapViewModel: MapViewModel,
    listViewModel: ListViewModel,
    selectedObjectId: Int?,
    onFeatureSelected: (Int) -> Unit,
    onObjectIdUsed: () -> Unit
) {

    when(selectedIndex) {
        0 -> MapPage(modifier = modifier, mapViewModel = mapViewModel,selectedObjectId=selectedObjectId, onObjectIdUsed = onObjectIdUsed)
        1 -> CreatePage(modifier = modifier, mapViewModel = mapViewModel)
        2 -> ListPage(modifier = modifier, listViewModel = listViewModel, onFeatureClick = onFeatureSelected)
        3 -> SettingsPage(modifier = modifier)
    }
}