/*
 * 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.data.parser.file

import org.dueattendant149.bookshelf.R
import org.dueattendant149.bookshelf.core.log.logE
import org.dueattendant149.bookshelf.core.ui.UIText
import org.dueattendant149.bookshelf.data.model.file.CachedFile
import org.dueattendant149.bookshelf.domain.model.library.Book
import javax.inject.Inject

private const val TAG = "TxtFileParser"

class TxtFileParser @Inject constructor() : FileParser {

    override suspend fun parse(cachedFile: CachedFile): Book? {
        return try {
            val title = cachedFile.name.substringBeforeLast(".").trim()

            Book(
                title = title,
                author = UIText.StringResource(R.string.unknown_author),
                description = null,
                scrollIndex = 0,
                scrollOffset = 0,
                progress = 0f,
                filePath = cachedFile.path,
                lastOpened = null,
                categories = emptyList(),
                coverImage = null
            )
        } catch (e: Exception) {
            logE(TAG, "Could not parse file with message: ${e.message}.")
            null
        }
    }
}