package dev.privacyllc.period.feature.export

import android.content.res.Configuration
import androidx.compose.foundation.layout.Column
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.Button
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.semantics.heading
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.hilt.lifecycle.viewmodel.compose.hiltViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import dev.privacyllc.period.designsystem.PeriodTheme

/**
 * The disclosure, and one button.
 *
 * No dialog and no illustration. A dialog is for a decision that needs
 * interrupting; this needs *reading*, and the three headings are the whole
 * point of the screen. `docs/design/README.md` rules out a padlock motif for the
 * lock screens, and it would be worse here — a padlock over a screen explaining
 * that the file is **not** protected asserts the opposite of the text.
 *
 * The outcome is deliberately not rendered here. With a PIN set, the user
 * returns from the picker to the lock screen and unlocks onto Today, so this
 * screen may not exist by the time the write finishes. `PeriodApp` renders the
 * result above the nav host instead, where they actually land.
 */
@Composable
fun ExportScreen(viewModel: ExportViewModel = hiltViewModel()) {
    val outcome by viewModel.outcome.collectAsStateWithLifecycle()
    ExportScreenContent(
        working = outcome == ExportOutcome.WRITING,
        onExport = viewModel::begin,
    )
}

@Composable
internal fun ExportScreenContent(working: Boolean, onExport: () -> Unit) {
    Surface(modifier = Modifier.fillMaxSize()) {
        Column(
            modifier = Modifier
                .fillMaxSize()
                .verticalScroll(rememberScrollState())
                .padding(24.dp),
        ) {
            Text(
                ExportCopy.TITLE,
                style = MaterialTheme.typography.headlineMedium,
                modifier = Modifier.semantics { heading() },
            )

            Section(ExportCopy.WHAT_HEADING, ExportCopy.WHAT_BODY)
            Section(ExportCopy.WHERE_HEADING, ExportCopy.WHERE_BODY)
            Section(
                ExportCopy.PROTECTION_HEADING,
                ExportCopy.PROTECTION_BODY,
                emphasised = true,
            )

            Spacer(Modifier.height(28.dp))

            Button(
                onClick = onExport,
                enabled = !working,
                modifier = Modifier.fillMaxWidth(),
            ) {
                Text(if (working) ExportCopy.WORKING else ExportCopy.ACTION)
            }
        }
    }
}

@Composable
private fun Section(heading: String, body: String, emphasised: Boolean = false) {
    Spacer(Modifier.height(24.dp))
    Text(
        heading,
        style = MaterialTheme.typography.titleMedium,
        color = if (emphasised) MaterialTheme.colorScheme.error else MaterialTheme.colorScheme.onSurface,
        // Announced as a heading so a screen reader can move between the three
        // sections rather than hearing one long paragraph.
        modifier = Modifier.semantics { heading() },
    )
    Spacer(Modifier.height(6.dp))
    Text(
        body,
        style = MaterialTheme.typography.bodyMedium,
        color = MaterialTheme.colorScheme.onSurfaceVariant,
    )
}

@Preview(name = "Export · light", showBackground = true, heightDp = 1000)
@Preview(
    name = "Export · dark",
    showBackground = true,
    heightDp = 1000,
    uiMode = Configuration.UI_MODE_NIGHT_YES,
)
@Preview(name = "Export · font 2.0", showBackground = true, heightDp = 2000, fontScale = 2.0f)
@Composable
private fun ExportPreview() {
    PeriodTheme { ExportScreenContent(working = false, onExport = {}) }
}
