/*
 * Copyright 2024 New Vector Ltd.
 *
 * SPDX-License-Identifier: AGPL-3.0-only
 * Please see LICENSE 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.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.api.anImageMediaInfo
import io.element.android.libraries.mediaviewer.api.local.LocalMedia
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 org.junit.Rule
import org.junit.Test
import org.junit.rules.TestRule
import org.junit.runner.RunWith

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

    @Test
    fun `clicking on back invokes expected callback`() {
        val eventsRecorder = EventsRecorder<MediaViewerEvents>(expectEvents = false)
        ensureCalledOnce { callback ->
            rule.setMediaViewerView(
                aMediaViewerState(
                    eventSink = eventsRecorder
                ),
                onBackClick = callback,
            )
            rule.pressBack()
        }
    }

    @Test
    fun `clicking on open emit expected Event`() {
        testMenuAction(CommonStrings.action_open_with, MediaViewerEvents.OpenWith)
    }

    @Test
    fun `clicking on save emit expected Event`() {
        testMenuAction(CommonStrings.action_save, MediaViewerEvents.SaveOnDisk)
    }

    @Test
    fun `clicking on share emit expected Event`() {
        testMenuAction(CommonStrings.action_share, MediaViewerEvents.Share)
    }

    private fun testMenuAction(contentDescriptionRes: Int, expectedEvent: MediaViewerEvents) {
        val eventsRecorder = EventsRecorder<MediaViewerEvents>()
        rule.setMediaViewerView(
            aMediaViewerState(
                downloadedMedia = AsyncData.Success(
                    LocalMedia(Uri.EMPTY, anImageMediaInfo())
                ),
                mediaInfo = anImageMediaInfo(),
                eventSink = eventsRecorder
            ),
        )
        val contentDescription = rule.activity.getString(contentDescriptionRes)
        rule.onNodeWithContentDescription(contentDescription).performClick()
        eventsRecorder.assertSingle(expectedEvent)
    }

    @Test
    fun `clicking on image hides the overlay`() {
        val eventsRecorder = EventsRecorder<MediaViewerEvents>(expectEvents = false)
        rule.setMediaViewerView(
            aMediaViewerState(
                downloadedMedia = AsyncData.Success(
                    LocalMedia(Uri.EMPTY, anImageMediaInfo())
                ),
                mediaInfo = anImageMediaInfo(),
                eventSink = eventsRecorder
            ),
        )
        // Ensure that the action are visible
        val contentDescription = rule.activity.getString(CommonStrings.action_open_with)
        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()
    }

    @Test
    fun `clicking swipe on the image invokes the expected callback`() {
        val eventsRecorder = EventsRecorder<MediaViewerEvents>(expectEvents = false)
        ensureCalledOnce { callback ->
            rule.setMediaViewerView(
                aMediaViewerState(
                    downloadedMedia = AsyncData.Success(
                        LocalMedia(Uri.EMPTY, anImageMediaInfo())
                    ),
                    mediaInfo = anImageMediaInfo(),
                    eventSink = eventsRecorder
                ),
                onBackClick = callback,
            )
            val imageContentDescription = rule.activity.getString(CommonStrings.common_image)
            rule.onNodeWithContentDescription(imageContentDescription).performTouchInput { swipeDown(startY = centerY) }
            rule.mainClock.advanceTimeBy(1_000)
        }
    }

    @Test
    fun `error case, click on retry emits the expected Event`() {
        val eventsRecorder = EventsRecorder<MediaViewerEvents>()
        rule.setMediaViewerView(
            aMediaViewerState(
                downloadedMedia = AsyncData.Failure(IllegalStateException("error")),
                mediaInfo = anImageMediaInfo(),
                eventSink = eventsRecorder
            ),
        )
        rule.clickOn(CommonStrings.action_retry)
        eventsRecorder.assertSingle(MediaViewerEvents.RetryLoading)
    }

    @Test
    fun `error case, click on cancel emits the expected Event`() {
        val eventsRecorder = EventsRecorder<MediaViewerEvents>()
        rule.setMediaViewerView(
            aMediaViewerState(
                downloadedMedia = AsyncData.Failure(IllegalStateException("error")),
                mediaInfo = anImageMediaInfo(),
                eventSink = eventsRecorder
            ),
        )
        rule.clickOn(CommonStrings.action_cancel)
        eventsRecorder.assertSingle(MediaViewerEvents.ClearLoadingError)
    }
}

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