/*
 * Copyright (c) 2025 Element Creations Ltd.
 * Copyright 2024, 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 io.element.android.libraries.mediaviewer.impl.viewer

import android.net.Uri
import androidx.activity.ComponentActivity
import androidx.annotation.StringRes
import androidx.compose.ui.test.assertHasClickAction
import androidx.compose.ui.test.junit4.AndroidComposeTestRule
import androidx.compose.ui.test.junit4.createAndroidComposeRule
import androidx.compose.ui.test.onNodeWithContentDescription
import androidx.compose.ui.test.performClick
import androidx.compose.ui.test.performTouchInput
import androidx.compose.ui.test.swipeDown
import androidx.test.ext.junit.runners.AndroidJUnit4
import io.element.android.libraries.architecture.AsyncData
import io.element.android.libraries.mediaviewer.impl.details.aMediaDetailsBottomSheetState
import io.element.android.libraries.mediaviewer.test.viewer.aLocalMedia
import io.element.android.libraries.ui.strings.CommonStrings
import io.element.android.tests.testutils.EnsureNeverCalled
import io.element.android.tests.testutils.EventsRecorder
import io.element.android.tests.testutils.clickOn
import io.element.android.tests.testutils.ensureCalledOnce
import io.element.android.tests.testutils.pressBack
import io.element.android.tests.testutils.setSafeContent
import io.mockk.mockk
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TestRule
import org.junit.runner.RunWith
import org.robolectric.annotation.Config

@RunWith(AndroidJUnit4::class)
class MediaViewerViewTest {
    @get:Rule val rule = createAndroidComposeRule<ComponentActivity>()

    private val mockMediaUrl: Uri = mockk("localMediaUri")

    @Test
    fun `clicking on back invokes expected callback`() {
        val eventsRecorder = EventsRecorder<MediaViewerEvent>()
        val state = aMediaViewerState(
            eventSink = eventsRecorder
        )
        ensureCalledOnce { callback ->
            rule.setMediaViewerView(
                state = state,
                onBackClick = callback,
            )
            rule.pressBack()
        }
        eventsRecorder.assertList(
            listOf(
                MediaViewerEvent.OnNavigateTo(0),
                MediaViewerEvent.LoadMedia(state.listData.first() as MediaViewerPageData.MediaViewerData),
            )
        )
    }

    @Test
    fun `clicking on info emits expected Event`() {
        val data = aMediaViewerPageData(
            downloadedMedia = AsyncData.Success(aLocalMedia(uri = mockMediaUrl)),
        )
        testMenuAction(
            data,
            CommonStrings.a11y_view_details,
            MediaViewerEvent.OpenInfo(data),
        )
    }

    @Test
    fun `clicking on top action share emits expected Event`() {
        val data = aMediaViewerPageData(
            downloadedMedia = AsyncData.Success(aLocalMedia(uri = mockMediaUrl)),
        )
        testMenuAction(
            data,
            CommonStrings.action_share,
            MediaViewerEvent.Share(data),
        )
    }

    @Test
    fun `clicking on top action download emits expected Event`() {
        val data = aMediaViewerPageData(
            downloadedMedia = AsyncData.Success(aLocalMedia(uri = mockMediaUrl)),
        )
        testMenuAction(
            data,
            CommonStrings.action_download,
            MediaViewerEvent.SaveOnDisk(data),
        )
    }

    private fun testMenuAction(
        data: MediaViewerPageData.MediaViewerData,
        @StringRes contentDescriptionRes: Int,
        expectedEvent: MediaViewerEvent,
    ) {
        val eventsRecorder = EventsRecorder<MediaViewerEvent>()
        rule.setMediaViewerView(
            aMediaViewerState(
                listData = listOf(data),
                eventSink = eventsRecorder
            ),
        )
        val contentDescription = rule.activity.getString(contentDescriptionRes)
        rule.onNodeWithContentDescription(contentDescription).performClick()
        eventsRecorder.assertList(
            listOf(
                MediaViewerEvent.OnNavigateTo(0),
                MediaViewerEvent.LoadMedia(data),
                expectedEvent,
            )
        )
    }

    @Test
    @Config(qualifiers = "h1024dp")
    fun `clicking on download emits expected Event`() {
        val data = aMediaViewerPageData()
        testBottomSheetAction(
            data,
            CommonStrings.action_download,
            MediaViewerEvent.SaveOnDisk(data),
        )
    }

    @Test
    @Config(qualifiers = "h1024dp")
    fun `clicking on share emits expected Event`() {
        val data = aMediaViewerPageData()
        testBottomSheetAction(
            data,
            CommonStrings.action_share,
            MediaViewerEvent.Share(data),
        )
    }

    @Config(qualifiers = "h1024dp")
    @Test
    fun `clicking on open in emits expected Event`() {
        val data = aMediaViewerPageData()
        testBottomSheetAction(
            data,
            CommonStrings.action_open_with,
            MediaViewerEvent.OpenWith(data),
        )
    }

    private fun testBottomSheetAction(
        data: MediaViewerPageData.MediaViewerData,
        @StringRes textRes: Int,
        expectedEvent: MediaViewerEvent,
    ) {
        val eventsRecorder = EventsRecorder<MediaViewerEvent>()
        rule.setMediaViewerView(
            aMediaViewerState(
                listData = listOf(data),
                mediaBottomSheetState = aMediaDetailsBottomSheetState(),
                eventSink = eventsRecorder
            ),
        )
        rule.clickOn(textRes)
        eventsRecorder.assertList(
            listOf(
                MediaViewerEvent.OnNavigateTo(0),
                MediaViewerEvent.LoadMedia(data),
                expectedEvent,
            )
        )
    }

    @Test
    fun `clicking on image hides the overlay`() {
        val eventsRecorder = EventsRecorder<MediaViewerEvent>()
        val state = aMediaViewerState(
            eventSink = eventsRecorder
        )
        rule.setMediaViewerView(
            state = state,
        )
        // Ensure that the action are visible
        val contentDescription = rule.activity.getString(CommonStrings.action_share)
        rule.onNodeWithContentDescription(contentDescription)
            .assertExists()
            .assertHasClickAction()
        val imageContentDescription = rule.activity.getString(CommonStrings.common_image)
        rule.onNodeWithContentDescription(imageContentDescription).performClick()
        // Give time for the animation (? since even by removing AnimatedVisibility it still fails)
        rule.mainClock.advanceTimeBy(1_000)
        rule.onNodeWithContentDescription(contentDescription)
            .assertDoesNotExist()
        eventsRecorder.assertList(
            listOf(
                MediaViewerEvent.OnNavigateTo(0),
                MediaViewerEvent.LoadMedia(state.listData.first() as MediaViewerPageData.MediaViewerData),
            )
        )
    }

    @Test
    fun `clicking swipe on the image invokes the expected callback`() {
        val eventsRecorder = EventsRecorder<MediaViewerEvent>()
        val state = aMediaViewerState(
            eventSink = eventsRecorder
        )
        ensureCalledOnce { callback ->
            rule.setMediaViewerView(
                state = state,
                onBackClick = callback,
            )
            val imageContentDescription = rule.activity.getString(CommonStrings.common_image)
            rule.onNodeWithContentDescription(imageContentDescription).performTouchInput { swipeDown(startY = centerY) }
            rule.mainClock.advanceTimeBy(1_000)
        }
        eventsRecorder.assertList(
            listOf(
                MediaViewerEvent.OnNavigateTo(0),
                MediaViewerEvent.LoadMedia(state.listData.first() as MediaViewerPageData.MediaViewerData),
            )
        )
    }

    @Test
    fun `error case, click on retry emits the expected Event`() {
        val eventsRecorder = EventsRecorder<MediaViewerEvent>()
        val data = aMediaViewerPageData(
            downloadedMedia = AsyncData.Failure(IllegalStateException("error")),
        )
        rule.setMediaViewerView(
            aMediaViewerState(
                listData = listOf(data),
                eventSink = eventsRecorder
            ),
        )
        rule.clickOn(CommonStrings.action_retry)
        eventsRecorder.assertList(
            listOf(
                MediaViewerEvent.OnNavigateTo(0),
                MediaViewerEvent.LoadMedia(data),
                MediaViewerEvent.LoadMedia(data),
            )
        )
    }

    @Test
    fun `error case, click on cancel emits the expected Event`() {
        val eventsRecorder = EventsRecorder<MediaViewerEvent>()
        val data = aMediaViewerPageData(
            downloadedMedia = AsyncData.Failure(IllegalStateException("error")),
        )
        rule.setMediaViewerView(
            aMediaViewerState(
                listData = listOf(data),
                eventSink = eventsRecorder
            ),
        )
        rule.clickOn(CommonStrings.action_cancel)
        eventsRecorder.assertList(
            listOf(
                MediaViewerEvent.OnNavigateTo(0),
                MediaViewerEvent.LoadMedia(data),
                MediaViewerEvent.ClearLoadingError(data)
            )
        )
    }
}

private fun <R : TestRule> AndroidComposeTestRule<R, ComponentActivity>.setMediaViewerView(
    state: MediaViewerState,
    onBackClick: () -> Unit = EnsureNeverCalled(),
) {
    setSafeContent {
        MediaViewerView(
            state = state,
            audioFocus = null,
            textFileViewer = { _, _ -> },
            onBackClick = onBackClick,
        )
    }
}
