/*
 * SPDX-License-Identifier: GPL-3.0-or-later
 */
package com.runicgateway.app.ui.shard

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.runicgateway.app.R
import com.runicgateway.app.data.api.dto.GuildDto
import com.runicgateway.app.ui.components.ShardCard

/** The guild board (PLAN.md §6.2), live via SSE deltas. */
@Composable
fun GuildsScreen(
    modifier: Modifier = Modifier,
    viewModel: GuildsViewModel = hiltViewModel(),
) {
    val state by viewModel.state.collectAsStateWithLifecycle()
    val connected by viewModel.connected.collectAsStateWithLifecycle()

    LiveBoardScreen(
        emptyMessage = stringResource(R.string.guilds_empty),
        state = state,
        connected = connected,
        onRetry = viewModel::load,
        key = { it.id },
        modifier = modifier,
    ) { guild -> GuildCard(guild) }
}

@Composable
private fun GuildCard(guild: GuildDto) {
    ShardCard(Modifier.fillMaxWidth()) {
        Column(Modifier.padding(16.dp)) {
            Row(Modifier.fillMaxWidth()) {
                Text(
                    text = buildString {
                        guild.abbr?.takeIf { it.isNotBlank() }?.let { append("[").append(it).append("] ") }
                        append(guild.name ?: stringResource(R.string.guilds_fallback_name))
                    },
                    style = MaterialTheme.typography.titleMedium,
                    maxLines = 1,
                    overflow = TextOverflow.Ellipsis,
                    modifier = Modifier.weight(1f),
                )
                guild.members?.let {
                    Text(
                        text = stringResource(R.string.guilds_members, it),
                        style = MaterialTheme.typography.labelMedium,
                        color = MaterialTheme.colorScheme.onSurfaceVariant,
                    )
                }
            }
            guild.leader?.let { leader ->
                Text(
                    text = stringResource(R.string.guilds_leader, leader.label),
                    style = MaterialTheme.typography.bodySmall,
                    color = MaterialTheme.colorScheme.onSurfaceVariant,
                    modifier = Modifier.padding(top = 6.dp),
                )
            }
            guild.alliance?.takeIf { it.isNotBlank() }?.let { alliance ->
                Text(
                    text = stringResource(R.string.guilds_alliance, alliance),
                    style = MaterialTheme.typography.labelSmall,
                    color = MaterialTheme.colorScheme.onSurfaceVariant,
                    modifier = Modifier.padding(top = 2.dp),
                )
            }
        }
    }
}
