package com.ponzischeme89.memby.data

import com.ponzischeme89.memby.data.model.MediaRating
import com.ponzischeme89.memby.data.model.displayable
import com.ponzischeme89.memby.data.model.formattedScore
import com.ponzischeme89.memby.data.model.ratingDisplayLimit
import com.ponzischeme89.memby.data.model.ratingsStripVisible
import com.ponzischeme89.memby.data.model.wordmark
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Test

class RatingsTest {
    @Test fun `sources retain familiar compact presentation`() {
        val imdb = MediaRating("imdb", "IMDb", "8.2", "/10")
        val rt = MediaRating("tomatoes", "Rotten Tomatoes", "91", "%")
        val letterboxd = MediaRating("letterboxd", "Letterboxd", "4.1", "/5")
        assertEquals("IMDb", imdb.wordmark())
        assertEquals("8.2", imdb.formattedScore())
        assertEquals("RT", rt.wordmark())
        assertEquals("91%", rt.formattedScore())
        assertEquals("4.1", letterboxd.formattedScore())
    }

    @Test fun `missing invalid and duplicate scores are omitted`() {
        val ratings = listOf(
            MediaRating("imdb", "IMDb", "", "/10"),
            MediaRating("tmdb", "TMDb", "0", "/10"),
            MediaRating("trakt", "Trakt", "80", "%"),
            MediaRating("trakt", "Trakt", "81", "%"),
        )
        assertEquals(listOf("80"), ratings.displayable().map(MediaRating::score))
    }

    @Test fun `visibility setting hides the whole strip`() {
        val ratings = listOf(MediaRating("imdb", "IMDb", "8.2", "/10"))
        assertTrue(ratingsStripVisible(true, ratings))
        assertFalse(ratingsStripVisible(false, ratings))
        assertFalse(ratingsStripVisible(true, emptyList()))
    }

    @Test fun `narrow layouts truncate progressively`() {
        assertEquals(1, ratingDisplayLimit(100))
        assertEquals(2, ratingDisplayLimit(180))
        assertEquals(3, ratingDisplayLimit(300))
        assertEquals(Int.MAX_VALUE, ratingDisplayLimit(500))
    }

    @Test fun `ratings preference belongs to each profile`() {
        val profile = EmbyProfile("id", "server", "token", "user", "name", showRatingsStrip = false)
        assertFalse(profile.showRatingsStrip)
        assertTrue(profile.copy(id = "other", showRatingsStrip = true).showRatingsStrip)
    }
}
