package com.tt.honeyDue.ui.screens

import androidx.compose.ui.test.junit4.ComposeTestRule
import androidx.compose.ui.test.performClick
import com.tt.honeyDue.testing.AccessibilityIds
import com.tt.honeyDue.ui.BaseScreen

/**
 * Page object for the main tab scaffold visible after login.
 * Mirrors `iosApp/HoneyDueUITests/PageObjects/MainTabScreen.swift`.
 */
class MainTabScreen(rule: ComposeTestRule) : BaseScreen(rule) {

    fun tapResidencesTab(): MainTabScreen {
        waitFor(AccessibilityIds.Navigation.residencesTab)
        tag(AccessibilityIds.Navigation.residencesTab).performClick()
        return this
    }

    fun tapTasksTab(): MainTabScreen {
        waitFor(AccessibilityIds.Navigation.tasksTab)
        tag(AccessibilityIds.Navigation.tasksTab).performClick()
        return this
    }

    fun tapContractorsTab(): MainTabScreen {
        waitFor(AccessibilityIds.Navigation.contractorsTab)
        tag(AccessibilityIds.Navigation.contractorsTab).performClick()
        return this
    }

    fun tapDocumentsTab(): MainTabScreen {
        waitFor(AccessibilityIds.Navigation.documentsTab)
        tag(AccessibilityIds.Navigation.documentsTab).performClick()
        return this
    }

    fun tapProfileTab(): MainTabScreen {
        waitFor(AccessibilityIds.Navigation.profileTab)
        tag(AccessibilityIds.Navigation.profileTab).performClick()
        return this
    }

    /** Opens the settings/profile sheet via the settings affordance. */
    fun goToSettings(): MainTabScreen {
        tapResidencesTab()
        waitFor(AccessibilityIds.Navigation.settingsButton)
        tag(AccessibilityIds.Navigation.settingsButton).performClick()
        return this
    }

    /**
     * Taps logout from the profile sheet. Caller is responsible for waiting
     * on the logout confirmation dialog if the app shows one.
     */
    fun tapLogout(): MainTabScreen {
        waitFor(AccessibilityIds.Profile.logoutButton)
        tag(AccessibilityIds.Profile.logoutButton).performClick()
        return this
    }

    override fun isDisplayed(): Boolean = exists(AccessibilityIds.Navigation.residencesTab)
}
