package com.webtoonviewer.db

import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.PrimaryKey
import androidx.room.TypeConverter
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken

@Entity(tableName = "books")
data class Book(
    @PrimaryKey(autoGenerate = true) val id: Int = 0,
    @ColumnInfo(name = "title") val title: String,
    @ColumnInfo(name = "author") val author: String,
    @ColumnInfo(name = "description") val description: String,
    @ColumnInfo(name = "tag") val tag: String,
    @ColumnInfo(name = "img") val img: String,
    @ColumnInfo(name = "chapters") val chapters: MutableList<String>,
    @ColumnInfo(name = "chapterSum") val chapterSum: Int,
    @ColumnInfo(name = "bookmarkChapter") var bookmarkChapter: String,
    @ColumnInfo(name = "bookmarkPosition") var bookmarkPosition: Int,
    @ColumnInfo(name = "bookmarkTopOffset") var bookmarkTopOffset: Int,
    @ColumnInfo(name = "isLocal") var isLocal: Int,
    ) {
    constructor(id: Int, title: String, author: String, description: String, tag: String, img: String, isLocal: Int) : this(
        id = id,
        title = title,
        author = author,
        description = description,
        tag = tag,
        img = img,
        chapters = mutableListOf(),
        chapterSum = 0,
        bookmarkChapter = "",
        bookmarkPosition = 0,
        bookmarkTopOffset = 0,
        isLocal = isLocal
    )
}


class Converters {
    @TypeConverter
    fun fromString(value: String): MutableList<String> {
        val listType = object : TypeToken<List<String>>() {}.type
        return Gson().fromJson(value, listType)
    }

    @TypeConverter
    fun fromList(list: MutableList<String>): String {
        return Gson().toJson(list)
    }
}
