package solutions.tretter.githugandroid

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp

@Composable
fun PaneWorkspace(
    modifier: Modifier = Modifier,
    paneLayout: PaneLayout,
    onMovePane: (PaneId, Int) -> Unit,
    onTogglePane: (PaneId) -> Unit,
    levelsContent: @Composable () -> Unit,
    visualContent: @Composable () -> Unit,
    exerciseContent: @Composable () -> Unit,
    terminalContent: @Composable () -> Unit,
) {
    Column(
        modifier = modifier.fillMaxSize(),
        verticalArrangement = Arrangement.spacedBy(0.dp),
    ) {
        paneLayout.order.forEachIndexed { index, paneId ->
            val isCollapsed = paneId in paneLayout.collapsed
            if (isCollapsed) {
                CollapsedPaneCard(
                    paneId = paneId,
                    canMoveUp = index > 0,
                    canMoveDown = index < paneLayout.order.lastIndex,
                    onMoveUp = { onMovePane(paneId, -1) },
                    onMoveDown = { onMovePane(paneId, 1) },
                    onToggleCollapse = { onTogglePane(paneId) },
                )
            } else {
                PaneCard(
                    paneId = paneId,
                    modifier = Modifier
                        .fillMaxWidth()
                        .wrapContentHeight(),
                    canMoveUp = index > 0,
                    canMoveDown = index < paneLayout.order.lastIndex,
                    onMoveUp = { onMovePane(paneId, -1) },
                    onMoveDown = { onMovePane(paneId, 1) },
                    onToggleCollapse = { onTogglePane(paneId) },
                ) {
                    when (paneId) {
                        PaneId.LEVELS -> levelsContent()
                        PaneId.VISUAL -> visualContent()
                        PaneId.EXERCISE -> exerciseContent()
                        PaneId.TERMINAL -> terminalContent()
                    }
                }
            }
        }
    }
}

@Composable
private fun PaneCard(
    paneId: PaneId,
    modifier: Modifier = Modifier,
    canMoveUp: Boolean,
    canMoveDown: Boolean,
    onMoveUp: () -> Unit,
    onMoveDown: () -> Unit,
    onToggleCollapse: () -> Unit,
    content: @Composable () -> Unit,
) {
    Card(
        modifier = modifier,
        colors = CardDefaults.cardColors(containerColor = PanelSecondary),
        shape = RoundedCornerShape(16.dp),
    ) {
        Column(modifier = Modifier.fillMaxSize()) {
            PaneHeader(
                paneId = paneId,
                collapsed = false,
                canMoveUp = canMoveUp,
                canMoveDown = canMoveDown,
                onMoveUp = onMoveUp,
                onMoveDown = onMoveDown,
                onToggleCollapse = onToggleCollapse,
            )
            Box(
                modifier = Modifier
                    .fillMaxSize()
                    .padding(horizontal = 12.dp, vertical = 10.dp),
            ) {
                content()
            }
        }
    }
}

@Composable
private fun CollapsedPaneCard(
    paneId: PaneId,
    canMoveUp: Boolean,
    canMoveDown: Boolean,
    onMoveUp: () -> Unit,
    onMoveDown: () -> Unit,
    onToggleCollapse: () -> Unit,
) {
    Card(
        modifier = Modifier.fillMaxWidth(),
        colors = CardDefaults.cardColors(containerColor = PanelSecondary),
        shape = RoundedCornerShape(16.dp),
    ) {
        PaneHeader(
            paneId = paneId,
            collapsed = true,
            canMoveUp = canMoveUp,
            canMoveDown = canMoveDown,
            onMoveUp = onMoveUp,
            onMoveDown = onMoveDown,
            onToggleCollapse = onToggleCollapse,
        )
    }
}

@Composable
private fun PaneHeader(
    paneId: PaneId,
    collapsed: Boolean,
    canMoveUp: Boolean,
    canMoveDown: Boolean,
    onMoveUp: () -> Unit,
    onMoveDown: () -> Unit,
    onToggleCollapse: () -> Unit,
) {
    Row(
        modifier = Modifier
            .fillMaxWidth()
            .background(if (collapsed) PanelTertiary else PanelPrimary)
            .padding(horizontal = 10.dp, vertical = 6.dp),
        verticalAlignment = Alignment.CenterVertically,
        horizontalArrangement = Arrangement.SpaceBetween,
    ) {
        Text(paneId.title, color = TextPrimary, fontWeight = FontWeight.Bold)
        Row(horizontalArrangement = Arrangement.spacedBy(4.dp)) {
            HeaderActionButton(label = if (collapsed) "Expand" else "Collapse", onClick = onToggleCollapse)
            HeaderActionButton(label = "↑", enabled = canMoveUp, onClick = onMoveUp)
            HeaderActionButton(label = "↓", enabled = canMoveDown, onClick = onMoveDown)
        }
    }
}

@Composable
private fun HeaderActionButton(
    label: String,
    enabled: Boolean = true,
    onClick: () -> Unit,
) {
    TextButton(
        onClick = onClick,
        enabled = enabled,
        contentPadding = PaddingValues(horizontal = 6.dp, vertical = 0.dp),
        colors = ButtonDefaults.textButtonColors(
            contentColor = Accent,
            disabledContentColor = TextMuted,
        ),
    ) {
        Text(label, fontSize = 11.sp)
    }
}
