package de.sebse.fuplanner2.database

import android.content.Context
import androidx.navigation.NavController
import androidx.room.Entity
import androidx.room.ForeignKey
import androidx.room.ForeignKey.CASCADE
import androidx.room.Index
import androidx.room.PrimaryKey
import com.beust.klaxon.JsonObject
import de.sebse.fuplanner2.R
import de.sebse.fuplanner2.ui.notification.NotificationFragmentDirections
import de.sebse.fuplanner2.utils.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

@Entity(
    foreignKeys = [
        ForeignKey(onDelete = CASCADE, entity = Course::class, parentColumns = ["uid"], childColumns = ["courseId"])
    ],
    indices = [
        Index("courseId"),
        Index("id", unique = true)
    ]
)
data class Announcement (
    @PrimaryKey(autoGenerate = true) var uid: Long? = null,
    val courseId: Long,
    val lastRefreshed: Long,
    val id: String,
    val title: String,
    val body: String,
    val createdOn: Long,
    val createdBy: String,
    var attachments: List<Attachment>
): Updatable {
    override fun getIdentifier(): String =
        id

    override fun getHash(): Int =
        hashCodeOf(title, body, createdOn, createdBy, attachments)

    override fun getData(): JsonObject =
        JsonObject(mapOf("title" to title, "uid" to uid))

    override fun getNotificationEntityType(): Notifications.CourseUpdateEntity =
        Notifications.CourseUpdateEntity.ANNOUNCEMENT

    companion object : UpdatableCompanion {
        override fun notificationText(
            actCtx: Context,
            type: Notifications.CourseUpdateType,
            data: JsonObject
        ): CharSequence? = when (type) {
            Notifications.CourseUpdateType.REMOVED -> actCtx.getHtmlSpannedString(R.string.not_course_update_announcement_removed, data.string("title"))
            Notifications.CourseUpdateType.UPDATED -> actCtx.getHtmlSpannedString(R.string.not_course_update_announcement_updated, data.string("title"))
            Notifications.CourseUpdateType.ADDED -> actCtx.getHtmlSpannedString(R.string.not_course_update_announcement_added, data.string("title"))
        }

        override fun adapterText(
            actCtx: Context,
            type: Notifications.CourseUpdateType,
            data: JsonObject
        ): CharSequence? = when (type) {
            Notifications.CourseUpdateType.REMOVED -> actCtx.getHtmlSpannedString(R.string.adapter_course_update_announcement_removed, data.string("title"))
            Notifications.CourseUpdateType.UPDATED -> actCtx.getHtmlSpannedString(R.string.adapter_course_update_announcement_updated, data.string("title"))
            Notifications.CourseUpdateType.ADDED -> actCtx.getHtmlSpannedString(R.string.adapter_course_update_announcement_added, data.string("title"))
        }

        override fun adapterCallback(
            actCtx: Context,
            type: Notifications.CourseUpdateType,
            data: JsonObject,
            navController: NavController
        ): () -> Unit {
            return {
                GlobalScope.launch {
                    data.long("uid")
                        ?.let { AppDatabase.getInstance().announcementDao().getAnnouncementById(it) }
                        ?.let { AppDatabase.getInstance().courseDao().getCourseById2(it.courseId) }
                        ?.let {
                            withContext(Dispatchers.Main) {
                                navController.navigate(NotificationFragmentDirections.actionNavNotificationsToCourseDetails(it.uid!!, it.title))
                            }
                        }
                }
            }
        }
    }
}
