package de.tsschulz.tt_tagebuch.shared.api.models

import kotlinx.serialization.json.Json
import org.junit.Test
import kotlin.test.assertEquals

class DiaryDrawingSerializationTest {

    private val json = Json { ignoreUnknownKeys = true }

    @Test
    fun predefinedActivity_deserializesDrawingDataAndImageLink() {
        val raw = """{"id":7,"name":"Aufschlag","imageLink":"/images/7.png","drawingData":{"targetPosition":"5"}}"""

        val activity = json.decodeFromString(PredefinedActivityDto.serializer(), raw)

        assertEquals("/images/7.png", activity.imageLink)
        assertEquals("""{"targetPosition":"5"}""", activity.drawingData)
    }

    @Test
    fun planActivity_deserializesNestedDrawingAndGroupScope() {
        val raw = """{"id":1,"drawingData":{"selectedStartPosition":"AS1"},"groupActivities":[{"id":2,"groupId":4,"drawingData":{"targetPosition":"4"},"groupPredefinedActivity":{"id":9,"drawingData":{"strokeType":"RH"}}}]}"""

        val activity = json.decodeFromString(DiaryDateActivityItem.serializer(), raw)
        val nested = activity.groupActivities.single()

        assertEquals("""{"selectedStartPosition":"AS1"}""", activity.drawingData)
        assertEquals(4, nested.groupId)
        assertEquals("""{"targetPosition":"4"}""", nested.drawingData)
        assertEquals("""{"strokeType":"RH"}""", nested.groupPredefinedActivity?.drawingData)
    }

    @Test
    fun planActivity_findsDrawingStoredOnActivityImage() {
        val raw = """{"id":3,"predefinedActivity":{"id":10,"images":[{"id":12,"drawingData":{"strokeType":"VH","targetPosition":"5"}}]}}"""

        val activity = json.decodeFromString(DiaryDateActivityItem.serializer(), raw)

        assertEquals("""{"strokeType":"VH","targetPosition":"5"}""", activity.predefinedActivity.drawingDataForDisplay())
    }

    @Test
    fun predefinedActivity_keepsStringDrawingDataCompatible() {
        val raw = """{"id":8,"drawingData":"{\"strokeType\":\"VH\"}"}"""

        val activity = json.decodeFromString(PredefinedActivityDto.serializer(), raw)

        assertEquals("""{"strokeType":"VH"}""", activity.drawingData)
    }
}
