package eu.gaudian.translator.view.vocabulary

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.defaultMinSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import eu.gaudian.translator.R
import eu.gaudian.translator.ui.theme.ThemePreviews
import eu.gaudian.translator.view.composable.AppButton
import eu.gaudian.translator.view.composable.AppIcons
import eu.gaudian.translator.view.composable.AppSlider
import eu.gaudian.translator.view.composable.OptionItemSwitch

@Composable
fun StartScreenContent(
    vocabularyItemsCount: Int,
    shuffleCards: Boolean,
    shuffleLanguages: Boolean,
    trainingMode: Boolean,
    dueTodayOnly: Boolean,
    onShuffleCardsChanged: (Boolean) -> Unit,
    onShuffleLanguagesChanged: (Boolean) -> Unit,
    onTrainingModeChanged: (Boolean) -> Unit,
    onDueTodayOnlyChanged: (Boolean) -> Unit,
    onAmountChanged: (Int) -> Unit,
    onStartClicked: () -> Unit,
    onClose: () -> Unit
) {
    var amount by remember { mutableIntStateOf(vocabularyItemsCount) }

    LaunchedEffect(vocabularyItemsCount) {
        amount = vocabularyItemsCount
        onAmountChanged(amount)
    }

    Column(modifier = Modifier.padding(16.dp)) {
        Box(
            modifier = Modifier
                .fillMaxWidth()
                .padding(bottom = 32.dp)
        ) {
            IconButton(
                onClick = onClose,
                modifier = Modifier
                    .size(28.dp)
                    .align(Alignment.CenterStart)
            ) {
                Icon(
                    imageVector = AppIcons.Close,
                    contentDescription = stringResource(R.string.label_close_exercise),
                    modifier = Modifier.size(36.dp)
                )
            }
            Text(
                text = stringResource(R.string.label_start_exercise),
                modifier = Modifier.align(Alignment.Center),
                style = MaterialTheme.typography.headlineMedium,
                color = MaterialTheme.colorScheme.onSurface,
            )
        }
        Text("${stringResource(id = R.string.text_amount_of_cards)}: $amount",         color = MaterialTheme.colorScheme.onSurface,
        )
        AppSlider(
            value = amount.toFloat(),
            onValueChange = {
                amount = it.toInt()
                onAmountChanged(amount)
            },
            valueRange = 1f..vocabularyItemsCount.toFloat(),
            steps = if (vocabularyItemsCount > 1) vocabularyItemsCount - 2 else 0,
        )
        Spacer(Modifier.height(16.dp))
        OptionItemSwitch(
            title = stringResource(id = R.string.text_shuffle_card_order),
            checked = shuffleCards,
            onCheckedChange = onShuffleCardsChanged
        )
        Spacer(Modifier.height(16.dp))

        OptionItemSwitch(
            title = stringResource(id = R.string.text_shuffle_languages),
            checked = shuffleLanguages,
            onCheckedChange = onShuffleLanguagesChanged
        )
        Spacer(Modifier.height(16.dp))

        OptionItemSwitch(
            title = stringResource(id = R.string.text_training_mode),
            description = stringResource(R.string.this_mode_will_not_affect_your_progress_in_stages),
            checked = trainingMode,
            onCheckedChange = onTrainingModeChanged
        )
        Spacer(Modifier.height(16.dp))
        OptionItemSwitch(
            title = stringResource(id = R.string.text_due_today_only),
            checked = dueTodayOnly,
            onCheckedChange = onDueTodayOnlyChanged
        )
        Spacer(Modifier.height(16.dp))

        AppButton (
            onClick = onStartClicked,
            modifier = Modifier
                .padding(top = 16.dp)
                .align(Alignment.CenterHorizontally)
                .defaultMinSize(minHeight = 48.dp),
        ) {
            Text(stringResource(id = R.string.label_start))
        }
    }
}

@ThemePreviews
@Composable
fun StartScreenContentPreview() {
    StartScreenContent(
        vocabularyItemsCount = 10,
        shuffleCards = false,
        shuffleLanguages = false,
        trainingMode = false,
        dueTodayOnly = false,
        onShuffleCardsChanged = {},
        onShuffleLanguagesChanged = {},
        onTrainingModeChanged = {},
        onDueTodayOnlyChanged = {},
        onAmountChanged = {},
        onStartClicked = {},
        onClose = {}
    )
}