/*
 * Copyright (c) 2025 Element Creations Ltd.
 * Copyright 2023-2025 New Vector Ltd.
 *
 * SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial.
 * Please see LICENSE files in the repository root for full details.
 */

package id.databisnis.android.bachot.features.messages.impl.timeline.groups

import id.databisnis.android.bachot.features.messages.impl.timeline.model.TimelineItem
import id.databisnis.android.bachot.features.messages.impl.timeline.model.event.TimelineItemAudioContent
import id.databisnis.android.bachot.features.messages.impl.timeline.model.event.TimelineItemEncryptedContent
import id.databisnis.android.bachot.features.messages.impl.timeline.model.event.TimelineItemFileContent
import id.databisnis.android.bachot.features.messages.impl.timeline.model.event.TimelineItemImageContent
import id.databisnis.android.bachot.features.messages.impl.timeline.model.event.TimelineItemLegacyCallInviteContent
import id.databisnis.android.bachot.features.messages.impl.timeline.model.event.TimelineItemLocationContent
import id.databisnis.android.bachot.features.messages.impl.timeline.model.event.TimelineItemPollContent
import id.databisnis.android.bachot.features.messages.impl.timeline.model.event.TimelineItemProfileChangeContent
import id.databisnis.android.bachot.features.messages.impl.timeline.model.event.TimelineItemRedactedContent
import id.databisnis.android.bachot.features.messages.impl.timeline.model.event.TimelineItemRoomMembershipContent
import id.databisnis.android.bachot.features.messages.impl.timeline.model.event.TimelineItemRtcNotificationContent
import id.databisnis.android.bachot.features.messages.impl.timeline.model.event.TimelineItemStateEventContent
import id.databisnis.android.bachot.features.messages.impl.timeline.model.event.TimelineItemStickerContent
import id.databisnis.android.bachot.features.messages.impl.timeline.model.event.TimelineItemTextBasedContent
import id.databisnis.android.bachot.features.messages.impl.timeline.model.event.TimelineItemUnknownContent
import id.databisnis.android.bachot.features.messages.impl.timeline.model.event.TimelineItemVideoContent
import id.databisnis.android.bachot.features.messages.impl.timeline.model.event.TimelineItemVoiceContent
import id.databisnis.android.bachot.libraries.matrix.api.timeline.MatrixTimelineItem
import id.databisnis.android.bachot.libraries.matrix.api.timeline.item.event.CallNotifyContent
import id.databisnis.android.bachot.libraries.matrix.api.timeline.item.event.FailedToParseMessageLikeContent
import id.databisnis.android.bachot.libraries.matrix.api.timeline.item.event.FailedToParseStateContent
import id.databisnis.android.bachot.libraries.matrix.api.timeline.item.event.LegacyCallInviteContent
import id.databisnis.android.bachot.libraries.matrix.api.timeline.item.event.MessageContent
import id.databisnis.android.bachot.libraries.matrix.api.timeline.item.event.PollContent
import id.databisnis.android.bachot.libraries.matrix.api.timeline.item.event.ProfileChangeContent
import id.databisnis.android.bachot.libraries.matrix.api.timeline.item.event.RedactedContent
import id.databisnis.android.bachot.libraries.matrix.api.timeline.item.event.RoomMembershipContent
import id.databisnis.android.bachot.libraries.matrix.api.timeline.item.event.StateContent
import id.databisnis.android.bachot.libraries.matrix.api.timeline.item.event.StickerContent
import id.databisnis.android.bachot.libraries.matrix.api.timeline.item.event.UnableToDecryptContent
import id.databisnis.android.bachot.libraries.matrix.api.timeline.item.event.UnknownContent

/**
 * Return true if the Event can be grouped in a collapse/expand block
 * When [canBeGrouped] returns a value, [canBeDisplayedInBubbleBlock] MUST return the opposite value.
 * Since the receiving type are not the same, the two functions exist.
 */
internal fun TimelineItem.Event.canBeGrouped(): Boolean {
    return when (content) {
        is TimelineItemTextBasedContent,
        is TimelineItemEncryptedContent,
        is TimelineItemImageContent,
        is TimelineItemStickerContent,
        is TimelineItemFileContent,
        is TimelineItemVideoContent,
        is TimelineItemAudioContent,
        is TimelineItemLocationContent,
        is TimelineItemPollContent,
        is TimelineItemVoiceContent,
        TimelineItemRedactedContent,
        TimelineItemUnknownContent,
        is TimelineItemLegacyCallInviteContent,
        is TimelineItemRtcNotificationContent -> false
        is TimelineItemProfileChangeContent,
        is TimelineItemRoomMembershipContent,
        is TimelineItemStateEventContent -> true
    }
}

/**
 * Return true if the Event can be grouped in a block of message bubbles.
 * When [canBeDisplayedInBubbleBlock] returns a value, [canBeGrouped] MUST return the opposite value.
 * Since the receiving type are not the same, the two functions exist.
 */
internal fun MatrixTimelineItem.Event.canBeDisplayedInBubbleBlock(): Boolean {
    return when (event.content) {
        // Can be grouped
        is FailedToParseMessageLikeContent,
        is MessageContent,
        RedactedContent,
        is StickerContent,
        is PollContent,
        is UnableToDecryptContent -> true
        // Can't be grouped
        is FailedToParseStateContent,
        is ProfileChangeContent,
        is RoomMembershipContent,
        UnknownContent,
        is LegacyCallInviteContent,
        CallNotifyContent,
        is StateContent -> false
    }
}
