package de.ladkau.deckinadash.ui.reading

import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.Button
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import de.ladkau.deckinadash.R
import de.ladkau.deckinadash.data.ReadingDto
import de.ladkau.deckinadash.data.SpreadPositionDto
import de.ladkau.deckinadash.data.TransitDto
import de.ladkau.deckinadash.ui.cardDrawableRes

@Composable
fun ReadingScreen(
    onNavigateToSettings: () -> Unit,
    modifier: Modifier = Modifier,
    viewModel: ReadingViewModel = viewModel(),
) {
    val uiState by viewModel.uiState.collectAsState()

    Box(modifier = modifier.fillMaxSize()) {
        when (val state = uiState) {
            is ReadingUiState.Loading -> CenteredMessage(stringResource(R.string.reading_loading))
            is ReadingUiState.NotConfigured -> Column(
                modifier = Modifier.wrapContentSize(Alignment.Center).padding(24.dp),
            ) {
                Text(stringResource(R.string.reading_not_configured))
                Button(onClick = onNavigateToSettings, modifier = Modifier.padding(top = 16.dp)) {
                    Text(stringResource(R.string.nav_settings))
                }
            }
            is ReadingUiState.Error -> CenteredMessage(stringResource(R.string.reading_error))
            is ReadingUiState.Content -> ReadingContent(state.reading)
        }
    }
}

@Composable
private fun CenteredMessage(text: String) {
    Column(
        modifier = Modifier.fillMaxSize(),
        verticalArrangement = Arrangement.Center,
        horizontalAlignment = Alignment.CenterHorizontally,
    ) {
        if (text == stringResource(R.string.reading_loading)) {
            CircularProgressIndicator(modifier = Modifier.padding(bottom = 16.dp))
        }
        Text(text)
    }
}

/** Single scrollable page, same content order as
 * watch/src/c/ui_report_window.c's build_content(): date, day
 * significance, top transits+narratives, full Celtic Cross spread,
 * guidance last. */
@Composable
private fun ReadingContent(reading: ReadingDto) {
    LazyColumn(
        modifier = Modifier.fillMaxSize().padding(16.dp),
        verticalArrangement = Arrangement.spacedBy(12.dp),
    ) {
        item {
            Text(reading.date, style = MaterialTheme.typography.labelLarge)
            Text(
                reading.daySignificance.levelLabel,
                style = MaterialTheme.typography.headlineSmall,
            )
        }

        if (reading.transits.isNotEmpty()) {
            item {
                Text(
                    stringResource(R.string.reading_transits_heading),
                    style = MaterialTheme.typography.titleMedium,
                )
            }
            items(reading.transits) { transit -> TransitItem(transit) }
        }

        item { HorizontalDivider() }

        item {
            Text(
                stringResource(R.string.reading_spread_heading),
                style = MaterialTheme.typography.titleMedium,
            )
        }
        items(reading.spread) { position -> SpreadItem(position) }

        item { HorizontalDivider() }

        item {
            Text(
                stringResource(R.string.reading_guidance_heading),
                style = MaterialTheme.typography.titleMedium,
            )
            Text(reading.guidance, style = MaterialTheme.typography.bodyMedium)
        }
    }
}

@Composable
private fun TransitItem(transit: TransitDto) {
    Column {
        Text(transit.title, style = MaterialTheme.typography.bodyLarge)
        if (transit.narrative.isNotBlank()) {
            Text(transit.narrative, style = MaterialTheme.typography.bodyMedium)
        }
    }
}

@Composable
private fun SpreadItem(position: SpreadPositionDto) {
    val context = LocalContext.current
    Column {
        Text(position.positionName, style = MaterialTheme.typography.titleSmall)
        Image(
            painter = painterResource(cardDrawableRes(context, position.cardSlug)),
            contentDescription = position.cardName,
            modifier = Modifier
                .width(160.dp)
                .padding(vertical = 4.dp)
                .graphicsLayer { rotationZ = if (position.reversed) 180f else 0f },
        )
        val cardTitle = if (position.reversed) {
            "${position.cardName} ${stringResource(R.string.reading_reversed)}"
        } else {
            position.cardName
        }
        Text(cardTitle, style = MaterialTheme.typography.bodyLarge)
        Text(position.positionDescription, style = MaterialTheme.typography.bodySmall)
        Text(position.cardMeaning, style = MaterialTheme.typography.bodyMedium)
    }
}
