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.viewmodel.ImpressumViewModel

@Composable
fun ImpressumScreen(
    viewModel: ImpressumViewModel = viewModel()
) {
    val state by viewModel.state.collectAsState()

    val sections = listOf(
        R.string.impressum_section_provider_title to R.string.impressum_section_provider_body,
        R.string.impressum_section_contact_title to R.string.impressum_section_contact_body,
        R.string.impressum_section_representative_title to R.string.impressum_section_representative_body,
        R.string.impressum_section_vat_title to R.string.impressum_section_vat_body,
        R.string.impressum_section_content_title to R.string.impressum_section_content_body
    )

    Scaffold { paddingValues ->
        Column(
            modifier = Modifier
                .fillMaxSize()
                .padding(paddingValues)
                .padding(16.dp),
            verticalArrangement = Arrangement.spacedBy(10.dp)
        ) {
            Text(
                text = stringResource(R.string.impressum_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))
            }
        }
    }
}
