package eu.friedcutlet.yokan.ui.screens.library import eu.friedcutlet.yokan.data.toDb import eu.friedcutlet.yokan.data.toDomain import eu.friedcutlet.yokan.database.DbSettings import eu.friedcutlet.yokan.database.DbSortLibraryManga import eu.friedcutlet.yokan.database.DbCategoryLibraryDisplay import eu.friedcutlet.yokan.database.DbCategoryLibraryPreferences import eu.friedcutlet.yokan.database.DbLibraryGrouping import eu.friedcutlet.yokan.database.DbLibraryExtraFilters import kotlin.random.Random import eu.friedcutlet.yokan.domain.DisplayType import eu.friedcutlet.yokan.domain.ItemType /** How one item type's library is laid out and which badges its entries show. */ data class LibraryDisplay( val displayType: DisplayType, /** Columns per row; 0 derives them from the width. */ val gridSize: Int, val downloadBadges: Boolean, val language: Boolean, val source: Boolean, val localBadge: Boolean, val continueButton: Boolean, val categoryTabs: Boolean, val itemCounts: Boolean, ) internal fun LibraryDisplay.forCategory(preferences: DbCategoryLibraryPreferences?): LibraryDisplay = preferences?.display?.let { stored -> copy( displayType = stored.displayType.toDomain(), gridSize = stored.gridSize.coerceIn(0, 7), downloadBadges = stored.downloadBadges, language = stored.language, source = stored.source, localBadge = stored.localBadge, continueButton = stored.continueButton, ) } ?: this internal fun LibraryDisplay.categoryPreferences() = DbCategoryLibraryDisplay( displayType.toDb(), gridSize.coerceIn(0, 7), downloadBadges, language, source, localBadge, continueButton, ) internal fun DbSettings.withLibraryGrouping(type: ItemType, grouping: DbLibraryGrouping): DbSettings = copy(features = features.copy(library = features.library.withGrouping(type.toDb(), grouping))) internal fun DbSettings.withCategoryLibraryPreferences(type: ItemType, id: Long, preferences: DbCategoryLibraryPreferences?): DbSettings = copy(features = features.copy(library = features.library.withCategory(type.toDb(), id, preferences))) internal fun DbSettings.withCategoryLibraryDisplay(type: ItemType, id: Long, transform: (DbSettings) -> DbSettings): DbSettings { val current = features.library.categories(type.toDb())[id] ?: DbCategoryLibraryPreferences() // Apply existing display controls to the effective category display, then store only card options. val effective = libraryDisplay(type).forCategory(current) val seed = withLibraryDisplayType(type, effective.displayType).withLibraryGridSize(type, effective.gridSize) .withLibraryDisplay(type, effective.downloadBadges, effective.language, effective.source, effective.localBadge, effective.continueButton) return withCategoryLibraryPreferences(type, id, current.copy(display = transform(seed).libraryDisplay(type).categoryPreferences())) } internal fun DbSettings.withCategoryLibrarySortSelected(type: ItemType, id: Long, index: Int): DbSettings { val current = features.library.categories(type.toDb())[id] ?: DbCategoryLibraryPreferences() val before = current.sort ?: librarySort(type) return withCategoryLibraryPreferences(type, id, current.copy(sort = before.copy( index = index, reverse = if (before.index == index) !before.reverse else before.reverse, ))) } fun DbSettings.libraryDisplay(type: ItemType): LibraryDisplay = when (type) { ItemType.Manga -> LibraryDisplay( displayType = displayType.toDomain(), gridSize = (mangaGridSize ?: 0).coerceIn(0, 7), downloadBadges = libraryDownloadedChapters, language = libraryShowLanguage, source = libraryShowSource, localBadge = libraryLocalSource == true, continueButton = libraryShowContinueReadingButton, categoryTabs = libraryShowCategoryTabs, itemCounts = libraryShowNumbersOfItems, ) ItemType.Anime -> LibraryDisplay( displayType = animeDisplayType.toDomain(), gridSize = (animeGridSize ?: 0).coerceIn(0, 7), downloadBadges = animeLibraryDownloadedChapters, language = animeLibraryShowLanguage, source = animeLibraryShowSource, localBadge = animeLibraryLocalSource == true, continueButton = animeLibraryShowContinueReadingButton, categoryTabs = animeLibraryShowCategoryTabs, itemCounts = animeLibraryShowNumbersOfItems, ) ItemType.Novel -> LibraryDisplay( displayType = novelDisplayType.toDomain(), gridSize = (novelGridSize ?: 0).coerceIn(0, 7), downloadBadges = novelLibraryDownloadedChapters, language = novelLibraryShowLanguage, source = novelLibraryShowSource, localBadge = novelLibraryLocalSource == true, continueButton = novelLibraryShowContinueReadingButton, categoryTabs = novelLibraryShowCategoryTabs, itemCounts = novelLibraryShowNumbersOfItems, ) } /** Sets the given display flags for [type]; `null` keeps the stored value. */ fun DbSettings.withLibraryDisplay( type: ItemType, downloadBadges: Boolean? = null, language: Boolean? = null, source: Boolean? = null, localBadge: Boolean? = null, continueButton: Boolean? = null, categoryTabs: Boolean? = null, itemCounts: Boolean? = null, ): DbSettings = when (type) { ItemType.Manga -> copy( libraryDownloadedChapters = downloadBadges ?: libraryDownloadedChapters, libraryShowLanguage = language ?: libraryShowLanguage, libraryShowSource = source ?: libraryShowSource, libraryLocalSource = localBadge ?: libraryLocalSource, libraryShowContinueReadingButton = continueButton ?: libraryShowContinueReadingButton, libraryShowCategoryTabs = categoryTabs ?: libraryShowCategoryTabs, libraryShowNumbersOfItems = itemCounts ?: libraryShowNumbersOfItems, ) ItemType.Anime -> copy( animeLibraryDownloadedChapters = downloadBadges ?: animeLibraryDownloadedChapters, animeLibraryShowLanguage = language ?: animeLibraryShowLanguage, animeLibraryShowSource = source ?: animeLibraryShowSource, animeLibraryLocalSource = localBadge ?: animeLibraryLocalSource, animeLibraryShowContinueReadingButton = continueButton ?: animeLibraryShowContinueReadingButton, animeLibraryShowCategoryTabs = categoryTabs ?: animeLibraryShowCategoryTabs, animeLibraryShowNumbersOfItems = itemCounts ?: animeLibraryShowNumbersOfItems, ) ItemType.Novel -> copy( novelLibraryDownloadedChapters = downloadBadges ?: novelLibraryDownloadedChapters, novelLibraryShowLanguage = language ?: novelLibraryShowLanguage, novelLibraryShowSource = source ?: novelLibraryShowSource, novelLibraryLocalSource = localBadge ?: novelLibraryLocalSource, novelLibraryShowContinueReadingButton = continueButton ?: novelLibraryShowContinueReadingButton, novelLibraryShowCategoryTabs = categoryTabs ?: novelLibraryShowCategoryTabs, novelLibraryShowNumbersOfItems = itemCounts ?: novelLibraryShowNumbersOfItems, ) } fun DbSettings.withLibraryDisplayType(type: ItemType, displayType: DisplayType): DbSettings { val value = displayType.toDb() return when (type) { ItemType.Manga -> copy(displayType = value) ItemType.Anime -> copy(animeDisplayType = value) ItemType.Novel -> copy(novelDisplayType = value) } } fun DbSettings.withLibraryGridSize(type: ItemType, size: Int): DbSettings { val normalized = size.coerceIn(0, 7) return when (type) { ItemType.Manga -> copy(mangaGridSize = normalized) ItemType.Anime -> copy(animeGridSize = normalized) ItemType.Novel -> copy(novelGridSize = normalized) } } fun DbSettings.librarySearch(type: ItemType): String = when (type) { ItemType.Manga -> librarySearchQuery ItemType.Anime -> animeLibrarySearchQuery ItemType.Novel -> novelLibrarySearchQuery } fun DbSettings.withLibrarySearch(type: ItemType, query: String): DbSettings = when (type) { ItemType.Manga -> copy(librarySearchQuery = query) ItemType.Anime -> copy(animeLibrarySearchQuery = query) ItemType.Novel -> copy(novelLibrarySearchQuery = query) } fun DbSettings.libraryFilters(type: ItemType): LibraryFilterTypes = when (type) { ItemType.Manga -> LibraryFilterTypes( downloaded = libraryFilterMangasDownloadType, unread = libraryFilterMangasUnreadType, started = libraryFilterMangasStartedType, bookmarked = libraryFilterMangasBookMarkedType, completed = libraryFilterMangasCompletedType, tracking = libraryFilterMangasTrackingType, ) ItemType.Anime -> LibraryFilterTypes( downloaded = libraryFilterAnimeDownloadType, unread = libraryFilterAnimeUnreadType, started = libraryFilterAnimeStartedType, bookmarked = libraryFilterAnimeBookMarkedType, completed = libraryFilterAnimeCompletedType, tracking = libraryFilterAnimeTrackingType, ) ItemType.Novel -> LibraryFilterTypes( downloaded = libraryFilterNovelDownloadType, unread = libraryFilterNovelUnreadType, started = libraryFilterNovelStartedType, bookmarked = libraryFilterNovelBookMarkedType, completed = libraryFilterNovelCompletedType, tracking = libraryFilterNovelTrackingType, ) }.let { base -> val extra = features.library.filters(type.toDb()) base.copy(customInterval = extra.customInterval.takeIf { it in 0..2 } ?: 0, providerTracking = extra.providerTracking.filter { (id, value) -> id > 0 && value in 1..2 }) } private fun DbSettings.withLibraryCustomIntervalFilter(type: ItemType, value: Int): DbSettings = copy(features = features.copy( library = features.library.withFilters(type.toDb(), features.library.filters(type.toDb()).copy(customInterval = value)), )) internal fun DbSettings.withLibraryProviderTrackingCycled(type: ItemType, providerId: Int): DbSettings { if (providerId <= 0) return this val before = features.library.filters(type.toDb()) val next = ((before.providerTracking[providerId] ?: 0).coerceIn(0, 2) + 1) % 3 return copy(features = features.copy(library = features.library.withFilters(type.toDb(), before.copy( providerTracking = if (next == 0) before.providerTracking - providerId else before.providerTracking + (providerId to next), )))) } internal fun DbSettings.withLibraryRandomSeed(type: ItemType, seed: Long): DbSettings = copy(features = features.copy( library = features.library.copy(randomSeeds = features.library.randomSeeds + (type.toDb() to seed)), )) /** Advances [kind] for [type] through off → include → exclude. */ fun DbSettings.withLibraryFilterCycled(type: ItemType, kind: LibraryFilterKind): DbSettings { val next = (libraryFilters(type).value(kind) + 1) % 3 return when (type) { ItemType.Manga -> when (kind) { LibraryFilterKind.Downloaded -> copy(libraryFilterMangasDownloadType = next) LibraryFilterKind.Unread -> copy(libraryFilterMangasUnreadType = next) LibraryFilterKind.Started -> copy(libraryFilterMangasStartedType = next) LibraryFilterKind.Bookmarked -> copy(libraryFilterMangasBookMarkedType = next) LibraryFilterKind.Completed -> copy(libraryFilterMangasCompletedType = next) LibraryFilterKind.Tracking -> copy(libraryFilterMangasTrackingType = next) LibraryFilterKind.CustomInterval -> withLibraryCustomIntervalFilter(type, next) } ItemType.Anime -> when (kind) { LibraryFilterKind.Downloaded -> copy(libraryFilterAnimeDownloadType = next) LibraryFilterKind.Unread -> copy(libraryFilterAnimeUnreadType = next) LibraryFilterKind.Started -> copy(libraryFilterAnimeStartedType = next) LibraryFilterKind.Bookmarked -> copy(libraryFilterAnimeBookMarkedType = next) LibraryFilterKind.Completed -> copy(libraryFilterAnimeCompletedType = next) LibraryFilterKind.Tracking -> copy(libraryFilterAnimeTrackingType = next) LibraryFilterKind.CustomInterval -> withLibraryCustomIntervalFilter(type, next) } ItemType.Novel -> when (kind) { LibraryFilterKind.Downloaded -> copy(libraryFilterNovelDownloadType = next) LibraryFilterKind.Unread -> copy(libraryFilterNovelUnreadType = next) LibraryFilterKind.Started -> copy(libraryFilterNovelStartedType = next) LibraryFilterKind.Bookmarked -> copy(libraryFilterNovelBookMarkedType = next) LibraryFilterKind.Completed -> copy(libraryFilterNovelCompletedType = next) LibraryFilterKind.Tracking -> copy(libraryFilterNovelTrackingType = next) LibraryFilterKind.CustomInterval -> withLibraryCustomIntervalFilter(type, next) } } } fun DbSettings.librarySort(type: ItemType): DbSortLibraryManga = when (type) { ItemType.Manga -> sortLibraryManga ItemType.Anime -> sortLibraryAnime ItemType.Novel -> sortLibraryNovel } ?: DbSortLibraryManga() /** Selects sort [index] for [type]; selecting the current one flips its direction. */ fun DbSettings.withLibrarySortSelected(type: ItemType, index: Int): DbSettings { val current = librarySort(type) val next = current.copy( index = index, reverse = if (current.index == index) !current.reverse else current.reverse, ) val updated = when (type) { ItemType.Manga -> copy(sortLibraryManga = next) ItemType.Anime -> copy(sortLibraryAnime = next) ItemType.Novel -> copy(sortLibraryNovel = next) } return if (index == 7 && current.index != 7) updated.withLibraryRandomSeed(type, Random.nextLong()) else updated }