package de.trainingstagebuch.app.ui.screens

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Button
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.SnackbarHostState
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import de.trainingstagebuch.app.R
import de.trainingstagebuch.app.ui.ErrorBanner
import de.trainingstagebuch.app.ui.ErrorSnackbarHost
import de.trainingstagebuch.app.viewmodel.ActivateStatus
import de.trainingstagebuch.app.viewmodel.ActivateViewModel

@Composable
fun ActivateScreen(
    activationCode: String,
    onActivationSuccess: () -> Unit,
    activateViewModel: ActivateViewModel = viewModel()
) {
    val state by activateViewModel.state.collectAsState()
    val context = LocalContext.current
    val snackbarHostState = remember { SnackbarHostState() }
    var dismissedError by remember { mutableStateOf<de.trainingstagebuch.app.utils.UiText?>(null) }

    LaunchedEffect(activationCode) {
        activateViewModel.activate(activationCode)
    }

    LaunchedEffect(state.status) {
        if (state.status == ActivateStatus.Success) {
            onActivationSuccess()
        }
    }

    LaunchedEffect(state.message, state.status) {
        val message = state.message
        if (state.status == ActivateStatus.Error && message != null) {
            dismissedError = null
            snackbarHostState.showSnackbar(message.resolve(context))
        }
    }

    Scaffold(
        snackbarHost = { ErrorSnackbarHost(hostState = snackbarHostState) }
    ) { paddingValues ->
        Column(
            modifier = Modifier
                .fillMaxSize()
                .padding(paddingValues)
                .padding(24.dp),
            verticalArrangement = Arrangement.Center,
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            Text(
                text = stringResource(R.string.auth_activate_title),
                style = MaterialTheme.typography.headlineSmall,
                modifier = Modifier.padding(bottom = 20.dp)
            )

            if (state.status == ActivateStatus.Loading) {
                CircularProgressIndicator(modifier = Modifier.padding(bottom = 12.dp))
                Text(text = stringResource(R.string.auth_activate_loading))
            }

            val currentError = state.message
            if (state.status == ActivateStatus.Error && currentError != null && currentError != dismissedError) {
                ErrorBanner(
                    message = currentError.resolve(context),
                    onDismiss = { dismissedError = currentError },
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(bottom = 12.dp)
                )
                Button(
                    onClick = { activateViewModel.activate(activationCode) },
                    modifier = Modifier.fillMaxWidth()
                ) {
                    Text(stringResource(R.string.auth_activate_button))
                }
            }
        }
    }
}
