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

import androidx.compose.animation.AnimatedVisibility
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.Card
import androidx.compose.material.Icon
import androidx.compose.material.IconButton
import androidx.compose.material.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.IntOffset
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import com.pixelized.desktop.lwa.ui.composable.scene.Scene
import com.pixelized.desktop.lwa.ui.composable.scene.camera.Camera
import com.pixelized.desktop.lwa.ui.composable.scene.cursor.Cursor
import com.pixelized.desktop.lwa.ui.composable.scene.cursor.onCursorControl
import com.pixelized.desktop.lwa.ui.composable.scene.debug.CameraDebugPanel
import com.pixelized.desktop.lwa.ui.composable.scene.debug.CursorDebugPanel
import com.pixelized.desktop.lwa.ui.composable.scene.debug.SceneDebugPanel
import com.pixelized.desktop.lwa.ui.composable.scene.rememberLayoutFromResource
import com.pixelized.desktop.lwa.ui.theme.lwa
import kotlinx.coroutines.launch
import lwacharactersheet.composeapp.generated.resources.Res
import lwacharactersheet.composeapp.generated.resources.ic_center_focus_weak_24dp
import lwacharactersheet.composeapp.generated.resources.ic_frame_bug_24dp
import lwacharactersheet.composeapp.generated.resources.ic_visibility_24dp
import lwacharactersheet.composeapp.generated.resources.ic_zoom_in_map_24dp
import lwacharactersheet.composeapp.generated.resources.ic_zoom_out_map_24dp
import lwacharactersheet.composeapp.generated.resources.image_dahome_maps
import lwacharactersheet.composeapp.generated.resources.image_dahome_regions
import org.jetbrains.compose.resources.painterResource

@Composable
fun MapScene(
    modifier: Modifier = Modifier,
    enableDebug: Boolean,
) {
    val scope = rememberCoroutineScope()

    val map = rememberLayoutFromResource(
        name = "Dahomé",
        resource = Res.drawable.image_dahome_maps,
    )
    val mapRegionOverlay = rememberLayoutFromResource(
        name = "Région",
        resource = Res.drawable.image_dahome_regions,
    )
    val camera = remember {
        Camera(
            initialZoom = 1f,
            initialOffset = IntOffset(x = -150, y = -120),
        )
    }
    val cursor = remember {
        Cursor()
    }
    val scene = remember(map, mapRegionOverlay) {
        Scene(
            layers = listOf(map, mapRegionOverlay),
            elements = emptyList(),
        )
    }
    val openDebugMenu = remember {
        mutableStateOf(false)
    }
    Scene(
        modifier = Modifier
            .onCursorControl(camera = camera, cursor = cursor)
            .then(other = modifier),
        scene = scene,
        camera = camera,
    ) {
        MapActions(
            modifier = Modifier
                .align(alignment = Alignment.BottomEnd)
                .padding(paddingValues = MaterialTheme.lwa.dimen.paddingValues),
            enableDebug = enableDebug,
            onZoomIn = {
                scope.launch {
                    camera.handleZoom(power = 0.3f)
                }
            },
            onZoomOut = {
                scope.launch {
                    camera.handleZoom(power = -0.3f)
                }
            },
            onResetCamera = {
                scope.launch {
                    camera.resetPosition()
                }
                scope.launch {
                    camera.resetZoom()
                }
            },
            onToggleLayer = {
                scope.launch {
                    scene.layers.getOrNull(1)?.let {
                        it.alpha(alpha = if (it.alpha == 0f) 1f else 0f)
                    }
                }
            },
            onToggleDebug = {
                openDebugMenu.value = openDebugMenu.value.not()
            },
        )

        AnimatedVisibility(
            modifier = Modifier.align(alignment = Alignment.TopEnd),
            visible = openDebugMenu.value,
        ) {
            Card(
                modifier = Modifier
                    .padding(paddingValues = MaterialTheme.lwa.dimen.paddingValues)
                    .verticalScroll(state = rememberScrollState()),
                backgroundColor = MaterialTheme.lwa.colorScheme.elevated.base4dp,
            ) {
                Column(
                    modifier = Modifier.padding(paddingValues = MaterialTheme.lwa.dimen.paddingValues),
                    verticalArrangement = Arrangement.spacedBy(space = 4.dp),
                ) {
                    SceneDebugPanel(
                        scene = scene,
                    )
                    CameraDebugPanel(
                        camera = camera,
                    )
                    CursorDebugPanel(
                        cursors = remember { listOf(cursor) },
                    )
                }
            }
        }
    }
}

@Composable
private fun MapActions(
    modifier: Modifier = Modifier,
    enableDebug: Boolean,
    onZoomIn: () -> Unit,
    onZoomOut: () -> Unit,
    onResetCamera: () -> Unit,
    onToggleLayer: () -> Unit,
    onToggleDebug: () -> Unit,
) {
    Row(
        modifier = modifier,
    ) {
        IconButton(
            onClick = onZoomIn,
        ) {
            Icon(
                painter = painterResource(Res.drawable.ic_zoom_in_map_24dp),
                contentDescription = null
            )
        }
        IconButton(
            onClick = onZoomOut,
        ) {
            Icon(
                painter = painterResource(Res.drawable.ic_zoom_out_map_24dp),
                contentDescription = null
            )
        }
        IconButton(
            onClick = onResetCamera,
        ) {
            Icon(
                painter = painterResource(Res.drawable.ic_center_focus_weak_24dp),
                contentDescription = null
            )
        }
        IconButton(
            onClick = onToggleLayer,
        ) {
            Icon(
                painter = painterResource(Res.drawable.ic_visibility_24dp),
                contentDescription = null
            )
        }
        AnimatedVisibility(
            visible = enableDebug,
        ) {
            IconButton(
                onClick = onToggleDebug,
            ) {
                Icon(
                    painter = painterResource(Res.drawable.ic_frame_bug_24dp),
                    contentDescription = null
                )
            }
        }
    }
}