package de.trainingstagebuch.app.ui.screens

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.Button
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import de.trainingstagebuch.app.R
import de.trainingstagebuch.app.ui.ErrorBanner
import de.trainingstagebuch.app.ui.TrainingstagebuchTopBar
import de.trainingstagebuch.app.viewmodel.DatenschutzViewModel

@Composable
fun DatenschutzScreen(
    viewModel: DatenschutzViewModel = viewModel()
) {
    val state by viewModel.state.collectAsState()

    val sections = listOf(
        R.string.datenschutz_section_controller_title to R.string.datenschutz_section_controller_body,
        R.string.datenschutz_section_purpose_title to R.string.datenschutz_section_purpose_body,
        R.string.datenschutz_section_data_title to R.string.datenschutz_section_data_body,
        R.string.datenschutz_section_recipients_title to R.string.datenschutz_section_recipients_body,
        R.string.datenschutz_section_storage_title to R.string.datenschutz_section_storage_body,
        R.string.datenschutz_section_rights_title to R.string.datenschutz_section_rights_body,
        R.string.datenschutz_section_security_title to R.string.datenschutz_section_security_body,
        R.string.datenschutz_section_updates_title to R.string.datenschutz_section_updates_body
    )

    Scaffold(
        topBar = { TrainingstagebuchTopBar(title = stringResource(R.string.datenschutz_title)) }
    ) { paddingValues ->
        Column(
            modifier = Modifier
                .fillMaxSize()
                .padding(paddingValues)
                .padding(16.dp),
            verticalArrangement = Arrangement.spacedBy(10.dp)
        ) {
            Text(
                text = stringResource(R.string.datenschutz_title),
                style = MaterialTheme.typography.headlineSmall
            )

            state.errorMessage?.let {
                ErrorBanner(
                    message = it.resolve(androidx.compose.ui.platform.LocalContext.current),
                    onDismiss = viewModel::retry
                )
            }

            LazyColumn(verticalArrangement = Arrangement.spacedBy(12.dp)) {
                items(sections) { section ->
                    Column(verticalArrangement = Arrangement.spacedBy(4.dp)) {
                        Text(
                            text = stringResource(section.first),
                            style = MaterialTheme.typography.titleMedium
                        )
                        Text(
                            text = stringResource(section.second),
                            style = MaterialTheme.typography.bodyMedium
                        )
                    }
                }
            }

            Button(onClick = viewModel::retry) {
                Text(stringResource(R.string.common_retry))
            }
        }
    }
}
