package de.webidsolutions.metaplugindemo.scenes

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.systemBarsPadding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.Checkbox
import androidx.compose.material3.CheckboxDefaults
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
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.graphics.Color
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import de.webidsolutions.metaplugindemo.R

enum class ThemingChoice(val useXml: Boolean) {
    COMPOSE(false),
    XML(true)
}

@Composable
fun MetaPluginDemoScreen(
    logText: String,
    onStartClicked: (
        useAutoIdent: Boolean,
        usePayOnServer: Boolean,
        useEidOnServer: Boolean,
        useVideo: Boolean,
        themingChoice: ThemingChoice
    ) -> Unit
) {
    var useAutoIdent by remember { mutableStateOf(false) }
    var usePayOnServer by remember { mutableStateOf(false) }
    var useEidOnServer by remember { mutableStateOf(false) }
    var useVideo by remember { mutableStateOf(false) }
    var themingChoice by remember { mutableStateOf(ThemingChoice.COMPOSE) }

    Column(
        modifier = Modifier
            .fillMaxSize()
            .padding(start = 10.dp, top = 10.dp, end = 10.dp)
            .systemBarsPadding()
    ) {
        Text(
            text = stringResource(R.string.choose_plugins),
            style = MaterialTheme.typography.headlineSmall,
            modifier = Modifier.padding(start = 10.dp)
        )

        Spacer(modifier = Modifier.height(10.dp))

        Column {
            CheckboxWithLabel(
                checked = useAutoIdent,
                onCheckedChange = { useAutoIdent = it },
                label = stringResource(R.string.autoid)
            )

            CheckboxWithLabel(
                checked = usePayOnServer,
                onCheckedChange = { usePayOnServer = it },
                label = stringResource(R.string.accountid)
            )

            CheckboxWithLabel(
                checked = useEidOnServer,
                onCheckedChange = { useEidOnServer = it },
                label = stringResource(R.string.eid)
            )

            CheckboxWithLabel(
                checked = useVideo,
                onCheckedChange = { useVideo = it },
                label = stringResource(R.string.videoid)
            )
        }

        Spacer(modifier = Modifier.height(10.dp))

        Text(
            text = stringResource(R.string.choose_theming),
            style = MaterialTheme.typography.headlineSmall,
            modifier = Modifier.padding(start = 10.dp)
        )

        Spacer(modifier = Modifier.height(10.dp))

        Column {
            CheckboxWithLabel(
                checked = themingChoice == ThemingChoice.COMPOSE,
                onCheckedChange = { themingChoice = ThemingChoice.COMPOSE },
                label = "Compose-Theming"
            )

            CheckboxWithLabel(
                checked = themingChoice == ThemingChoice.XML,
                onCheckedChange = { themingChoice = ThemingChoice.XML },
                label = "XML-Theming"
            )
        }

        Spacer(modifier = Modifier.height(10.dp))

        Text(
            text = stringResource(R.string.log_headline),
            style = MaterialTheme.typography.headlineSmall,
            modifier = Modifier.padding(start = 10.dp)
        )

        Spacer(modifier = Modifier.height(10.dp))

        Box(
            modifier = Modifier
                .weight(1f)
                .fillMaxWidth()
                .padding(horizontal = 10.dp)
        ) {
            val scrollState = rememberScrollState()

            LaunchedEffect(logText) {
                scrollState.animateScrollTo(scrollState.maxValue)
            }

            Surface(
                modifier = Modifier.fillMaxSize(),
                shape = MaterialTheme.shapes.small
            ) {
                Text(
                    text = logText.ifEmpty { stringResource(R.string.log_placeholder) },
                    modifier = Modifier
                        .fillMaxSize()
                        .verticalScroll(scrollState)
                        .padding(16.dp)
                        .padding(bottom = 80.dp),
                    style = MaterialTheme.typography.bodyMedium
                )
            }

            Button(
                onClick = {
                    onStartClicked(
                        useAutoIdent,
                        usePayOnServer,
                        useEidOnServer,
                        useVideo,
                        themingChoice
                    )
                },
                modifier = Modifier
                    .align(Alignment.BottomCenter)
                    .fillMaxWidth()
                    .height(60.dp),
                colors = ButtonDefaults.buttonColors(
                    containerColor = Color(0xFF05B1FB)
                )
            ) {
                Text(
                    text = stringResource(R.string.start_button),
                    color = Color.White
                )
            }
        }
    }
}

@Composable
private fun CheckboxWithLabel(
    checked: Boolean,
    onCheckedChange: (Boolean) -> Unit,
    label: String
) {
    Row(
        verticalAlignment = Alignment.CenterVertically,
    ) {
        Checkbox(
            checked = checked,
            onCheckedChange = onCheckedChange,
            colors = CheckboxDefaults.colors(
                checkedColor = Color(0xFF05B1FB),
                checkmarkColor = Color.White
            )
        )

        Spacer(modifier = Modifier.width(2.dp))
        Text(text = label)
    }
}

@Preview(
    name = "MetaPluginDemoScreen - Long Log",
    showBackground = true,
    showSystemUi = true
)
@Composable
private fun MetaPluginDemoScreenLongLogPreview() {
    MaterialTheme {
        MetaPluginDemoScreen(
            logText = buildString {
                repeat(20) { index ->
                    appendLine("Log entry ${index + 1}: Processing...")
                }
            },
            onStartClicked = { _, _, _, _, _ -> }
        )
    }
}

