package de.haushalt.app.ui.schema

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.Button
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedButton
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavController
import de.haushalt.app.ui.task.DatePickerField

@Composable
fun SchemaEditScreen(
    navController: NavController,
    schemaId: Int,
    viewModel: SchemaEditViewModel = viewModel(),
) {
    LaunchedEffect(schemaId) {
        viewModel.load(schemaId)
    }

    if (viewModel.isLoading) {
        Text("Lädt…", modifier = Modifier.padding(16.dp))
        return
    }

    Column(
        modifier = Modifier
            .fillMaxSize()
            .padding(16.dp)
            .verticalScroll(rememberScrollState()),
        verticalArrangement = Arrangement.spacedBy(12.dp),
    ) {
        OutlinedTextField(
            value = viewModel.name,
            onValueChange = { viewModel.name = it },
            label = { Text("Name") },
            modifier = Modifier.fillMaxWidth(),
            singleLine = true,
        )

        Row(
            modifier = Modifier.fillMaxWidth(),
            horizontalArrangement = Arrangement.spacedBy(8.dp),
        ) {
            SchemaStatusDropdown(
                current = viewModel.status,
                onChange = { viewModel.status = it },
                modifier = Modifier.weight(1f),
            )
            TaskStatusDropdown(
                current = viewModel.taskStatus,
                onChange = { viewModel.taskStatus = it },
                modifier = Modifier.weight(1f),
            )
        }

        RepeatTypeDropdown(
            current = viewModel.repeatType,
            onChange = { viewModel.repeatType = it },
        )

        if (viewModel.repeatType == "none") {
            DatePickerField(
                value = viewModel.date,
                onChange = { viewModel.date = it },
                label = "Datum",
            )
        }

        if (viewModel.repeatType in listOf("weekly", "2week", "4week")) {
            WeekdaySelector(
                selected = viewModel.weekly,
                onChange = { viewModel.weekly = it },
            )
        }

        if (viewModel.repeatType == "days") {
            DaysSelector(
                days = viewModel.days,
                onChange = { viewModel.days = it },
            )
        }

        if (viewModel.repeatType == "monthly") {
            MonthdaySelector(
                selected = viewModel.monthly,
                onChange = { viewModel.monthly = it },
            )
        }

        if (viewModel.repeatType !in listOf("none", "days")) {
            Row(
                modifier = Modifier.fillMaxWidth(),
                horizontalArrangement = Arrangement.spacedBy(8.dp),
            ) {
                DatePickerField(
                    value = viewModel.start,
                    onChange = { viewModel.start = it },
                    label = "Start",
                    modifier = Modifier.weight(1f),
                )
                DatePickerField(
                    value = viewModel.end,
                    onChange = { viewModel.end = it },
                    label = "Ende",
                    modifier = Modifier.weight(1f),
                )
            }
        }

        viewModel.error?.let {
            Text(it, color = MaterialTheme.colorScheme.error)
        }

        Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) {
            Button(
                enabled = !viewModel.isSubmitting && viewModel.name.isNotBlank(),
                onClick = { viewModel.update(schemaId) { navController.popBackStack() } },
            ) {
                Text("Aktualisieren")
            }
            OutlinedButton(onClick = { navController.popBackStack() }) {
                Text("Abbrechen")
            }
        }
    }
}
