//
//  NoteEditorScreen.swift
//  Tests iOS
//
//  Screen object for the Journal Note editor sheet.
//

import XCTest

struct NoteEditorScreen {
    let app: XCUIApplication

    // MARK: - Elements

    var navigationTitle: XCUIElement { app.navigationBars.firstMatch }
    var textEditor: XCUIElement { app.textViews[UITestID.NoteEditor.text] }
    var saveButton: XCUIElement { app.buttons[UITestID.NoteEditor.save] }
    var cancelButton: XCUIElement { app.buttons[UITestID.NoteEditor.cancel] }

    // MARK: - Actions

    func typeNote(_ text: String) {
        textEditor.tapWhenReady()
        textEditor.typeText(text)
    }

    func clearAndTypeNote(_ text: String) {
        textEditor.tapWhenReady()
        // Select all and replace
        textEditor.press(forDuration: 1.0)
        let selectAll = app.menuItems["Select All"]
        if selectAll.waitForExistence(timeout: 2) {
            selectAll.tap()
        }
        textEditor.typeText(text)
    }

    func save() {
        saveButton.tapWhenReady()
    }

    func cancel() {
        cancelButton.tapWhenReady()
    }

    // MARK: - Assertions

    func assertVisible(file: StaticString = #file, line: UInt = #line) {
        XCTAssertTrue(
            textEditor.waitForExistence(timeout: 5),
            "Note editor should be visible",
            file: file, line: line
        )
    }

    func assertDismissed(file: StaticString = #file, line: UInt = #line) {
        XCTAssertTrue(
            textEditor.waitForDisappearance(timeout: 5),
            "Note editor should be dismissed",
            file: file, line: line
        )
    }
}
