package solutions.tretter.githugandroid

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
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.shape.RoundedCornerShape
import androidx.compose.foundation.text.selection.SelectionContainer
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp

@Composable
fun VisualPane(repo: RepoState) {
    val configuration = LocalConfiguration.current
    val isWide = configuration.screenWidthDp >= 840

    if (isWide) {
        Row(
            modifier = Modifier.fillMaxSize(),
            horizontalArrangement = Arrangement.spacedBy(12.dp),
        ) {
            InfoPaneCard(
                title = "Workspace",
                content = repo.files.joinToString("\n") {
                    "${if (it.staged) "[staged]" else "[file]  "} ${it.name}"
                }.ifBlank { "(empty)" },
                modifier = Modifier.weight(1f),
            )
            InfoPaneCard(
                title = "Branches",
                content = buildString {
                    appendLine("HEAD -> ${repo.headBranch}")
                    repo.branches.forEach { (name, _) -> appendLine(name) }
                }.trim(),
                modifier = Modifier.weight(1f),
            )
            InfoPaneCard(
                title = "Commits",
                content = repo.commits.reversed().joinToString("\n") { "${it.id} ${it.message}" }.ifBlank { "No commits yet" },
                modifier = Modifier.weight(1f),
            )
        }
    } else {
        Column(
            modifier = Modifier
                .fillMaxWidth(),
            verticalArrangement = Arrangement.spacedBy(10.dp),
        ) {
            InfoPaneCard(
                title = "Workspace",
                content = repo.files.joinToString("\n") {
                    "${if (it.staged) "[staged]" else "[file]  "} ${it.name}"
                }.ifBlank { "(empty)" },
            )
            InfoPaneCard(
                title = "Branches",
                content = buildString {
                    appendLine("HEAD -> ${repo.headBranch}")
                    repo.branches.forEach { (name, _) -> appendLine(name) }
                }.trim(),
            )
            InfoPaneCard(
                title = "Commits",
                content = repo.commits.reversed().joinToString("\n") { "${it.id} ${it.message}" }.ifBlank { "No commits yet" },
            )
        }
    }
}

@Composable
fun InfoPaneCard(
    title: String,
    content: String,
    modifier: Modifier = Modifier,
) {
    Card(
        modifier = modifier.fillMaxWidth(),
        colors = CardDefaults.cardColors(containerColor = PanelPrimary),
        shape = RoundedCornerShape(10.dp),
    ) {
        Column(
            modifier = Modifier
                .fillMaxSize()
                .padding(8.dp),
            verticalArrangement = Arrangement.spacedBy(4.dp),
        ) {
            Text(title, color = TextPrimary, fontWeight = FontWeight.Bold)
            SelectionContainer {
                Text(
                    content,
                    color = TextSecondary,
                    fontFamily = FontFamily.Monospace,
                )
            }
        }
    }
}
