package com.tt.honeyDue.ui.screens

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

/**
 * Page object for the registration screen.
 * Mirrors `iosApp/HoneyDueUITests/PageObjects/RegisterScreen.swift`.
 */
class RegisterScreen(rule: ComposeTestRule) : BaseScreen(rule) {

    fun enterUsername(value: String): RegisterScreen {
        waitFor(AccessibilityIds.Authentication.registerUsernameField)
        tag(AccessibilityIds.Authentication.registerUsernameField).performTextInput(value)
        return this
    }

    fun enterEmail(value: String): RegisterScreen {
        waitFor(AccessibilityIds.Authentication.registerEmailField)
        tag(AccessibilityIds.Authentication.registerEmailField).performTextInput(value)
        return this
    }

    fun enterPassword(value: String): RegisterScreen {
        waitFor(AccessibilityIds.Authentication.registerPasswordField)
        tag(AccessibilityIds.Authentication.registerPasswordField).performTextInput(value)
        return this
    }

    fun enterConfirmPassword(value: String): RegisterScreen {
        waitFor(AccessibilityIds.Authentication.registerConfirmPasswordField)
        tag(AccessibilityIds.Authentication.registerConfirmPasswordField).performTextInput(value)
        return this
    }

    fun tapRegister(): MainTabScreen {
        waitFor(AccessibilityIds.Authentication.registerButton)
        tag(AccessibilityIds.Authentication.registerButton).performClick()
        return MainTabScreen(rule)
    }

    fun tapCancel(): LoginScreen {
        waitFor(AccessibilityIds.Authentication.registerCancelButton)
        tag(AccessibilityIds.Authentication.registerCancelButton).performClick()
        return LoginScreen(rule)
    }

    /** Convenience: fill out the form and submit. */
    fun register(username: String, email: String, password: String): MainTabScreen {
        enterUsername(username)
        enterEmail(email)
        enterPassword(password)
        enterConfirmPassword(password)
        return tapRegister()
    }

    override fun isDisplayed(): Boolean =
        exists(AccessibilityIds.Authentication.registerUsernameField) ||
            exists(AccessibilityIds.Authentication.registerEmailField)
}
