
package app.crossword.yourealwaysbe.forkyz.view

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Switch
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
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.unit.dp
import androidx.lifecycle.compose.collectAsStateWithLifecycle

import org.jetbrains.compose.resources.stringResource

import app.crossword.yourealwaysbe.forkyz.app.generated.resources.Res
import app.crossword.yourealwaysbe.forkyz.app.generated.resources.enter_special_desc
import app.crossword.yourealwaysbe.forkyz.app.generated.resources.special_character
import app.crossword.yourealwaysbe.forkyz.app.generated.resources.special_entry
import app.crossword.yourealwaysbe.forkyz.app.generated.resources.special_entry_force_caps
import app.crossword.yourealwaysbe.forkyz.theme.ThemeHelper

@Composable
fun SpecialEntryDialog(
    viewModel : SpecialEntryDialogViewModel,
    onClose : () -> Unit,
) {
    OKCancelDialog(
        title = Res.string.special_entry,
        summary = Res.string.enter_special_desc,
        onCancel = { onClose() },
        onOK = {
            viewModel.playResponse()
            onClose()
        },
    ) {
        val response by viewModel.response.collectAsStateWithLifecycle()
        // needs immediate updates:
        // https://medium.com/androiddevelopers/effective-state-management-for-textfield-in-compose-d6e5b070fbe5
        var currentResponse by remember(response) {
            mutableStateOf(response)
        }

        OutlinedTextField(
            label = {
                Text(stringResource(Res.string.special_character))
            },
            value = currentResponse,
            onValueChange = {
                currentResponse = it
                viewModel.setResponse(it)
            },
            modifier = Modifier.fillMaxWidth(),
        )
        Row(
            modifier = Modifier.padding(top = 10.dp),
            verticalAlignment = Alignment.CenterVertically,
        ) {
            val forceCaps by viewModel.forceCaps.collectAsStateWithLifecycle()
            Switch(
                modifier = Modifier.padding(end = 10.dp),
                checked = forceCaps,
                onCheckedChange = viewModel::setForceCaps,
            )
            Text(
                stringResource(
                    Res.string.special_entry_force_caps
                ),
            )
        }
    }
}
