package de.trainingstagebuch.app.ui

import androidx.compose.foundation.layout.Box
import androidx.compose.material3.DropdownMenu
import androidx.compose.material3.DropdownMenuItem
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.compositionLocalOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import de.trainingstagebuch.app.R

data class AppMenuActions(
    val currentClubId: String?,
    val onNavigateToCreateClub: () -> Unit,
    val onNavigateToShowClub: (String) -> Unit,
    val onNavigateToTrainingStats: () -> Unit,
    val onNavigateToMembers: () -> Unit,
    val onNavigateToDiary: () -> Unit,
    val onNavigateToSchedule: () -> Unit,
    val onNavigateToTournaments: () -> Unit,
    val onNavigateToClubSettings: () -> Unit,
    val onNavigateToPendingApprovals: () -> Unit,
    val onNavigateToPermissions: () -> Unit,
    val onNavigateToTeamManagement: () -> Unit,
    val onNavigateToMemberTransferSettings: () -> Unit,
    val onNavigateToPredefinedActivities: () -> Unit,
    val onNavigateToMyTischtennisAccount: () -> Unit,
    val onNavigateToLogs: () -> Unit,
    val onNavigateToPersonalSettings: () -> Unit,
    val onNavigateToImpressum: () -> Unit,
    val onNavigateToDatenschutz: () -> Unit,
    val onLogout: () -> Unit
)

val LocalAppMenuActions = compositionLocalOf<AppMenuActions?> { null }

@Composable
fun ProvideAppMenuActions(
    actions: AppMenuActions,
    content: @Composable () -> Unit
) {
    CompositionLocalProvider(LocalAppMenuActions provides actions, content = content)
}

@Composable
fun TrainingstagebuchTopBar(
    title: String
) {
    val menuActions = LocalAppMenuActions.current
    var menuExpanded by remember { mutableStateOf(false) }

    fun navigateFromMenu(action: () -> Unit) {
        menuExpanded = false
        action()
    }

    TopAppBar(
        title = { Text(title) },
        colors = TopAppBarDefaults.topAppBarColors(
            containerColor = MaterialTheme.colorScheme.primary,
            titleContentColor = MaterialTheme.colorScheme.onPrimary,
            actionIconContentColor = MaterialTheme.colorScheme.onPrimary
        ),
        actions = {
            if (menuActions != null) {
                Box {
                    IconButton(onClick = { menuExpanded = !menuExpanded }) {
                        Icon(
                            painter = painterResource(id = android.R.drawable.ic_menu_sort_by_size),
                            contentDescription = stringResource(R.string.home_menu_toggle)
                        )
                    }
                    DropdownMenu(
                        expanded = menuExpanded,
                        onDismissRequest = { menuExpanded = false },
                        containerColor = MaterialTheme.colorScheme.surface.copy(alpha = 0.88f)
                    ) {
                        DropdownMenuItem(
                            text = { Text(stringResource(R.string.create_club_create_button)) },
                            onClick = { navigateFromMenu(menuActions.onNavigateToCreateClub) }
                        )
                        if (menuActions.currentClubId?.isNotBlank() == true) {
                            DropdownMenuItem(
                                text = { Text(stringResource(R.string.home_open_current_club)) },
                                onClick = {
                                    navigateFromMenu {
                                        menuActions.onNavigateToShowClub(menuActions.currentClubId)
                                    }
                                }
                            )
                            DropdownMenuItem(
                                text = { Text(stringResource(R.string.home_open_training_stats)) },
                                onClick = { navigateFromMenu(menuActions.onNavigateToTrainingStats) }
                            )
                            DropdownMenuItem(
                                text = { Text(stringResource(R.string.home_open_members)) },
                                onClick = { navigateFromMenu(menuActions.onNavigateToMembers) }
                            )
                            DropdownMenuItem(
                                text = { Text(stringResource(R.string.home_open_diary)) },
                                onClick = { navigateFromMenu(menuActions.onNavigateToDiary) }
                            )
                            DropdownMenuItem(
                                text = { Text(stringResource(R.string.home_open_schedule)) },
                                onClick = { navigateFromMenu(menuActions.onNavigateToSchedule) }
                            )
                            DropdownMenuItem(
                                text = { Text(stringResource(R.string.home_open_tournaments)) },
                                onClick = { navigateFromMenu(menuActions.onNavigateToTournaments) }
                            )
                            DropdownMenuItem(
                                text = { Text(stringResource(R.string.home_open_club_settings)) },
                                onClick = { navigateFromMenu(menuActions.onNavigateToClubSettings) }
                            )
                            DropdownMenuItem(
                                text = { Text(stringResource(R.string.home_open_pending_approvals)) },
                                onClick = { navigateFromMenu(menuActions.onNavigateToPendingApprovals) }
                            )
                            DropdownMenuItem(
                                text = { Text(stringResource(R.string.home_open_permissions)) },
                                onClick = { navigateFromMenu(menuActions.onNavigateToPermissions) }
                            )
                            DropdownMenuItem(
                                text = { Text(stringResource(R.string.home_open_team_management)) },
                                onClick = { navigateFromMenu(menuActions.onNavigateToTeamManagement) }
                            )
                            DropdownMenuItem(
                                text = { Text(stringResource(R.string.home_open_member_transfer)) },
                                onClick = { navigateFromMenu(menuActions.onNavigateToMemberTransferSettings) }
                            )
                            DropdownMenuItem(
                                text = { Text(stringResource(R.string.home_open_predefined_activities)) },
                                onClick = { navigateFromMenu(menuActions.onNavigateToPredefinedActivities) }
                            )
                            DropdownMenuItem(
                                text = { Text(stringResource(R.string.home_open_mytischtennis_account)) },
                                onClick = { navigateFromMenu(menuActions.onNavigateToMyTischtennisAccount) }
                            )
                            DropdownMenuItem(
                                text = { Text(stringResource(R.string.home_open_logs)) },
                                onClick = { navigateFromMenu(menuActions.onNavigateToLogs) }
                            )
                        }
                        DropdownMenuItem(
                            text = { Text(stringResource(R.string.home_open_personal_settings)) },
                            onClick = { navigateFromMenu(menuActions.onNavigateToPersonalSettings) }
                        )
                        DropdownMenuItem(
                            text = { Text(stringResource(R.string.home_open_impressum)) },
                            onClick = { navigateFromMenu(menuActions.onNavigateToImpressum) }
                        )
                        DropdownMenuItem(
                            text = { Text(stringResource(R.string.home_open_datenschutz)) },
                            onClick = { navigateFromMenu(menuActions.onNavigateToDatenschutz) }
                        )
                        DropdownMenuItem(
                            text = { Text(stringResource(R.string.common_logout)) },
                            onClick = { navigateFromMenu(menuActions.onLogout) }
                        )
                    }
                }
            }
        }
    )
}
