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

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.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.AssistChip
import androidx.compose.material3.AssistChipDefaults
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
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.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.WikiPageDto
import com.runicgateway.app.ui.UiState
import com.runicgateway.app.ui.components.ErrorView
import com.runicgateway.app.ui.components.HtmlText
import com.runicgateway.app.ui.components.LoadingView

/** Wiki page detail: title, category, HTML body, tags, and backlinks (PLAN.md §6.1). */
@Composable
fun WikiPageScreen(
    onOpenPage: (slug: String) -> Unit,
    modifier: Modifier = Modifier,
    viewModel: WikiPageViewModel = hiltViewModel(),
) {
    val state by viewModel.state.collectAsStateWithLifecycle()

    when (val s = state) {
        is UiState.Loading -> LoadingView(modifier)
        is UiState.Error -> ErrorView(s.kind, onRetry = viewModel::load, modifier = modifier)
        is UiState.Success -> WikiPageContent(s.data, onOpenPage, modifier)
    }
}

@OptIn(ExperimentalLayoutApi::class)
@Composable
private fun WikiPageContent(
    page: WikiPageDto,
    onOpenPage: (String) -> Unit,
    modifier: Modifier = Modifier,
) {
    Column(
        modifier = modifier
            .fillMaxSize()
            .verticalScroll(rememberScrollState())
            .padding(20.dp),
    ) {
        Text(page.title, style = MaterialTheme.typography.headlineSmall)
        page.categoryTitle?.takeIf { it.isNotBlank() }?.let { cat ->
            Text(
                text = cat,
                style = MaterialTheme.typography.labelMedium,
                color = MaterialTheme.colorScheme.onSurfaceVariant,
                modifier = Modifier.padding(top = 4.dp),
            )
        }

        page.body?.takeIf { it.isNotBlank() }?.let { body ->
            Spacer(Modifier.height(16.dp))
            HtmlText(body, modifier = Modifier.fillMaxWidth())
        }

        if (page.tags.isNotEmpty()) {
            Spacer(Modifier.height(20.dp))
            Text(stringResource(R.string.wiki_section_tags), style = MaterialTheme.typography.titleSmall)
            FlowRow(
                horizontalArrangement = Arrangement.spacedBy(8.dp),
                modifier = Modifier.padding(top = 8.dp),
            ) {
                page.tags.forEach { tag ->
                    AssistChip(
                        onClick = { },
                        label = { Text(tag.label) },
                        border = AssistChipDefaults.assistChipBorder(enabled = true),
                    )
                }
            }
        }

        if (page.backlinks.isNotEmpty()) {
            Spacer(Modifier.height(20.dp))
            HorizontalDivider()
            Text(
                text = stringResource(R.string.wiki_section_backlinks),
                style = MaterialTheme.typography.titleSmall,
                modifier = Modifier.padding(top = 12.dp),
            )
            page.backlinks.forEach { backlink ->
                TextButton(onClick = { onOpenPage(backlink.slug) }) {
                    Text(backlink.title)
                }
            }
        }
    }
}
