package com.github.nullptroma.wallenc.ui.screens.main.screens.storage

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.outlined.Notes
import androidx.compose.material.icons.outlined.Lock
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.Icon
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.graphics.vector.ImageVector
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.hilt.lifecycle.viewmodel.compose.hiltViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.github.nullptroma.wallenc.ui.R
import com.github.nullptroma.wallenc.ui.elements.WallencScreenContentPadding
import com.github.nullptroma.wallenc.ui.elements.WallencScreenScaffold
import com.github.nullptroma.wallenc.ui.resources.resolveText

@Composable
fun StorageHomeScreen(
    modifier: Modifier = Modifier,
    viewModel: StorageHomeViewModel = hiltViewModel(),
    onOpenTwoFa: (String) -> Unit,
    onOpenTextSecrets: (String) -> Unit,
) {
    val uiState by viewModel.state.collectAsStateWithLifecycle()

    WallencScreenScaffold(modifier = modifier) { innerPadding ->
        WallencScreenContentPadding(innerPadding) {
        Column(
            modifier = Modifier.fillMaxSize(),
            verticalArrangement = Arrangement.spacedBy(12.dp),
        ) {
            if (uiState.isLoading) {
                Box(
                    modifier = Modifier.fillMaxSize(),
                    contentAlignment = Alignment.Center,
                ) {
                    CircularProgressIndicator()
                }
                return@Column
            }

            Text(
                text = uiState.storageName.ifBlank {
                    stringResource(R.string.storage_home_unnamed_storage)
                },
                style = MaterialTheme.typography.headlineSmall,
            )
            Text(
                text = uiState.storageUuid,
                style = MaterialTheme.typography.bodySmall,
                color = MaterialTheme.colorScheme.onSurfaceVariant,
            )
            Text(
                text = stringResource(
                    R.string.storage_home_status_line,
                    if (uiState.isAvailable) {
                        stringResource(R.string.storage_home_status_available)
                    } else {
                        stringResource(R.string.storage_home_status_unavailable)
                    },
                    if (uiState.isEncrypted) {
                        stringResource(R.string.storage_home_status_encrypted)
                    } else {
                        stringResource(R.string.storage_home_status_not_encrypted)
                    },
                ),
            )

            uiState.errorNotification?.let { notification ->
                Text(
                    text = notification.resolveText(),
                    color = MaterialTheme.colorScheme.error,
                )
            }

            StorageSectionCard(
                title = stringResource(R.string.storage_home_two_fa_title, uiState.twoFaCount),
                description = stringResource(R.string.storage_home_two_fa_subtitle),
                icon = Icons.Outlined.Lock,
                enabled = uiState.canManageDomainData,
                onClick = { onOpenTwoFa(uiState.storageUuid) },
            )

            StorageSectionCard(
                title = stringResource(R.string.storage_home_text_secrets_title, uiState.textSecretsCount),
                description = stringResource(R.string.storage_home_text_secrets_subtitle),
                icon = Icons.AutoMirrored.Outlined.Notes,
                enabled = uiState.canManageDomainData,
                onClick = { onOpenTextSecrets(uiState.storageUuid) },
            )

            Text(
                text = stringResource(R.string.storage_home_future_sections),
                style = MaterialTheme.typography.bodySmall,
                color = MaterialTheme.colorScheme.onSurfaceVariant,
            )
        }
        }
    }
}

@Composable
private fun StorageSectionCard(
    title: String,
    description: String,
    icon: ImageVector,
    enabled: Boolean,
    onClick: () -> Unit,
) {
    Card(
        modifier = Modifier.fillMaxWidth(),
        colors = CardDefaults.elevatedCardColors(
            containerColor = MaterialTheme.colorScheme.surfaceContainerHigh,
        ),
        onClick = onClick,
        enabled = enabled,
    ) {
        Row(
            modifier = Modifier
                .fillMaxWidth()
                .padding(16.dp),
            horizontalArrangement = Arrangement.spacedBy(14.dp),
        ) {
            Icon(
                imageVector = icon,
                contentDescription = null,
                modifier = Modifier.size(28.dp),
                tint = MaterialTheme.colorScheme.primary,
            )
            Column(
                verticalArrangement = Arrangement.spacedBy(4.dp),
            ) {
                Text(
                    text = title,
                    style = MaterialTheme.typography.titleMedium,
                )
                Text(
                    text = description,
                    style = MaterialTheme.typography.bodySmall,
                    color = MaterialTheme.colorScheme.onSurfaceVariant,
                )
            }
        }
    }
}
