package de.sebse.fuplanner2.ui.details_description

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.tooling.preview.PreviewParameter
import androidx.lifecycle.viewmodel.compose.viewModel
import de.sebse.fuplanner2.R
import de.sebse.fuplanner2.Tools
import de.sebse.fuplanner2.database.Course
import de.sebse.fuplanner2.ui.shared.Heading
import de.sebse.fuplanner2.ui.shared.HtmlText
import de.sebse.fuplanner2.ui.theme.AppTheme
import de.sebse.fuplanner2.ui.tools.previews.CoursePreviewProvider
import de.sebse.fuplanner2.ui.tools.viewmodels.DetailsViewModel
import de.sebse.fuplanner2.ui.tools.viewmodels.DetailsViewModelFactory

@Composable
fun CourseDescriptionScreen(tools: Tools, id: Long) {
    val coursesViewModel: DetailsViewModel = viewModel(factory = DetailsViewModelFactory(id))
    val course by coursesViewModel.course.observeAsState()
    val title = course?.title ?: stringResource(id = R.string.description)
    LaunchedEffect(title) {
        tools.setTitle(title)
    }
    CourseDescriptionScreen(course?.description ?: "")
}

@Composable
fun CourseDescriptionScreen(description: String) {
    val scrollState = rememberScrollState()
    Column(
        modifier = Modifier.verticalScroll(scrollState)
    ) {
        Heading(text = stringResource(id = R.string.description))
        HtmlText(
            html = description,
            modifier = Modifier.fillMaxWidth()
        )
    }
}

@Preview
@Composable
fun CourseDescriptionScreenPreview(@PreviewParameter(CoursePreviewProvider::class, 1) course: Course) {
    AppTheme {
        CourseDescriptionScreen(
            "scrip<b>fsdfsfg</b><br><a href='example.com'>ti</a>on"
        )
    }
}
