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

import id.databisnis.android.bachot.features.messages.impl.messagesummary.FakeMessageSummaryFormatter
import id.databisnis.android.bachot.features.messages.impl.timeline.factories.TimelineItemsFactory
import id.databisnis.android.bachot.features.messages.impl.timeline.factories.TimelineItemsFactoryConfig
import id.databisnis.android.bachot.features.messages.impl.timeline.factories.event.TimelineItemContentFactory
import id.databisnis.android.bachot.features.messages.impl.timeline.factories.event.TimelineItemContentFailedToParseMessageFactory
import id.databisnis.android.bachot.features.messages.impl.timeline.factories.event.TimelineItemContentFailedToParseStateFactory
import id.databisnis.android.bachot.features.messages.impl.timeline.factories.event.TimelineItemContentMessageFactory
import id.databisnis.android.bachot.features.messages.impl.timeline.factories.event.TimelineItemContentPollFactory
import id.databisnis.android.bachot.features.messages.impl.timeline.factories.event.TimelineItemContentProfileChangeFactory
import id.databisnis.android.bachot.features.messages.impl.timeline.factories.event.TimelineItemContentRedactedFactory
import id.databisnis.android.bachot.features.messages.impl.timeline.factories.event.TimelineItemContentRoomMembershipFactory
import id.databisnis.android.bachot.features.messages.impl.timeline.factories.event.TimelineItemContentStateFactory
import id.databisnis.android.bachot.features.messages.impl.timeline.factories.event.TimelineItemContentStickerFactory
import id.databisnis.android.bachot.features.messages.impl.timeline.factories.event.TimelineItemContentUTDFactory
import id.databisnis.android.bachot.features.messages.impl.timeline.factories.event.TimelineItemEventFactory
import id.databisnis.android.bachot.features.messages.impl.timeline.factories.virtual.TimelineItemDaySeparatorFactory
import id.databisnis.android.bachot.features.messages.impl.timeline.factories.virtual.TimelineItemVirtualFactory
import id.databisnis.android.bachot.features.messages.impl.timeline.groups.TimelineItemGrouper
import id.databisnis.android.bachot.features.messages.impl.utils.FakeTextPillificationHelper
import id.databisnis.android.bachot.features.messages.test.timeline.FakeHtmlConverterProvider
import id.databisnis.android.bachot.features.poll.test.pollcontent.FakePollContentStateFactory
import id.databisnis.android.bachot.libraries.androidutils.filesize.FakeFileSizeFormatter
import id.databisnis.android.bachot.libraries.dateformatter.test.FakeDateFormatter
import id.databisnis.android.bachot.libraries.eventformatter.api.TimelineEventFormatter
import id.databisnis.android.bachot.libraries.matrix.api.core.UserId
import id.databisnis.android.bachot.libraries.matrix.api.timeline.item.event.EventContent
import id.databisnis.android.bachot.libraries.matrix.test.FakeMatrixClient
import id.databisnis.android.bachot.libraries.matrix.test.permalink.FakePermalinkParser
import id.databisnis.android.bachot.libraries.mediaviewer.test.util.FileExtensionExtractorWithoutValidation
import id.databisnis.android.bachot.tests.testutils.testCoroutineDispatchers
import kotlinx.coroutines.test.TestScope

internal fun TestScope.aTimelineItemsFactoryCreator(): TimelineItemsFactory.Creator {
    return object : TimelineItemsFactory.Creator {
        override fun create(config: TimelineItemsFactoryConfig): TimelineItemsFactory {
            return aTimelineItemsFactory(config)
        }
    }
}

internal fun TestScope.aTimelineItemsFactory(
    config: TimelineItemsFactoryConfig,
): TimelineItemsFactory {
    val timelineEventFormatter = aTimelineEventFormatter()
    val matrixClient = FakeMatrixClient()
    return TimelineItemsFactory(
        dispatchers = testCoroutineDispatchers(),
        eventItemFactoryCreator = object : TimelineItemEventFactory.Creator {
            override fun create(config: TimelineItemsFactoryConfig): TimelineItemEventFactory {
                return TimelineItemEventFactory(
                    contentFactory = TimelineItemContentFactory(
                        messageFactory = TimelineItemContentMessageFactory(
                            fileSizeFormatter = FakeFileSizeFormatter(),
                            fileExtensionExtractor = FileExtensionExtractorWithoutValidation(),
                            htmlConverterProvider = FakeHtmlConverterProvider(),
                            permalinkParser = FakePermalinkParser(),
                            textPillificationHelper = FakeTextPillificationHelper(),
                        ),
                        redactedMessageFactory = TimelineItemContentRedactedFactory(),
                        stickerFactory = TimelineItemContentStickerFactory(
                            fileSizeFormatter = FakeFileSizeFormatter(),
                            fileExtensionExtractor = FileExtensionExtractorWithoutValidation()
                        ),
                        pollFactory = TimelineItemContentPollFactory(FakePollContentStateFactory()),
                        utdFactory = TimelineItemContentUTDFactory(),
                        roomMembershipFactory = TimelineItemContentRoomMembershipFactory(timelineEventFormatter),
                        profileChangeFactory = TimelineItemContentProfileChangeFactory(timelineEventFormatter),
                        stateFactory = TimelineItemContentStateFactory(timelineEventFormatter),
                        failedToParseMessageFactory = TimelineItemContentFailedToParseMessageFactory(),
                        failedToParseStateFactory = TimelineItemContentFailedToParseStateFactory(),
                        sessionId = matrixClient.sessionId,
                    ),
                    matrixClient = matrixClient,
                    dateFormatter = FakeDateFormatter(),
                    permalinkParser = FakePermalinkParser(),
                    config = config,
                    summaryFormatter = FakeMessageSummaryFormatter(),
                )
            }
        },
        virtualItemFactory = TimelineItemVirtualFactory(
            daySeparatorFactory = TimelineItemDaySeparatorFactory(
                FakeDateFormatter()
            ),
        ),
        timelineItemGrouper = TimelineItemGrouper(),
        config = config
    )
}

internal fun aTimelineEventFormatter(): TimelineEventFormatter {
    return object : TimelineEventFormatter {
        override fun format(content: EventContent, isOutgoing: Boolean, sender: UserId, senderDisambiguatedDisplayName: String): CharSequence? {
            return ""
        }
    }
}
