package bou.amine.apps.readerforselfossv2.utils

import bou.amine.apps.readerforselfossv2.dao.ITEM
import bou.amine.apps.readerforselfossv2.dao.SOURCE
import bou.amine.apps.readerforselfossv2.dao.TAG
import bou.amine.apps.readerforselfossv2.model.SelfossModel

fun TAG.toView(): SelfossModel.Tag =
    SelfossModel.Tag(
        this.name,
        this.color,
        this.unread.toInt()
    )

fun SOURCE.toView(): SelfossModel.Source =
    SelfossModel.Source(
        this.id.toInt(),
        this.title,
        this.tags.split(","),
        this.spout,
        this.error,
        this.icon
    )

fun SelfossModel.Source.toEntity(): SOURCE =
    SOURCE(
        this.id.toString(),
        this.title.getHtmlDecoded(),
        this.tags.joinToString(","),
        this.spout,
        this.error,
        this.icon.orEmpty()
    )

fun SelfossModel.Tag.toEntity(): TAG =
    TAG(
        this.tag,
        this.color,
        this.unread.toLong()
    )

fun ITEM.toView(): SelfossModel.Item =
    SelfossModel.Item(
        this.id.toInt(),
        this.datetime,
        this.title,
        this.content,
        this.unread,
        this.starred,
        this.thumbnail,
        this.icon,
        this.link,
        this.sourcetitle,
        this.tags.split(",")
    )

fun SelfossModel.Item.toEntity(): ITEM =
    ITEM(
        this.id.toString(),
        this.datetime,
        this.title.getHtmlDecoded(),
        this.content,
        this.unread,
        this.starred,
        this.thumbnail,
        this.icon,
        this.link,
        this.title.getHtmlDecoded(),
        this.tags.joinToString(",")
    )