/*
 * 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

import androidx.compose.ui.tooling.preview.PreviewParameterProvider
import id.databisnis.android.bachot.features.messages.api.timeline.voicemessages.composer.VoiceMessageComposerState
import id.databisnis.android.bachot.features.messages.api.timeline.voicemessages.composer.aVoiceMessageComposerState
import id.databisnis.android.bachot.features.messages.api.timeline.voicemessages.composer.aVoiceMessagePreviewState
import id.databisnis.android.bachot.features.messages.impl.actionlist.ActionListState
import id.databisnis.android.bachot.features.messages.impl.actionlist.anActionListState
import id.databisnis.android.bachot.features.messages.impl.crypto.identity.IdentityChangeState
import id.databisnis.android.bachot.features.messages.impl.crypto.identity.anIdentityChangeState
import id.databisnis.android.bachot.features.messages.impl.link.LinkState
import id.databisnis.android.bachot.features.messages.impl.link.aLinkState
import id.databisnis.android.bachot.features.messages.impl.messagecomposer.MessageComposerState
import id.databisnis.android.bachot.features.messages.impl.messagecomposer.aMessageComposerState
import id.databisnis.android.bachot.features.messages.impl.pinned.banner.PinnedMessagesBannerState
import id.databisnis.android.bachot.features.messages.impl.pinned.banner.aLoadedPinnedMessagesBannerState
import id.databisnis.android.bachot.features.messages.impl.timeline.TimelineState
import id.databisnis.android.bachot.features.messages.impl.timeline.aTimelineItemList
import id.databisnis.android.bachot.features.messages.impl.timeline.aTimelineState
import id.databisnis.android.bachot.features.messages.impl.timeline.components.customreaction.CustomReactionEvents
import id.databisnis.android.bachot.features.messages.impl.timeline.components.customreaction.CustomReactionState
import id.databisnis.android.bachot.features.messages.impl.timeline.components.reactionsummary.ReactionSummaryEvents
import id.databisnis.android.bachot.features.messages.impl.timeline.components.reactionsummary.ReactionSummaryState
import id.databisnis.android.bachot.features.messages.impl.timeline.components.receipt.bottomsheet.ReadReceiptBottomSheetEvents
import id.databisnis.android.bachot.features.messages.impl.timeline.components.receipt.bottomsheet.ReadReceiptBottomSheetState
import id.databisnis.android.bachot.features.messages.impl.timeline.model.TimelineItem
import id.databisnis.android.bachot.features.messages.impl.timeline.model.event.aTimelineItemTextContent
import id.databisnis.android.bachot.features.messages.impl.timeline.protection.TimelineProtectionState
import id.databisnis.android.bachot.features.messages.impl.timeline.protection.aTimelineProtectionState
import id.databisnis.android.bachot.features.roomcall.api.RoomCallState
import id.databisnis.android.bachot.features.roomcall.api.aStandByCallState
import id.databisnis.android.bachot.features.roommembermoderation.api.RoomMemberModerationEvents
import id.databisnis.android.bachot.features.roommembermoderation.api.RoomMemberModerationState
import id.databisnis.android.bachot.libraries.architecture.AsyncData
import id.databisnis.android.bachot.libraries.designsystem.components.avatar.AvatarData
import id.databisnis.android.bachot.libraries.designsystem.components.avatar.AvatarSize
import id.databisnis.android.bachot.libraries.matrix.api.core.RoomId
import id.databisnis.android.bachot.libraries.matrix.api.core.ThreadId
import id.databisnis.android.bachot.libraries.matrix.api.encryption.identity.IdentityState
import id.databisnis.android.bachot.libraries.matrix.api.room.tombstone.SuccessorRoom
import id.databisnis.android.bachot.libraries.matrix.api.timeline.Timeline
import id.databisnis.android.bachot.libraries.textcomposer.model.MessageComposerMode
import id.databisnis.android.bachot.libraries.textcomposer.model.aTextEditorStateRich
import kotlinx.collections.immutable.ImmutableList
import kotlinx.collections.immutable.persistentListOf
import kotlinx.collections.immutable.persistentSetOf

open class MessagesStateProvider : PreviewParameterProvider<MessagesState> {
    override val values: Sequence<MessagesState>
        get() = sequenceOf(
            aMessagesState(),
            aMessagesState(composerState = aMessageComposerState(showAttachmentSourcePicker = true)),
            aMessagesState(userEventPermissions = aUserEventPermissions(canSendMessage = false)),
            aMessagesState(showReinvitePrompt = true),
            aMessagesState(composerState = aMessageComposerState(showTextFormatting = true)),
            aMessagesState(
                voiceMessageComposerState = aVoiceMessageComposerState(showPermissionRationaleDialog = true),
            ),
            aMessagesState(
                voiceMessageComposerState = aVoiceMessageComposerState(
                    voiceMessageState = aVoiceMessagePreviewState(),
                    showSendFailureDialog = true
                ),
            ),
            aMessagesState(
                pinnedMessagesBannerState = aLoadedPinnedMessagesBannerState(
                    knownPinnedMessagesCount = 4,
                    currentPinnedMessageIndex = 0,
                ),
            ),
            aMessagesState(successorRoom = SuccessorRoom(RoomId("!id:domain"), null)),
            aMessagesState(
                timelineState = aTimelineState(
                    timelineMode = Timeline.Mode.Thread(threadRootId = ThreadId("\$a-thread-id")),
                    timelineItems = aTimelineItemList(aTimelineItemTextContent()),
                )
            ),
        )
}

fun aMessagesState(
    roomName: String? = "Room name",
    roomAvatar: AvatarData = AvatarData("!id:domain", "Room name", size = AvatarSize.TimelineRoom),
    userEventPermissions: UserEventPermissions = aUserEventPermissions(),
    composerState: MessageComposerState = aMessageComposerState(
        textEditorState = aTextEditorStateRich(initialText = "Hello", initialFocus = true),
        isFullScreen = false,
        mode = MessageComposerMode.Normal,
    ),
    voiceMessageComposerState: VoiceMessageComposerState = aVoiceMessageComposerState(),
    timelineState: TimelineState = aTimelineState(
        timelineItems = aTimelineItemList(aTimelineItemTextContent()),
        // Render a focused event for an event with sender information displayed
        focusedEventIndex = 2,
    ),
    timelineProtectionState: TimelineProtectionState = aTimelineProtectionState(),
    identityChangeState: IdentityChangeState = anIdentityChangeState(),
    linkState: LinkState = aLinkState(),
    readReceiptBottomSheetState: ReadReceiptBottomSheetState = aReadReceiptBottomSheetState(),
    actionListState: ActionListState = anActionListState(),
    customReactionState: CustomReactionState = aCustomReactionState(),
    reactionSummaryState: ReactionSummaryState = aReactionSummaryState(),
    showReinvitePrompt: Boolean = false,
    roomCallState: RoomCallState = aStandByCallState(),
    pinnedMessagesBannerState: PinnedMessagesBannerState = aLoadedPinnedMessagesBannerState(),
    dmUserVerificationState: IdentityState? = null,
    roomMemberModerationState: RoomMemberModerationState = aRoomMemberModerationState(),
    successorRoom: SuccessorRoom? = null,
    eventSink: (MessagesEvents) -> Unit = {},
) = MessagesState(
    roomId = RoomId("!id:domain"),
    roomName = roomName,
    roomAvatar = roomAvatar,
    heroes = persistentListOf(),
    userEventPermissions = userEventPermissions,
    composerState = composerState,
    voiceMessageComposerState = voiceMessageComposerState,
    timelineProtectionState = timelineProtectionState,
    identityChangeState = identityChangeState,
    linkState = linkState,
    timelineState = timelineState,
    readReceiptBottomSheetState = readReceiptBottomSheetState,
    actionListState = actionListState,
    customReactionState = customReactionState,
    reactionSummaryState = reactionSummaryState,
    snackbarMessage = null,
    inviteProgress = AsyncData.Uninitialized,
    showReinvitePrompt = showReinvitePrompt,
    enableTextFormatting = true,
    roomCallState = roomCallState,
    appName = "Element",
    pinnedMessagesBannerState = pinnedMessagesBannerState,
    dmUserVerificationState = dmUserVerificationState,
    roomMemberModerationState = roomMemberModerationState,
    successorRoom = successorRoom,
    eventSink = eventSink,
)

fun aRoomMemberModerationState(
    canKick: Boolean = false,
    canBan: Boolean = false,
) = object : RoomMemberModerationState {
    override val canKick: Boolean = canKick
    override val canBan: Boolean = canBan
    override val eventSink: (RoomMemberModerationEvents) -> Unit = {}
}

fun aUserEventPermissions(
    canRedactOwn: Boolean = false,
    canRedactOther: Boolean = false,
    canSendMessage: Boolean = true,
    canSendReaction: Boolean = true,
    canPinUnpin: Boolean = false,
) = UserEventPermissions(
    canRedactOwn = canRedactOwn,
    canRedactOther = canRedactOther,
    canSendMessage = canSendMessage,
    canSendReaction = canSendReaction,
    canPinUnpin = canPinUnpin,
)

fun aReactionSummaryState(
    target: ReactionSummaryState.Summary? = null,
    eventSink: (ReactionSummaryEvents) -> Unit = {}
) = ReactionSummaryState(
    target = target,
    eventSink = eventSink,
)

fun aCustomReactionState(
    target: CustomReactionState.Target = CustomReactionState.Target.None,
    recentEmojis: ImmutableList<String> = persistentListOf(),
    eventSink: (CustomReactionEvents) -> Unit = {},
) = CustomReactionState(
    target = target,
    recentEmojis = recentEmojis,
    selectedEmoji = persistentSetOf(),
    eventSink = eventSink,
)

fun aReadReceiptBottomSheetState(
    selectedEvent: TimelineItem.Event? = null,
    eventSink: (ReadReceiptBottomSheetEvents) -> Unit = {},
) = ReadReceiptBottomSheetState(
    selectedEvent = selectedEvent,
    eventSink = eventSink,
)
