<commit_before>package chat.rocket.android.chatroom.viewmodel

import DateTimeHelper
import android.content.Context
import android.graphics.Color
import android.graphics.Typeface
import android.text.SpannableString
import android.text.SpannableStringBuilder
import android.text.style.AbsoluteSizeSpan
import android.text.style.ForegroundColorSpan
import android.text.style.StyleSpan
import chat.rocket.android.R
import chat.rocket.android.helper.MessageParser
import chat.rocket.android.helper.UrlHelper
import chat.rocket.android.server.domain.MessagesRepository
import chat.rocket.android.server.domain.SITE_URL
import chat.rocket.android.server.domain.useRealName
import chat.rocket.common.model.Token
import chat.rocket.core.model.Message
import chat.rocket.core.model.MessageType.*
import chat.rocket.core.model.Value
import chat.rocket.core.model.attachment.*
import chat.rocket.core.model.url.Url
import okhttp3.HttpUrl
import timber.log.Timber

data class MessageViewModel(val context: Context,
                            private val token: Token?,
                            private val message: Message,
                            private val settings: Map<String, Value<Any>>,
                            private val parser: MessageParser,
                            private val messagesRepository: MessagesRepository) {
    val id: String = message.id
    val roomId: String = message.roomId
    val time: CharSequence
    val sender: CharSequence
    val content: CharSequence
    var quote: Message? = null
    var urlsWithMeta = arrayListOf<Url>()
    var attachmentUrl: String? = null
    var attachmentTitle: CharSequence? = null
    var attachmentType: AttachmentType? = null
    var attachmentMessageText: String? = null
    var attachmentMessageAuthor: String? = null
    var attachmentMessageIcon: String? = null
    var attachmentTimestamp: Long? = null
    var isSystemMessage: Boolean = false
    var isPinned: Boolean = false

    init {
        sender = getSenderName()
        time = getTime(message.timestamp)
        isPinned = message.pinned

        val baseUrl = settings.get(SITE_URL)
        message.urls?.let {
            if (it.isEmpty()) return@let
            for (url in it) {
                if (url.meta != null) {
                    urlsWithMeta.add(url)
                }
                baseUrl?.let {
                    val quoteUrl = HttpUrl.parse(url.url)
                    val serverUrl = HttpUrl.parse(baseUrl.value.toString())
                    if (quoteUrl != null && serverUrl != null) {
                        makeQuote(quoteUrl, serverUrl)
                    }
                }
            }
        }

        message.attachments?.let {
            if (it.isEmpty() || it[0] == null) return@let

            if (it[0] is FileAttachment) {
                val attachment = it[0] as FileAttachment
                baseUrl?.let {
                    attachmentUrl = attachmentUrl("${baseUrl.value}${attachment.url}")
                    attachmentTitle = attachment.title

                    attachmentType = when (attachment) {
                        is ImageAttachment -> AttachmentType.Image
                        is VideoAttachment -> AttachmentType.Video
                        is AudioAttachment -> AttachmentType.Audio
                        else -> null
                    }
                }
            } else if (it[0] is MessageAttachment) {
                val attachment = it[0] as MessageAttachment
                attachmentType = AttachmentType.Message
                attachmentMessageText = attachment.text ?: ""
                attachmentMessageAuthor = attachment.author ?: ""
                attachmentMessageIcon = attachment.icon
                attachmentTimestamp = attachment.timestamp
            }
        }

        content = getContent(context)
    }

    private fun makeQuote(quoteUrl: HttpUrl, serverUrl: HttpUrl) {
        if (quoteUrl.host() == serverUrl.host()) {
            val msgIdToQuote = quoteUrl.queryParameter("msg")
            Timber.d("Will quote message Id: $msgIdToQuote")
            if (msgIdToQuote != null) {
                quote = messagesRepository.getById(msgIdToQuote)
            }
        }
    }

    fun getAvatarUrl(serverUrl: String): String? {
        return message.sender?.username.let {
            return@let UrlHelper.getAvatarUrl(serverUrl, it.toString())
        }
    }

    fun getOriginalMessage() = message.message

    private fun getTime(timestamp: Long) = DateTimeHelper.getTime(DateTimeHelper.getLocalDateTime(timestamp))

    private fun getSenderName(): CharSequence {
        val username = message.sender?.username
        val realName = message.sender?.name
        val senderName = if (settings.useRealName()) realName else username
        return senderName ?: username.toString()
    }

    private fun getContent(context: Context): CharSequence {
        val contentMessage: CharSequence
        when (message.type) {
        //TODO: Add implementation for Welcome type.
            is MessageRemoved -> contentMessage = getSystemMessage(context.getString(R.string.message_removed))<commit_msg>Some refactoring<commit_after>package chat.rocket.android.chatroom.viewmodel

import DateTimeHelper
import android.content.Context
import android.graphics.Color
import android.graphics.Typeface
import android.text.SpannableString
import android.text.SpannableStringBuilder
import android.text.style.AbsoluteSizeSpan
import android.text.style.ForegroundColorSpan
import android.text.style.StyleSpan
import chat.rocket.android.R
import chat.rocket.android.helper.MessageParser
import chat.rocket.android.helper.UrlHelper
import chat.rocket.android.server.domain.MessagesRepository
import chat.rocket.android.server.domain.SITE_URL
import chat.rocket.android.server.domain.useRealName
import chat.rocket.common.model.Token
import chat.rocket.core.model.Message
import chat.rocket.core.model.MessageType.*
import chat.rocket.core.model.Value
import chat.rocket.core.model.attachment.*
import chat.rocket.core.model.url.Url
import okhttp3.HttpUrl
import timber.log.Timber

data class MessageViewModel(val context: Context,
                            private val token: Token?,
                            private val message: Message,
                            private val settings: Map<String, Value<Any>>,
                            private val parser: MessageParser,
                            private val messagesRepository: MessagesRepository) {
    val id: String = message.id
    val roomId: String = message.roomId
    val time: CharSequence
    val sender: CharSequence
    val content: CharSequence
    var quote: Message? = null
    var urlsWithMeta = arrayListOf<Url>()
    var attachmentUrl: String? = null
    var attachmentTitle: CharSequence? = null
    var attachmentType: AttachmentType? = null
    var attachmentMessageText: String? = null
    var attachmentMessageAuthor: String? = null
    var attachmentMessageIcon: String? = null
    var attachmentTimestamp: Long? = null
    var isSystemMessage: Boolean = false
    var isPinned: Boolean = false

    init {
        sender = getSenderName()
        time = getTime(message.timestamp)
        isPinned = message.pinned

        val baseUrl = settings.get(SITE_URL)
        message.urls?.let {
            if (it.isEmpty()) return@let
            for (url in it) {
                if (url.meta != null) {
                    urlsWithMeta.add(url)
                }
                baseUrl?.let {
                    val quoteUrl = HttpUrl.parse(url.url)
                    val serverUrl = HttpUrl.parse(baseUrl.value.toString())
                    if (quoteUrl != null && serverUrl != null) {
                        makeQuote(quoteUrl, serverUrl)
                    }
                }
            }
        }

        message.attachments?.let { attachments ->
            val attachment = attachments.firstOrNull()
            if (attachments.isEmpty() || attachment == null) return@let
            when (attachment) {
                is FileAttachment -> {
                    baseUrl?.let {
                        attachmentUrl = attachmentUrl("${baseUrl.value}${attachment.url}")
                        attachmentTitle = attachment.title

                        attachmentType = when (attachment) {
                            is ImageAttachment -> AttachmentType.Image
                            is VideoAttachment -> AttachmentType.Video
                            is AudioAttachment -> AttachmentType.Audio
                            else -> null
                        }
                    }
                }
                is MessageAttachment -> {
                    attachmentType = AttachmentType.Message
                    attachmentMessageText = attachment.text ?: ""
                    attachmentMessageAuthor = attachment.author ?: ""
                    attachmentMessageIcon = attachment.icon
                    attachmentTimestamp = attachment.timestamp
                }
            }
        }

        content = getContent(context)
    }

    private fun makeQuote(quoteUrl: HttpUrl, serverUrl: HttpUrl) {
        if (quoteUrl.host() == serverUrl.host()) {
            val msgIdToQuote = quoteUrl.queryParameter("msg")
            Timber.d("Will quote message Id: $msgIdToQuote")
            if (msgIdToQuote != null) {
                quote = messagesRepository.getById(msgIdToQuote)
            }
        }
    }

    fun getAvatarUrl(serverUrl: String): String? {
        return message.sender?.username.let {
            return@let UrlHelper.getAvatarUrl(serverUrl, it.toString())
        }
    }

    /**
     * Get the original message as a String.
     */
    fun getOriginalMessage() = message.message

    private fun getTime(timestamp: Long) = DateTimeHelper.getTime(DateTimeHelper.getLocalDateTime(timestamp))

    private fun getSenderName(): CharSequence {
        val username = message.sender?.username
        val realName = message.sender?.name
        val senderName = if (settings.useRealName()) realName else username
        return senderName ?: username.toString()
    }

    private fun getContent(context: Context): CharSequence {
        val contentMessage: CharSequence
        when (message.type) {
        //TODO: Add implementation for Welcome type.
            is MessageRemoved -> contentMessage = getSystemMessage(context.getString(R.string.message_removed))
{'fraction_non_alphanumeric': 0.08308633712592026, 'fraction_numerical': 0.0006692800458934888, 'mean_word_length': 2.077375698734922, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6348570', 'n_tokens_mistral': 2616, 'n_tokens_neox': 2542, 'n_words': 608}