package moe.lava.neon.ui.screens.navigator

import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.expandVertically
import androidx.compose.animation.shrinkVertically
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.plus
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.ExperimentalMaterial3ExpressiveApi
import androidx.compose.material3.Icon
import androidx.compose.material3.ListItemDefaults
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.SegmentedListItem
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import moe.lava.neon.resources.Res
import moe.lava.neon.resources.arrow_drop_down
import moe.lava.neon.resources.arrow_drop_up
import moe.lava.neon.resources.tag
import moe.lava.neon.resources.volume_up
import org.jetbrains.compose.resources.painterResource

@OptIn(ExperimentalMaterial3ExpressiveApi::class)
@Composable
fun Channels(
    model: NavigatorModel,
    bottomSpace: Dp,
    padding: PaddingValues,
    onNavigate: (NavigatorModel.Channel) -> Unit,
) {
    val colors = ListItemDefaults.colors(
        containerColor = MaterialTheme.colorScheme.surfaceContainer,
        selectedContainerColor = MaterialTheme.colorScheme.primary,
        selectedContentColor = MaterialTheme.colorScheme.onPrimary,
    )

    Column(
        modifier = Modifier
            .padding(padding.plus(PaddingValues(horizontal = 16.dp)))
            .verticalScroll(rememberScrollState()),
        verticalArrangement = Arrangement.spacedBy(ListItemDefaults.SegmentedGap),
    ) {
        for (cat in model.channels) {
            var expanded by rememberSaveable { mutableStateOf(true) }
            val base = ListItemDefaults.segmentedShapes(0, 2)
            val large = MaterialTheme.shapes.large

            SegmentedListItem(
                onClick = { expanded = !expanded },
                colors = colors,
                shapes = if (expanded) base else base.copy(shape = large),
                trailingContent = {
                    Icon(
                        painterResource(if (expanded) Res.drawable.arrow_drop_up else Res.drawable.arrow_drop_down),
                        contentDescription = null,
                        modifier = Modifier
                            .background(
                                if (expanded) MaterialTheme.colorScheme.surface else Color.Companion.Transparent,
                                shape = RoundedCornerShape(100)
                            )
                            .padding(6.dp),
                        tint = MaterialTheme.colorScheme.onSurface,
                    )
                },
            ) {
                Text(
                    text = cat.name,
                    style = MaterialTheme.typography.labelLarge,
                )
            }
            AnimatedVisibility(
                visible = expanded,
                enter = expandVertically(MaterialTheme.motionScheme.fastSpatialSpec()),
                exit = shrinkVertically(MaterialTheme.motionScheme.fastSpatialSpec()),
            ) {
                Column(verticalArrangement = Arrangement.spacedBy(ListItemDefaults.SegmentedGap)) {
                    cat.channels.forEachIndexed { idx, ch ->
                        SegmentedListItem(
                            selected = model.selected.second == ch,
                            onClick = { onNavigate(ch) },
                            colors = colors,
                            leadingContent = {
                                val res = when (ch.type) {
                                    NavigatorModel.ChannelType.Text -> Res.drawable.tag
                                    NavigatorModel.ChannelType.Voice -> Res.drawable.volume_up
                                }
                                Icon(
                                    painterResource(res),
                                    contentDescription = null,
                                    modifier = Modifier
                                        .background(
                                            MaterialTheme.colorScheme.surface,
                                            shape = RoundedCornerShape(100)
                                        )
                                        .padding(6.dp),
                                    tint = MaterialTheme.colorScheme.onSurface,
                                )
                            },
                            shapes = ListItemDefaults.segmentedShapes(
                                idx + 1,
                                cat.channels.size + 1
                            )
                        ) {
                            Text(
                                text = ch.name,
                                style = MaterialTheme.typography.labelLargeEmphasized,
                            )
                        }
                    }
                }
            }
            Spacer(Modifier.height(10.dp))
        }
        Spacer(Modifier.height(bottomSpace))
    }
}
