package dev.privacyllc.period.feature.export

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.semantics.LiveRegionMode
import androidx.compose.ui.semantics.liveRegion
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.unit.dp
import androidx.hilt.lifecycle.viewmodel.compose.hiltViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle

/**
 * Writes a parked export, and reports what happened — wherever the user is.
 *
 * ## Why the result is not shown on the export screen
 *
 * Choosing a destination leaves the app. Coming back can re-lock it, and the
 * user then unlocks onto Today rather than onto the screen they started from —
 * so a result rendered by `ExportScreen` would frequently be rendered to nobody.
 * "Nothing happened" and "a broken file is sitting in your Drive" look identical
 * from the outside, and only one of them needs the user to do something.
 *
 * One renderer, hosted above the tabs, driven by `ExportController`.
 *
 * ## The write happens here because here is after the unlock
 *
 * This composable exists only inside the gate's unlocked branch. Draining the
 * destination anywhere earlier would let somebody who picked up the phone during
 * the save dialog receive the entire history without ever passing the lock.
 */
@Composable
fun ExportHost(viewModel: ExportViewModel = hiltViewModel()) {
    val context = LocalContext.current
    val destination by viewModel.destination.collectAsStateWithLifecycle()
    val outcome by viewModel.outcome.collectAsStateWithLifecycle()

    LaunchedEffect(destination) {
        if (destination != null) viewModel.writePendingExport(context)
    }

    val message = when (outcome) {
        ExportOutcome.DONE -> ExportCopy.DONE
        ExportOutcome.FAILED -> ExportCopy.FAILED
        ExportOutcome.FAILED_PARTIAL_FILE -> ExportCopy.FAILED_PARTIAL
        // A cancelled picker says nothing at all: the user changed their mind,
        // which is not an event worth a message.
        else -> null
    } ?: return

    Surface(color = MaterialTheme.colorScheme.surfaceVariant) {
        Column(Modifier.fillMaxWidth().padding(horizontal = 24.dp, vertical = 12.dp)) {
            Text(
                message,
                style = MaterialTheme.typography.bodyMedium,
                color = if (outcome == ExportOutcome.DONE) {
                    MaterialTheme.colorScheme.onSurfaceVariant
                } else {
                    MaterialTheme.colorScheme.error
                },
                // The most important accessibility line in this feature. The
                // result arrives after returning from another app's UI, with no
                // focus change, so without a live region a screen-reader user is
                // never told whether their data was saved.
                modifier = Modifier.semantics { liveRegion = LiveRegionMode.Polite },
            )
            TextButton(onClick = viewModel::acknowledge) { Text(ExportCopy.ACKNOWLEDGE) }
        }
    }
}
