/*
 * F7cloud Talk - Android Client
 *
 * SPDX-FileCopyrightText: 2022 Marcel Hibbe <dev@mhibbe.de>
 * SPDX-License-Identifier: GPL-3.0-or-later
 */
package ru.f7cloud.talk.adapters.messages

import android.view.View
import ru.f7cloud.talk.R
import ru.f7cloud.talk.chat.ChatActivity
import ru.f7cloud.talk.databinding.ReactionsInsideMessageBinding
import ru.f7cloud.talk.chat.data.model.ChatMessage
import ru.f7cloud.talk.databinding.ItemThreadTitleBinding

class Thread {

    fun showThreadPreview(
        chatActivity: ChatActivity,
        message: ChatMessage,
        threadBinding: ItemThreadTitleBinding,
        reactionsBinding: ReactionsInsideMessageBinding,
        openThread: (message: ChatMessage) -> Unit
    ) {
        val isFirstMessageOfThreadInNormalChat = chatActivity.conversationThreadId == null && message.isThread
        if (isFirstMessageOfThreadInNormalChat) {
            threadBinding.threadTitleLayout.visibility = View.VISIBLE

            threadBinding.threadTitleLayout.findViewById<androidx.emoji2.widget.EmojiTextView>(R.id.threadTitle).text =
                message.threadTitle

            reactionsBinding.threadButton.visibility = View.VISIBLE

            reactionsBinding.threadButton.setContent {
                ThreadButtonComposable(
                    message.threadReplies ?: 0,
                    onButtonClick = { openThread(message) }
                )
            }
        } else {
            threadBinding.threadTitleLayout.visibility = View.GONE
            reactionsBinding.threadButton.visibility = View.GONE
        }
    }
}
