/*
 * 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.mapper.book

import androidx.core.net.toUri
import org.dueattendant149.bookshelf.R
import org.dueattendant149.bookshelf.core.ui.UIText
import org.dueattendant149.bookshelf.data.local.dto.BookEntity
import org.dueattendant149.bookshelf.domain.model.library.Book
import javax.inject.Inject

class BookMapperImpl @Inject constructor() : BookMapper {
    override fun toBookEntity(book: Book): BookEntity {
        return BookEntity(
            id = book.id,
            title = book.title,
            filePath = book.filePath,
            scrollIndex = book.scrollIndex,
            scrollOffset = book.scrollOffset,
            progress = book.progress,
            author = book.author.getAsString() ?: "",
            description = book.description,
            image = book.coverImage?.toString(),
            categories = book.categories,
            remoteId = book.remoteId,
            libraryId = book.libraryId,
            mediaType = book.mediaType,
            hasAudio = book.hasAudio,
            hasEbook = book.hasEbook,
            audioDuration = book.audioDuration,
            coverUrl = book.coverUrl,
            lastSyncedAt = book.lastSyncedAt,
        )
    }

    override fun toBook(bookEntity: BookEntity): Book {
        return Book(
            id = bookEntity.id,
            title = bookEntity.title,
            author = bookEntity.author.let { author ->
                if (author.isNotBlank()) UIText.StringValue(author)
                else UIText.StringResource(R.string.unknown_author)
            },
            description = bookEntity.description,
            scrollIndex = bookEntity.scrollIndex,
            scrollOffset = bookEntity.scrollOffset,
            progress = bookEntity.progress,
            filePath = bookEntity.filePath,
            lastOpened = null,
            coverImage = bookEntity.image?.toUri(),
            categories = bookEntity.categories,
            remoteId = bookEntity.remoteId,
            libraryId = bookEntity.libraryId,
            mediaType = bookEntity.mediaType,
            hasAudio = bookEntity.hasAudio,
            hasEbook = bookEntity.hasEbook,
            audioDuration = bookEntity.audioDuration,
            coverUrl = bookEntity.coverUrl,
            lastSyncedAt = bookEntity.lastSyncedAt,
        )
    }
}