package com.pixelized.desktop.lwa.ui.screen.campaign.toolbar

import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.material.DropdownMenu
import androidx.compose.material.Icon
import androidx.compose.material.IconButton
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.material.TextButton
import androidx.compose.material.TopAppBar
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.Stable
import androidx.compose.runtime.State
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import com.pixelized.desktop.lwa.LocalWindowController
import com.pixelized.desktop.lwa.repository.network.NetworkRepository
import com.pixelized.desktop.lwa.ui.navigation.screen.LocalScreenController
import com.pixelized.desktop.lwa.ui.navigation.screen.destination.navigateToSettings
import com.pixelized.desktop.lwa.ui.navigation.window.destination.navigateToGameMasterWindow
import com.pixelized.desktop.lwa.ui.screen.campaign.toolbar.links.ResourcesMenu
import com.pixelized.desktop.lwa.ui.screen.campaign.toolbar.network.NetworkMenu
import com.pixelized.desktop.lwa.ui.theme.lwa
import lwacharactersheet.composeapp.generated.resources.Res
import lwacharactersheet.composeapp.generated.resources.ic_link_24dp
import lwacharactersheet.composeapp.generated.resources.ic_settings_24dp
import lwacharactersheet.composeapp.generated.resources.ic_wifi_24dp
import lwacharactersheet.composeapp.generated.resources.ic_wifi_off_24dp
import lwacharactersheet.composeapp.generated.resources.settings__title
import org.jetbrains.compose.resources.painterResource
import org.jetbrains.compose.resources.stringResource
import org.koin.compose.viewmodel.koinViewModel

@Stable
data class MenuState(
    val isResourcesMenuOpen: MutableState<Boolean> = mutableStateOf(false),
    val isNetworkMenuOpen: MutableState<Boolean> = mutableStateOf(false),
)

@Composable
fun CampaignToolbar(
    viewModel: CampaignToolbarViewModel = koinViewModel(),
) {
    val windows = LocalWindowController.current
    val screen = LocalScreenController.current

    val menuState = remember { MenuState() }

    val title = viewModel.title.collectAsState()
    val status = viewModel.status.collectAsState()
    val isAdmin = viewModel.isAdmin.collectAsState()

    CampaignToolbarContent(
        title = title,
        status = status,
        isAdmin = isAdmin,
        state = menuState,
        onAdmin = {
            windows.navigateToGameMasterWindow()
        },
        onNetwork = {
            menuState.isNetworkMenuOpen.value = true
        },
        onResources = {
            menuState.isResourcesMenuOpen.value = true
        },
        onSettings = {
            screen.navigateToSettings()
        },
        onDismissNetworkMenu = {
            menuState.isNetworkMenuOpen.value = false
        },
        onDismissResourcesMenu = {
            menuState.isResourcesMenuOpen.value = false
        },
    )
}

@Composable
private fun CampaignToolbarContent(
    modifier: Modifier = Modifier,
    title: State<String>,
    status: State<NetworkRepository.Status>,
    isAdmin: State<Boolean>,
    state: MenuState,
    onAdmin: () -> Unit,
    onNetwork: () -> Unit,
    onResources: () -> Unit,
    onSettings: () -> Unit,
    onDismissNetworkMenu: () -> Unit,
    onDismissResourcesMenu: () -> Unit,
) {
    TopAppBar(
        modifier = modifier,
        title = {
            Text(
                text = title.value,
            )
        },
        actions = {
            AnimatedVisibility(
                visible = isAdmin.value,
                enter = fadeIn(),
                exit = fadeOut(),
            ) {
                TextButton(
                    onClick = onAdmin,
                ) {
                    Text(
                        fontWeight = FontWeight.SemiBold,
                        text = "Admin",
                    )
                }
            }
            IconButton(
                onClick = onResources,
            ) {
                Icon(
                    painter = painterResource(Res.drawable.ic_link_24dp),
                    tint = MaterialTheme.colors.primary,
                    contentDescription = null,
                )
                DropdownMenu(
                    expanded = state.isResourcesMenuOpen.value,
                    onDismissRequest = onDismissResourcesMenu,
                    content = {
                        ResourcesMenu(
                            modifier = Modifier.width(384.dp + 96.dp),
                        )
                    },
                )
            }
            IconButton(
                onClick = onNetwork,
            ) {
                Icon(
                    painter = painterResource(
                        when (status.value) {
                            NetworkRepository.Status.CONNECTED -> Res.drawable.ic_wifi_24dp
                            NetworkRepository.Status.DISCONNECTED -> Res.drawable.ic_wifi_off_24dp
                        }
                    ),
                    tint = when (status.value) {
                        NetworkRepository.Status.CONNECTED -> MaterialTheme.lwa.colorScheme.base.primary
                        NetworkRepository.Status.DISCONNECTED -> MaterialTheme.lwa.colorScheme.base.error
                    },
                    contentDescription = null,
                )
                DropdownMenu(
                    expanded = state.isNetworkMenuOpen.value,
                    onDismissRequest = onDismissNetworkMenu,
                    content = {
                        NetworkMenu(
                            modifier = Modifier.size(384.dp + 96.dp, 240.dp),
                        )
                    },
                )
            }
            IconButton(
                onClick = onSettings,
            ) {
                Icon(
                    painter = painterResource(Res.drawable.ic_settings_24dp),
                    tint = MaterialTheme.colors.primary,
                    contentDescription = stringResource(Res.string.settings__title),
                )
            }
        },
    )
}