package bou.amine.apps.readerforselfossv2.model

import bou.amine.apps.readerforselfossv2.utils.DateUtils
import bou.amine.apps.readerforselfossv2.utils.getHtmlDecoded
import kotlinx.serialization.Serializable

class SelfossModel {

    @Serializable
    data class Tag(
        val tag: String,
        val color: String,
        val unread: Int
    )

    @Serializable
    class SuccessResponse(val success: Boolean) {
        val isSuccess: Boolean
            get() = success
    }

    @Serializable
    class Stats(
        val total: Int,
        val unread: Int,
        val starred: Int
    )

    @Serializable
    data class Spout(
        val name: String,
        val description: String
    )

    @Serializable
    data class ApiVersion(
        val version: String?,
        val apiversion: String?
    ) {
        fun getApiMajorVersion() : Int {
            var versionNumber = 0
            if (apiversion != null) {
                versionNumber = apiversion.substringBefore(".").toInt()
            }
            return versionNumber
        }
    }

    @Serializable
    data class Source(
        val id: Int,
        val title: String,
        val tags: List<String>,
        val spout: String,
        val error: String,
        val icon: String?
    )

    @Serializable
    data class Item(
        val id: Int,
        val datetime: String,
        val title: String,
        val content: String,
        var unread: Boolean,
        var starred: Boolean,
        val thumbnail: String?,
        val icon: String?,
        val link: String,
        val sourcetitle: String,
        val tags: List<String>
    ) {
        // TODO: maybe find a better way to handle these kind of urls
        fun getLinkDecoded(): String {
            var stringUrl: String
            stringUrl =
                if (link.startsWith("http://news.google.com/news/") || link.startsWith("https://news.google.com/news/")) {
                    if (link.contains("&amp;url=")) {
                        link.substringAfter("&amp;url=")
                    } else {
                        this.link.replace("&amp;", "&")
                    }
                } else {
                    this.link.replace("&amp;", "&")
                }

            // handle :443 => https
            if (stringUrl.contains(":443")) {
                stringUrl = stringUrl.replace(":443", "").replace("http://", "https://")
            }

            // handle url not starting with http
            if (stringUrl.startsWith("//")) {
                stringUrl = "http:$stringUrl"
            }

            return stringUrl
        }

        fun sourceAndDateText(dateUtils: DateUtils): String =
            this.sourcetitle.getHtmlDecoded() + dateUtils.parseRelativeDate(this.datetime)

        fun toggleStar(): Item {
            this.starred = !this.starred
            return this
        }
    }

    class StatusAndData<T>(val success: Boolean, val data: T? = null) {
        companion object {
            fun <T> succes(d: T): StatusAndData<T> {
                return StatusAndData(true, d)
            }

            fun <T> error(): StatusAndData<T> {
                return StatusAndData(false)
            }
        }
    }
}