package de.sebse.fuplanner2.ui.courses

import android.content.Context
import android.content.res.Configuration
import androidx.annotation.StringRes
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.outlined.Info
import androidx.compose.material.icons.outlined.Person
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.dimensionResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import com.google.android.material.composethemeadapter.MdcTheme
import de.sebse.fuplanner2.R
import de.sebse.fuplanner2.database.Course
import de.sebse.fuplanner2.database.Lecturer
import de.sebse.fuplanner2.utils.color.getColor

@Composable
fun CourseList(courses: List<Course>, onclick: () -> Unit) {
    LazyColumn {
        items(courses) { course -> CourseItem(course, onclick) }
    }
}

@Composable
fun CourseItem(course: Course, onclick: () -> Unit) {
    @Suppress("SimplifiableCallChain")
    CourseItem(
        id = course.uid,
        title = course.title,
        lecturers = course.lecturers
            .filter { lecturer -> lecturer.isResponsible }
            .map { lecturer -> stringResource(R.string.full_name, lecturer.firstName.substring(0, 1)+".", lecturer.lastName) }
            .joinToString(),
        type = course.type,
        onclick = onclick
    )
}

@Composable
fun CourseItem(id: Long?, title: String, lecturers: String, type: String, onclick: () -> Unit) {
    Card(
        modifier = Modifier
            .fillMaxWidth()
            .clickable(onClick = onclick)
            .padding(dimensionResource(R.dimen.card_view_margin))
    ) {
        Column(
            modifier = Modifier
                .fillMaxWidth()
                .padding(dimensionResource(R.dimen.card_view_padding))
        ) {
            Text(
                text = title,
                color = Color(getColor(LocalContext.current, id ?: 0, highContrast = true)),
                style = MaterialTheme.typography.h6
            )
            CourseItemHint(
                icon = Icons.Outlined.Person,
                imageAltRes = R.string.lecturers,
                text = lecturers
            )
            CourseItemHint(
                icon = Icons.Outlined.Info,
                imageAltRes = R.string.course_type,
                text = type
            )
        }
    }
}

@Composable
fun CourseItemHint(icon: ImageVector, @StringRes imageAltRes: Int, text: String) {
    Row {
        Icon(
            icon,
            contentDescription = stringResource(imageAltRes),
            modifier = Modifier.padding(
                start = dimensionResource(R.dimen.card_view_padding)
            )
        )
        Text(
            text = text,
            style = MaterialTheme.typography.subtitle1,
            modifier = Modifier.padding(
                start = dimensionResource(R.dimen.card_view_padding)
            )
        )
    }
}

@Preview(uiMode = Configuration.UI_MODE_NIGHT_YES)
@Composable
fun CourseListPreview() {
    val course = Course(
        userId = 1,
        lastRefreshed = 1,
        lvNumber = hashSetOf("1000"),
        title = "Kurs mit tollem Namen",
        description = "Beschreibung",
        internalId = "165464635",
        isSummerSemester = false,
        year = 2020,
        lecturers = listOf(
            Lecturer(
                firstName = "Max",
                lastName = "Maurer",
                email = "some@mail.com",
                isResponsible = false
            ),
            Lecturer(
                firstName = "Peter",
                lastName = "Engelbert",
                email = "coolio@example.com",
                isResponsible = false
            )
        ),
        moduleType = 1236,
        type = "Type"
    )
    MdcTheme {
        CourseList(listOf(
            course, course, course
        )) { }
    }
}

@Preview(uiMode = Configuration.UI_MODE_NIGHT_YES)
@Preview
@Composable
fun CourseItemPreview() {
    MdcTheme {
        CourseItem(
            12411111111,
            "Höhere Algorithmik",
            "M. Berta, P. Parker",
            "Vorlesung"
        ) {}
    }
}

