package com.pixelized.desktop.lwa.ui.screen.admin

import androidx.compose.foundation.Image
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.layout.widthIn
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material.DropdownMenu
import androidx.compose.material.IconButton
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Scaffold
import androidx.compose.material.Surface
import androidx.compose.material.Switch
import androidx.compose.material.Text
import androidx.compose.material.TopAppBar
import androidx.compose.runtime.Composable
import androidx.compose.runtime.State
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.navigation.NavHostController
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.rememberNavController
import com.pixelized.desktop.lwa.BuildKonfig
import com.pixelized.desktop.lwa.ui.navigation.screen.destination.gamemaster.GMActionDestination
import com.pixelized.desktop.lwa.ui.navigation.screen.destination.gamemaster.composableGameMasterActionPage
import com.pixelized.desktop.lwa.ui.navigation.screen.destination.gamemaster.composableGameMasterAlterationPage
import com.pixelized.desktop.lwa.ui.navigation.screen.destination.gamemaster.composableGameMasterCharacterPage
import com.pixelized.desktop.lwa.ui.navigation.screen.destination.gamemaster.composableGameMasterMapPage
import com.pixelized.desktop.lwa.ui.navigation.screen.destination.gamemaster.composableGameMasterObjectPage
import com.pixelized.desktop.lwa.ui.navigation.screen.destination.gamemaster.navigateToGameMasterActionPage
import com.pixelized.desktop.lwa.ui.navigation.screen.destination.gamemaster.navigateToGameMasterAlterationPage
import com.pixelized.desktop.lwa.ui.navigation.screen.destination.gamemaster.navigateToGameMasterCharacterPage
import com.pixelized.desktop.lwa.ui.navigation.screen.destination.gamemaster.navigateToGameMasterMapPage
import com.pixelized.desktop.lwa.ui.navigation.screen.destination.gamemaster.navigateToGameMasterObjectPage
import com.pixelized.desktop.lwa.ui.screen.admin.common.GMTab
import com.pixelized.desktop.lwa.ui.screen.admin.common.GMTabUio
import com.pixelized.desktop.lwa.ui.screen.campaign.toolbar.dice.DiceMenu
import com.pixelized.desktop.lwa.ui.theme.color.component.LwaSwitchColors
import com.pixelized.desktop.lwa.ui.theme.lwa
import lwacharactersheet.composeapp.generated.resources.Res
import lwacharactersheet.composeapp.generated.resources.app_version
import lwacharactersheet.composeapp.generated.resources.game_master__action
import lwacharactersheet.composeapp.generated.resources.game_master__title
import lwacharactersheet.composeapp.generated.resources.icon_d100
import org.jetbrains.compose.resources.painterResource
import org.jetbrains.compose.resources.stringResource
import org.koin.compose.viewmodel.koinViewModel

@Composable
fun GameMasterScreen(
    viewModel: AdminViewModel = koinViewModel(),
    controller: NavHostController = LocalGMScreenController.current,
) {
    val gameMaster = viewModel.isGameMaster.collectAsState()
    val isDiceMenuOpen = remember { mutableStateOf(false) }

    GameMasterContent(
        modifier = Modifier.fillMaxSize(),
        controller = controller,
        isDiceMenuOpen = isDiceMenuOpen,
        gameMaster = gameMaster,
        onGameMaster = viewModel::onGameMaster,
        onTab = {
            when (it) {
                GMTabUio.Actions -> controller.navigateToGameMasterActionPage()
                GMTabUio.Characters -> controller.navigateToGameMasterCharacterPage()
                GMTabUio.Alterations -> controller.navigateToGameMasterAlterationPage()
                GMTabUio.Objects -> controller.navigateToGameMasterObjectPage()
                GMTabUio.Map -> controller.navigateToGameMasterMapPage()
            }
        },
        onDice = {
            isDiceMenuOpen.value = it
        }
    )
}

@Composable
private fun GameMasterContent(
    modifier: Modifier = Modifier,
    controller: NavHostController = rememberNavController(),
    isDiceMenuOpen: State<Boolean>,
    gameMaster: State<Boolean>,
    onDice: (Boolean) -> Unit,
    onGameMaster: (Boolean) -> Unit,
    onTab: (GMTabUio) -> Unit,
) {
    Scaffold(
        modifier = modifier,
        topBar = {
            TopAppBar(
                title = {
                    Row(
                        horizontalArrangement = Arrangement.spacedBy(space = 4.dp),
                    ) {
                        Text(
                            modifier = Modifier.alignByBaseline(),
                            text = stringResource(Res.string.game_master__title),
                        )
                        Text(
                            modifier = Modifier.alignByBaseline(),
                            style = MaterialTheme.lwa.typography.system.caption,
                            text = stringResource(Res.string.app_version, BuildKonfig.version),
                        )
                    }
                },
                actions = {
                    Row(
                        modifier = Modifier
                            .clip(shape = CircleShape)
                            .clickable { onGameMaster(gameMaster.value.not()) }
                            .padding(all = 8.dp),
                        verticalAlignment = Alignment.CenterVertically,
                        horizontalArrangement = Arrangement.spacedBy(space = 8.dp)
                    ) {
                        Text(
                            color = MaterialTheme.lwa.colorScheme.base.primary,
                            style = MaterialTheme.typography.body2,
                            fontWeight = FontWeight.SemiBold,
                            text = stringResource(Res.string.game_master__action),
                        )
                        Switch(
                            colors = LwaSwitchColors(),
                            checked = gameMaster.value,
                            onCheckedChange = null,
                        )
                    }
                    IconButton(
                        onClick = { onDice(true) },
                    ) {
                        Image(
                            modifier = Modifier.size(32.dp),
                            painter = painterResource(Res.drawable.icon_d100),
                            contentDescription = null,
                        )
                        DropdownMenu(
                            expanded = isDiceMenuOpen.value,
                            onDismissRequest = { onDice(false) },
                            content = {
                                DiceMenu(
                                    modifier = Modifier.widthIn(min = 92.dp),
                                )
                            },
                        )
                    }
                }
            )
        },
        content = { paddingValues ->
            Box(
                modifier = Modifier
                    .fillMaxSize()
                    .padding(paddingValues),
            ) {
                Row {
                    Surface(
                        elevation = 1.dp,
                    ) {
                        Column(
                            modifier = Modifier
                                .fillMaxHeight()
                                .width(width = 64.dp)
                                .padding(vertical = 8.dp),
                            horizontalAlignment = Alignment.CenterHorizontally,
                            verticalArrangement = Arrangement.spacedBy(space = 8.dp),
                        ) {
                            GMTabUio.entries.forEach {
                                GMTab(
                                    tab = it,
                                    onClick = { onTab(it) },
                                )
                            }
                        }
                    }
                    Surface(
                        modifier = Modifier
                            .fillMaxHeight()
                            .weight(weight = 1f),
                    ) {
                        NavHost(
                            modifier = Modifier.fillMaxSize(),
                            navController = controller,
                            startDestination = GMActionDestination.navigationRoute(),
                        ) {
                            composableGameMasterActionPage()
                            composableGameMasterCharacterPage()
                            composableGameMasterAlterationPage()
                            composableGameMasterObjectPage()
                            composableGameMasterMapPage()
                        }
                    }
                }
            }
        }
    )
}