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

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
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.BrandDto
import com.runicgateway.app.data.api.dto.StatusDto
import com.runicgateway.app.ui.UiState
import com.runicgateway.app.ui.components.BrandHero
import com.runicgateway.app.ui.components.ErrorView
import com.runicgateway.app.ui.components.FeatureCard
import com.runicgateway.app.ui.components.LoadingView
import com.runicgateway.app.ui.components.OnlineDot
import com.runicgateway.app.ui.components.PillTone

/**
 * Home / status (PLAN.md §6.1): the shard's name + tagline from branding, and a
 * live site-status card (online vs maintenance) with the backend version.
 */
@Composable
fun HomeScreen(
    brand: BrandDto?,
    modifier: Modifier = Modifier,
    viewModel: HomeViewModel = 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 -> HomeContent(brand, s.data, modifier)
    }
}

@Composable
private fun HomeContent(brand: BrandDto?, status: StatusDto, modifier: Modifier = Modifier) {
    Column(
        modifier = modifier
            .fillMaxSize()
            .verticalScroll(rememberScrollState())
            .padding(20.dp),
    ) {
        // The instance's hero above the title block (§5.6) — Home is the one screen
        // with a hero-shaped space. Its bottom gap rides on the image's own modifier
        // rather than a Spacer, so an instance with no hero (or one whose hero fails
        // to load) opens on the title exactly where it has always been.
        BrandHero(hero = brand?.hero, modifier = Modifier.padding(bottom = 16.dp))

        Text(
            text = brand?.name?.takeIf { it.isNotBlank() } ?: stringResource(R.string.app_name),
            style = MaterialTheme.typography.headlineMedium,
        )
        brand?.tagline?.takeIf { it.isNotBlank() }?.let { tagline ->
            Text(
                text = tagline,
                style = MaterialTheme.typography.titleSmall,
                color = MaterialTheme.colorScheme.onSurfaceVariant,
                modifier = Modifier.padding(top = 4.dp),
            )
        }

        Spacer(Modifier.height(20.dp))

        StatusCard(status)

        brand?.description?.takeIf { it.isNotBlank() }?.let { desc ->
            Spacer(Modifier.height(16.dp))
            Text(text = desc, style = MaterialTheme.typography.bodyMedium)
        }
    }
}

@Composable
private fun StatusCard(status: StatusDto) {
    val online = !status.isMaintenance

    FeatureCard {
        Row(verticalAlignment = Alignment.CenterVertically) {
            OnlineDot(if (online) PillTone.Success else PillTone.Danger)
            Spacer(Modifier.width(10.dp))
            Text(
                text = stringResource(
                    if (online) R.string.home_status_live else R.string.home_status_maintenance,
                ),
                style = MaterialTheme.typography.titleMedium,
                color = MaterialTheme.colorScheme.onSurface,
            )
        }
        if (status.isMaintenance && status.statusMessage.isNotBlank()) {
            Text(
                text = status.statusMessage,
                style = MaterialTheme.typography.bodyMedium,
                modifier = Modifier.padding(top = 6.dp),
            )
        }
        if (status.version.server.isNotBlank()) {
            Text(
                text = stringResource(
                    R.string.home_server_version,
                    status.version.server,
                    status.version.api,
                ),
                style = MaterialTheme.typography.labelMedium,
                color = MaterialTheme.colorScheme.onSurfaceVariant,
                modifier = Modifier.padding(top = 10.dp),
            )
        }
    }
}
