package com.github.nullptroma.wallenc.ui.elements

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.material3.BasicAlertDialog
import androidx.compose.material3.Button
import androidx.compose.material3.Card
import androidx.compose.material3.Checkbox
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.material3.TextField
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
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.focus.FocusRequester
import androidx.compose.ui.focus.focusRequester
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import com.github.nullptroma.wallenc.ui.R

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun TextEditCancelOkDialog(
    onDismiss: () -> Unit,
    onConfirmation: (String) -> Unit,
    title: String,
    startString: String = "",
) {
    var name by remember { mutableStateOf(startString) }
    val focusRequester = remember { FocusRequester() }
    BasicAlertDialog(
        onDismissRequest = { onDismiss() },
    ) {
        Card {
            Column(modifier = Modifier.padding(12.dp)) {
                Text(title, style = MaterialTheme.typography.titleLarge)
                TextField(
                    modifier = Modifier.focusRequester(focusRequester),
                    value = name,
                    onValueChange = { name = it },
                )
                Spacer(modifier = Modifier.height(24.dp))
                Row(
                    modifier = Modifier.fillMaxWidth(),
                    horizontalArrangement = Arrangement.SpaceEvenly,
                ) {
                    Button(modifier = Modifier.weight(1f), onClick = onDismiss) {
                        Text(stringResource(R.string.dialog_cancel))
                    }
                    Spacer(modifier = Modifier.width(12.dp))
                    Button(
                        modifier = Modifier.weight(1f),
                        onClick = {
                            onConfirmation(name)
                        },
                    ) {
                        Text(stringResource(R.string.dialog_ok))
                    }
                }
            }
        }
    }

    LaunchedEffect(Unit) {
        focusRequester.requestFocus()
    }
}

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun ConfirmationCancelOkDialog(onDismiss: () -> Unit, onConfirmation: () -> Unit, title: String) {
    BasicAlertDialog(
        onDismissRequest = { onDismiss() },
    ) {
        Card {
            Column(modifier = Modifier.padding(12.dp)) {
                Text(title, style = MaterialTheme.typography.titleLarge)
                Spacer(modifier = Modifier.height(24.dp))
                Row(
                    modifier = Modifier.fillMaxWidth(),
                    horizontalArrangement = Arrangement.SpaceEvenly,
                ) {
                    Button(modifier = Modifier.weight(1f), onClick = onDismiss) {
                        Text(stringResource(R.string.dialog_cancel))
                    }
                    Spacer(modifier = Modifier.width(12.dp))
                    Button(
                        modifier = Modifier.weight(1f),
                        onClick = {
                            onConfirmation()
                        },
                    ) {
                        Text(stringResource(R.string.dialog_ok))
                    }
                }
            }
        }
    }
}

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun EncryptionSetupDialog(
    onDismiss: () -> Unit,
    onConfirmation: (password: String, encryptPath: Boolean, rememberPassword: Boolean) -> Unit,
) {
    var password by remember { mutableStateOf("") }
    var encryptPath by remember { mutableStateOf(false) }
    var rememberPassword by remember { mutableStateOf(true) }
    BasicAlertDialog(onDismissRequest = onDismiss) {
        Card {
            Column(modifier = Modifier.padding(12.dp)) {
                Text(
                    stringResource(R.string.dialog_encryption_enable_title),
                    style = MaterialTheme.typography.titleLarge,
                )
                Spacer(modifier = Modifier.height(12.dp))
                TextField(
                    value = password,
                    onValueChange = { password = it },
                    label = { Text(stringResource(R.string.dialog_password_label)) },
                )
                Row(verticalAlignment = Alignment.CenterVertically) {
                    Checkbox(checked = encryptPath, onCheckedChange = { encryptPath = it })
                    Text(stringResource(R.string.dialog_encrypt_paths))
                }
                Row(verticalAlignment = Alignment.CenterVertically) {
                    Checkbox(checked = rememberPassword, onCheckedChange = { rememberPassword = it })
                    Text(stringResource(R.string.dialog_remember_password))
                }
                Spacer(modifier = Modifier.height(16.dp))
                Row(modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceEvenly) {
                    Button(modifier = Modifier.weight(1f), onClick = onDismiss) {
                        Text(stringResource(R.string.dialog_cancel))
                    }
                    Spacer(modifier = Modifier.width(12.dp))
                    Button(
                        modifier = Modifier.weight(1f),
                        onClick = { onConfirmation(password, encryptPath, rememberPassword) },
                        enabled = password.isNotEmpty(),
                    ) {
                        Text(stringResource(R.string.dialog_apply))
                    }
                }
            }
        }
    }
}

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun OpenEncryptedStorageDialog(
    onDismiss: () -> Unit,
    onConfirmation: (password: String, rememberPassword: Boolean) -> Unit,
) {
    var password by remember { mutableStateOf("") }
    var rememberPassword by remember { mutableStateOf(false) }
    BasicAlertDialog(onDismissRequest = onDismiss) {
        Card {
            Column(modifier = Modifier.padding(12.dp)) {
                Text(
                    stringResource(R.string.dialog_open_encrypted_title),
                    style = MaterialTheme.typography.titleLarge,
                )
                Spacer(modifier = Modifier.height(12.dp))
                TextField(
                    value = password,
                    onValueChange = { password = it },
                    label = { Text(stringResource(R.string.dialog_password_label)) },
                )
                Row(verticalAlignment = Alignment.CenterVertically) {
                    Checkbox(checked = rememberPassword, onCheckedChange = { rememberPassword = it })
                    Text(stringResource(R.string.dialog_remember_password))
                }
                Spacer(modifier = Modifier.height(16.dp))
                Row(modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceEvenly) {
                    Button(modifier = Modifier.weight(1f), onClick = onDismiss) {
                        Text(stringResource(R.string.dialog_cancel))
                    }
                    Spacer(modifier = Modifier.width(12.dp))
                    Button(
                        modifier = Modifier.weight(1f),
                        onClick = { onConfirmation(password, rememberPassword) },
                        enabled = password.isNotEmpty(),
                    ) {
                        Text(stringResource(R.string.dialog_open))
                    }
                }
            }
        }
    }
}

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun StorageEncryptionActionsDialog(
    onDismiss: () -> Unit,
    title: String,
    isOpened: Boolean,
    onOpen: () -> Unit,
    onClose: () -> Unit,
    onDisable: () -> Unit,
) {
    BasicAlertDialog(onDismissRequest = onDismiss) {
        Card {
            Column(modifier = Modifier.padding(12.dp)) {
                Text(title, style = MaterialTheme.typography.titleLarge)
                Spacer(modifier = Modifier.height(12.dp))
                if (isOpened) {
                    Button(onClick = onClose, modifier = Modifier.fillMaxWidth()) {
                        Text(stringResource(R.string.dialog_close))
                    }
                } else {
                    Button(onClick = onOpen, modifier = Modifier.fillMaxWidth()) {
                        Text(stringResource(R.string.dialog_open))
                    }
                }
                Spacer(modifier = Modifier.height(8.dp))
                Button(onClick = onDisable, modifier = Modifier.fillMaxWidth()) {
                    Text(stringResource(R.string.dialog_disable_encryption))
                }
                Spacer(modifier = Modifier.height(12.dp))
                Button(onClick = onDismiss, modifier = Modifier.fillMaxWidth()) {
                    Text(stringResource(R.string.dialog_done))
                }
            }
        }
    }
}
