package com.yorvana.screenshots

import androidx.compose.ui.test.hasClickAction
import androidx.compose.ui.test.hasText
import androidx.compose.ui.test.onNodeWithTag
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performClick
import androidx.compose.ui.test.performScrollTo
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.SdkSuppress
import com.yorvana.R
import com.yorvana.data.preferences.DebugBillingOverrideMode
import com.yorvana.testsupport.awaitTagVisible
import com.yorvana.testsupport.awaitText
import com.yorvana.ui.TestTags
import kotlinx.coroutines.runBlocking
import org.junit.Test
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
@SdkSuppress(minSdkVersion = 34)
class StoreScreenshotTest : StoreScreenshotBase() {
    @Test
    fun screen_01_vehicle_list() {
        composeTestRule.awaitText(DEMO_VEHICLE_NAME)

        captureStoreScreenshot("screen_01_vehicle_list")
    }

    @Test
    fun screen_02_vehicle_detail() {
        openVehicleDetail()
        composeTestRule.awaitText("Coolant flush", substring = true)

        captureStoreScreenshot("screen_02_vehicle_detail")
    }

    @Test
    fun screen_03_add_record() {
        openVehicleDetail()
        composeTestRule.onNodeWithTag(TestTags.ADD_RECORD_FAB).performClick()
        composeTestRule.awaitTagVisible(TestTags.RECORD_ODOMETER_FIELD)

        captureStoreScreenshot("screen_03_add_record")
    }

    @Test
    fun screen_04_categories() {
        openSettings()
        composeTestRule
            .onNodeWithText(composeTestRule.activity.getString(R.string.settings_categories))
            .performScrollTo()
            .performClick()
        composeTestRule.awaitText(composeTestRule.activity.getString(R.string.categories_title))
        composeTestRule.awaitTagVisible(TestTags.ADD_CATEGORY_BUTTON)

        captureStoreScreenshot("screen_04_categories")
    }

    @Test
    fun screen_05_settings_privacy() {
        openSettings()
        // Scroll to the next node after the privacy section to ensure the whole section is visible.
        composeTestRule
            .onNodeWithTag(TestTags.SETTINGS_CONTACT_ROW)
            .performScrollTo()
        composeTestRule.awaitTagVisible(TestTags.SETTINGS_CRASH_REPORTING_TOGGLE)

        captureStoreScreenshot("screen_05_settings_privacy")
    }

    @Test
    fun screen_06_paywall_dialog() {
        setBillingOverride(DebugBillingOverrideMode.FORCE_FREE)
        composeTestRule.onNodeWithTag(TestTags.ADD_VEHICLE_FAB).performClick()
        composeTestRule.awaitTagVisible(TestTags.PAYWALL_INFO_SHEET)
        composeTestRule.awaitText(composeTestRule.activity.getString(R.string.paywall_sheet_headline))

        captureStoreScreenshot("screen_06_paywall_dialog")
    }

    private fun openVehicleDetail() {
        composeTestRule.awaitText(DEMO_VEHICLE_NAME)
        composeTestRule
            .onNode(hasText(DEMO_VEHICLE_NAME) and hasClickAction())
            .performScrollTo()
            .performClick()
        composeTestRule.awaitTagVisible(TestTags.ADD_RECORD_FAB)
    }

    private fun openSettings() {
        composeTestRule.awaitTagVisible(TestTags.SETTINGS_BUTTON)
        composeTestRule.onNodeWithTag(TestTags.SETTINGS_BUTTON).performClick()
        composeTestRule.awaitText(composeTestRule.activity.getString(R.string.settings_title))
    }

    private fun setBillingOverride(mode: DebugBillingOverrideMode) {
        val app = composeTestRule.activity.application as com.yorvana.YorvanaApplication
        runBlocking {
            app.debugBillingOverride?.setMode(mode)
        }
        composeTestRule.waitForIdle()
    }

    private companion object {
        const val DEMO_VEHICLE_NAME = "2019 Honda Civic"
    }
}
