package ru.forbion.f7cloud.core.push

object F7PushEventParser {
    fun parse(
        data: Map<String, String>,
        title: String,
        body: String,
    ): F7PushEvent {
        val type = data["type"]?.lowercase()
        val source = data["source"]?.lowercase()
        val url = data["acceptUrl"]
            ?: data["url"]
            ?: data["clickUrl"]
            ?: data["link"]
        val lowerUrl = url?.lowercase().orEmpty()

        if (type == "call") {
            return F7PushEvent.Call(
                title = title,
                body = body,
                roomToken = data["roomToken"],
                acceptUrl = data["acceptUrl"] ?: url,
                url = url,
            )
        }

        if (type == "mail" || source == "mail" ||
            lowerUrl.contains("/apps/f7mail") || lowerUrl.contains("/apps/mail")
        ) {
            return F7PushEvent.Mail(
                title = title,
                body = body,
                mailboxId = data["mailboxId"]?.toIntOrNull()
                    ?: Regex("""/box/(\d+)""", RegexOption.IGNORE_CASE).find(lowerUrl)
                        ?.groupValues?.get(1)?.toIntOrNull(),
                messageId = data["messageId"]?.toIntOrNull()
                    ?: Regex("""/thread/(\d+)""", RegexOption.IGNORE_CASE).find(lowerUrl)
                        ?.groupValues?.get(1)?.toIntOrNull(),
                url = url,
            )
        }

        if (type == "chat" || source == "spreed" || lowerUrl.contains("/apps/spreed")) {
            val roomToken = data["roomToken"]
                ?: extractTalkRoomToken(url)
            val messageId = data["messageId"]?.toLongOrNull()
                ?: Regex("""#message_(\d+)""").find(url.orEmpty())?.groupValues?.get(1)?.toLongOrNull()
            return F7PushEvent.Talk(
                title = title,
                body = body,
                roomToken = roomToken,
                messageId = messageId,
                url = url,
            )
        }

        if (source == "files" || lowerUrl.contains("/apps/files") || lowerUrl.contains("/remote.php/dav/files")) {
            return F7PushEvent.Files(
                title = title,
                body = body,
                fileId = data["fileId"]?.toLongOrNull()
                    ?: Regex("""fileid=(\d+)""", RegexOption.IGNORE_CASE).find(lowerUrl)
                        ?.groupValues?.get(1)?.toLongOrNull(),
                url = url,
            )
        }

        return F7PushEvent.Notification(
            title = title,
            body = body,
            source = source ?: data["source"],
            url = url,
        )
    }

    private fun extractTalkRoomToken(url: String?): String? {
        if (url.isNullOrBlank()) return null
        Regex("""/spreed/([a-z0-9]+)""", RegexOption.IGNORE_CASE).find(url)?.groupValues?.get(1)?.let { return it }
        Regex("""/call/([a-z0-9]+)""", RegexOption.IGNORE_CASE).find(url)?.groupValues?.get(1)?.let { return it }
        return null
    }
}
