package de.sebse.fuplanner2.ui.details

import android.content.res.Configuration
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
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.platform.LocalContext
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.MenuItem
import de.sebse.fuplanner2.R
import de.sebse.fuplanner2.Tools
import de.sebse.fuplanner2.database.Announcement
import de.sebse.fuplanner2.database.Course
import de.sebse.fuplanner2.database.Event
import de.sebse.fuplanner2.database.Lecturer
import de.sebse.fuplanner2.ui.details.components.AnnouncementItem
import de.sebse.fuplanner2.ui.details.components.EventItem
import de.sebse.fuplanner2.ui.details.components.LecturerItem
import de.sebse.fuplanner2.ui.details.components.QuickLinks
import de.sebse.fuplanner2.ui.shared.Heading
import de.sebse.fuplanner2.ui.theme.AppTheme
import de.sebse.fuplanner2.ui.tools.previews.AnnouncementPreviewProvider
import de.sebse.fuplanner2.ui.tools.previews.CoursePreviewProvider
import de.sebse.fuplanner2.ui.tools.previews.EventPreviewProvider
import de.sebse.fuplanner2.ui.tools.viewmodels.DetailsViewModel
import de.sebse.fuplanner2.ui.tools.viewmodels.DetailsViewModelFactory
import kotlin.math.min

@Composable
fun CourseDetailsScreen(tools: Tools, id: Long) {
    val coursesViewModel: DetailsViewModel = viewModel(factory = DetailsViewModelFactory(id))
    val course by coursesViewModel.course.observeAsState()
    val announcements by coursesViewModel.announcements.observeAsState()
    val events by coursesViewModel.events.observeAsState()
    val title = course?.title
    val context = LocalContext.current
    LaunchedEffect(title) {
        title?.let { tools.setTitle(it) }
    }
    LaunchedEffect(true) {
        coursesViewModel.refresh(context)
    }
    CourseDetailsScreen(
        course?.lecturers ?: emptyList(),
        announcements ?: emptyList(),
        events ?: emptyList(),
        id,
        course?.title ?: ""
    ) {
        tools.navTo(it)
    }
}

@Composable
fun CourseDetailsScreen(
    lecturers: List<Lecturer>,
    announcements: List<Announcement>,
    events: List<Event>,
    id: Long,
    title: String,
    onClick: (String) -> Unit
) {
    LazyColumn {
        item {
            QuickLinks(courseId = id, onClick)
            if (lecturers.isNotEmpty()) Heading(stringResource(R.string.lecturers))
        }
        items(lecturers.sortedBy { (if (it.isResponsible) "AAAA" else "ZZZZ") + it.lastName }) {
            LecturerItem(lecturer = it, courseTitle = title)
        }
        item {
            if (announcements.isNotEmpty()) Heading(stringResource(R.string.announcements))
        }
        items(items = announcements.subList(0, min(announcements.size, 3)) ?: listOf()) {
            AnnouncementItem(it) {
                onClick("${MenuItem.Courses.route}/$id/announcements/${it.uid}")
            }
        }
        item {
            if (events.isNotEmpty()) Heading(stringResource(R.string.events))
        }
        items(items = events.subList(0, min(events.size, 3))) {
            EventItem(it)
        }
        // TODO: Add current assignments, upcoming events
    }
}

@Preview(showBackground = true)
@Preview(uiMode = Configuration.UI_MODE_NIGHT_YES, showBackground = true)
@Composable
fun CourseDetailsScreenPreview(@PreviewParameter(CoursePreviewProvider::class, 1) course: Course) {
    AppTheme {
        CourseDetailsScreen(
            course.lecturers,
            AnnouncementPreviewProvider().values.take(3).toList(),
            EventPreviewProvider().values.take(3).toList(),
            course.uid!!,
            course.title
        ) { }
    }
}
