package dev.fyloz.plannervio.core.service

import dev.fyloz.plannervio.core.model.BackgroundImage
import io.mockk.*
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
import java.io.InputStream
import kotlin.test.assertEquals
import kotlin.test.assertNotNull
import kotlin.test.assertNull

class BackgroundImageServiceTest {
    private val service: BackgroundImageService = spyk()
    private val fakeInputStream: InputStream = mockk()
    private val backgroundImage = BackgroundImage("image", true)

    @BeforeEach
    internal fun setUp() {
        clearAllMocks()
    }

    @Nested
    inner class LoadImage {
        @Test
        fun `Image is loaded`() {
            every { service.getImageInputStream(backgroundImage) } returns fakeInputStream

            val found = service.loadImage(backgroundImage)

            assertNotNull(found)
        }

        @Test
        fun `Image is null`() {
            every { service.getImageInputStream(backgroundImage) } returns null

            val found = service.loadImage(backgroundImage)

            assertNull(found)
        }
    }

    @Nested
    inner class GetImageInputStream {
        @Test
        fun `call getBundledImageInputStream when bundled`() {
            every { service.getBundledImageInputStream(backgroundImage) } returns fakeInputStream

            val found = service.getImageInputStream(backgroundImage)

            verify(exactly = 1) { service.getBundledImageInputStream(backgroundImage) }
            assertEquals(fakeInputStream, found)
        }

        @Test
        fun `call getCustomImageInputStream when custom`() {
            val customBackgroundImage = BackgroundImage("image", false)
            every { service.getCustomImageInputStream(customBackgroundImage) } returns fakeInputStream

            val found = service.getImageInputStream(customBackgroundImage)

            verify(exactly = 1) { service.getCustomImageInputStream(customBackgroundImage) }
            assertEquals(fakeInputStream, found)
        }
    }
}
