package com.fabledsword.minstrel.shared.widgets.trackactions

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.ExperimentalLayoutApi
import androidx.compose.foundation.layout.FlowRow
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Button
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.FilterChip
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.ModalBottomSheet
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.material3.rememberModalBottomSheetState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp

private data class HideReason(val wire: String, val label: String)

private val HIDE_REASONS = listOf(
    HideReason("bad_rip", "Bad rip"),
    HideReason("wrong_file", "Wrong file"),
    HideReason("wrong_tags", "Wrong tags"),
    HideReason("duplicate", "Duplicate"),
    HideReason("other", "Other"),
)

/**
 * ModalBottomSheet that collects a hide reason + optional notes and
 * pops back via [onSubmit]. Mirrors Flutter's `HideTrackSheet`. The
 * reason vocabulary matches the server wire values exactly.
 */
@OptIn(ExperimentalMaterial3Api::class, ExperimentalLayoutApi::class)
@Composable
fun HideTrackSheet(
    onSubmit: (reason: String, notes: String) -> Unit,
    onDismiss: () -> Unit,
) {
    val sheetState = rememberModalBottomSheetState(skipPartiallyExpanded = true)
    var reason by remember { mutableStateOf(HIDE_REASONS.first().wire) }
    var notes by remember { mutableStateOf("") }
    ModalBottomSheet(
        onDismissRequest = onDismiss,
        sheetState = sheetState,
    ) {
        Column(
            modifier = Modifier
                .fillMaxWidth()
                .padding(horizontal = 20.dp, vertical = 8.dp),
        ) {
            Text(
                text = "Hide this track",
                style = MaterialTheme.typography.titleLarge,
                color = MaterialTheme.colorScheme.onSurface,
            )
            Spacer(Modifier.height(8.dp))
            Text(
                text = "Pick a reason. Optional notes are visible to admins.",
                style = MaterialTheme.typography.bodyMedium,
                color = MaterialTheme.colorScheme.onSurfaceVariant,
            )
            Spacer(Modifier.height(12.dp))
            FlowRow(
                horizontalArrangement = Arrangement.spacedBy(8.dp),
                verticalArrangement = Arrangement.spacedBy(8.dp),
                modifier = Modifier.fillMaxWidth(),
            ) {
                HIDE_REASONS.forEach { r ->
                    FilterChip(
                        selected = reason == r.wire,
                        onClick = { reason = r.wire },
                        label = { Text(r.label) },
                    )
                }
            }
            Spacer(Modifier.height(12.dp))
            OutlinedTextField(
                value = notes,
                onValueChange = { notes = it },
                label = { Text("Notes (optional)") },
                modifier = Modifier.fillMaxWidth(),
                maxLines = 2,
            )
            Spacer(Modifier.height(16.dp))
            HideSheetButtons(
                onCancel = onDismiss,
                onSubmit = { onSubmit(reason, notes.trim()) },
            )
            Spacer(Modifier.height(8.dp))
        }
    }
}

@Composable
private fun HideSheetButtons(onCancel: () -> Unit, onSubmit: () -> Unit) {
    Row(
        modifier = Modifier.fillMaxWidth(),
        horizontalArrangement = Arrangement.End,
    ) {
        TextButton(onClick = onCancel) { Text("Cancel") }
        Spacer(Modifier.padding(horizontal = 4.dp))
        Button(onClick = onSubmit) { Text("Hide") }
    }
}
