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.Stable
import androidx.compose.runtime.State
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.lifecycle.compose.collectAsStateWithLifecycle
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.openGameMasterWindow
import com.pixelized.desktop.lwa.ui.screen.campaign.map.MapScene
import com.pixelized.desktop.lwa.ui.screen.campaign.map.MapSceneViewModel
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_map_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 CampaignMenuStateUio(
    val isResourcesMenuOpen: Boolean,
    val isNetworkMenuOpen: Boolean,
    val isMapMenuOpen: Boolean,
)

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

    val title = toolBarViewModel.title.collectAsStateWithLifecycle()
    val status = toolBarViewModel.status.collectAsStateWithLifecycle()
    val isAdmin = toolBarViewModel.isAdmin.collectAsStateWithLifecycle()
    val isMapEnable = toolBarViewModel.isMapEnable.collectAsStateWithLifecycle()
    val menusState = toolBarViewModel.menusState.collectAsStateWithLifecycle()

    CampaignToolbarContent(
        title = title,
        status = status,
        isAdmin = isAdmin,
        displayMapButton = isMapEnable,
        menusState = menusState,
        onAdmin = {
            windows.openGameMasterWindow()
        },
        onSettings = {
            screen.navigateToSettings()
        },
        onNetwork = toolBarViewModel::onNetwork,
        onResources = toolBarViewModel::onResources,
        onMap = toolBarViewModel::onMap,
        onDismissNetworkMenu = toolBarViewModel::onDismissNetworkMenu,
        onDismissResourcesMenu = toolBarViewModel::onDismissResourcesMenu,
        onDismissMapMenu = toolBarViewModel::onDismissMapMenu,
        map = {
            MapScene(
                modifier = Modifier.size(640.dp),
                viewModel = mapViewModel,
            )
        }
    )
}

@OptIn(ExperimentalComposeUiApi::class)
@Composable
private fun CampaignToolbarContent(
    modifier: Modifier = Modifier,
    title: State<String>,
    status: State<NetworkRepository.Status>,
    isAdmin: State<Boolean>,
    displayMapButton: State<Boolean>,
    menusState: State<CampaignMenuStateUio>,
    onAdmin: () -> Unit,
    onNetwork: () -> Unit,
    onMap: () -> Unit,
    onResources: () -> Unit,
    onSettings: () -> Unit,
    onDismissNetworkMenu: () -> Unit,
    onDismissResourcesMenu: () -> Unit,
    onDismissMapMenu: () -> Unit,
    map: @Composable () -> 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",
                    )
                }
            }
            AnimatedVisibility(
                visible = displayMapButton.value,
                enter = fadeIn(),
                exit = fadeOut(),
            ) {
                IconButton(
                    onClick = onMap,
                ) {
                    Icon(
                        painter = painterResource(Res.drawable.ic_map_24dp),
                        tint = MaterialTheme.colors.primary,
                        contentDescription = null,
                    )
                    DropdownMenu(
                        expanded = menusState.value.isMapMenuOpen,
                        onDismissRequest = onDismissMapMenu,
                        content = { map() },
                    )
                }
            }

            IconButton(
                onClick = onResources,
            ) {
                Icon(
                    painter = painterResource(Res.drawable.ic_link_24dp),
                    tint = MaterialTheme.colors.primary,
                    contentDescription = null,
                )
                DropdownMenu(
                    expanded = menusState.value.isResourcesMenuOpen,
                    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 = menusState.value.isNetworkMenuOpen,
                    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),
                )
            }
        },
    )
}