/*
 * Book's Story — free and open-source Material You eBook reader.
 * Copyright (C) 2024-2026 Acclorite
 * SPDX-License-Identifier: GPL-3.0-only
 */

package org.dueattendant149.bookshelf.domain.use_case.book

import org.dueattendant149.bookshelf.core.helpers.coerceAndPreventNaN
import org.dueattendant149.bookshelf.domain.model.reader.ReaderText
import org.dueattendant149.bookshelf.domain.model.reader.ReaderText.Chapter
import javax.inject.Inject

class GetChapterProgressUseCase @Inject constructor() {

    operator fun invoke(index: Int, text: List<ReaderText>): Pair<Chapter?, Float> {
        fun getCurrentChapter(index: Int): Chapter? {
            for (textIndex in index downTo 0) {
                val readerText = text.getOrNull(textIndex) ?: break
                if (readerText is Chapter) {
                    return readerText
                }
            }
            return null
        }

        fun getCurrentChapterProgress(currentChapter: Chapter?): Float {
            return currentChapter?.let { currentChapter ->
                val startIndex = text.indexOf(currentChapter).coerceIn(0, text.lastIndex)
                val endIndex = (text.indexOfFirst {
                    it is Chapter && text.indexOf(it) > startIndex
                }.takeIf { it != -1 }) ?: (text.lastIndex + 1)

                val currentIndexInChapter = (index - startIndex).coerceAtLeast(1)
                val chapterLength = endIndex - (startIndex + 1)
                (currentIndexInChapter / chapterLength.toFloat())
            }.coerceAndPreventNaN()
        }

        val currentChapter = getCurrentChapter(index)
        val currentChapterProgress = getCurrentChapterProgress(currentChapter)

        return currentChapter to currentChapterProgress
    }
}