import XCTest

final class CommandNotchUITests: XCTestCase {
    override func setUpWithError() throws {
        continueAfterFailure = false
    }

    func testSettingsWorkspaceRenameFlow() {
        let app = launchIntoSettings()

        let newWorkspaceButton = app.buttons["settings.workspaces.new"]
        XCTAssertTrue(newWorkspaceButton.waitForExistence(timeout: 5))
        newWorkspaceButton.tap()

        let nameField = app.textFields["settings.workspaces.name-field"]
        XCTAssertTrue(nameField.waitForExistence(timeout: 5))
        nameField.tap()
        nameField.typeKey("a", modifierFlags: .command)
        nameField.typeText(XCUIKeyboardKey.delete.rawValue)
        nameField.typeText("Docs")

        let saveButton = app.buttons["settings.workspaces.save-name"]
        XCTAssertTrue(saveButton.exists)
        saveButton.tap()

        XCTAssertTrue(app.staticTexts["Docs"].waitForExistence(timeout: 5))
    }

    func testSettingsWorkspaceDeleteFallsBackToMain() {
        let app = launchIntoSettings()

        let newWorkspaceButton = app.buttons["settings.workspaces.new"]
        XCTAssertTrue(newWorkspaceButton.waitForExistence(timeout: 5))
        newWorkspaceButton.tap()

        let nameField = app.textFields["settings.workspaces.name-field"]
        XCTAssertTrue(nameField.waitForExistence(timeout: 5))
        nameField.tap()
        nameField.typeKey("a", modifierFlags: .command)
        nameField.typeText(XCUIKeyboardKey.delete.rawValue)
        nameField.typeText("Scratch")

        let saveButton = app.buttons["settings.workspaces.save-name"]
        XCTAssertTrue(saveButton.exists)
        saveButton.tap()
        XCTAssertTrue(app.staticTexts["Scratch"].waitForExistence(timeout: 5))

        let assignCurrentButton = app.buttons["settings.workspaces.assign-current"]
        XCTAssertTrue(assignCurrentButton.waitForExistence(timeout: 5))
        assignCurrentButton.tap()

        let deleteButton = app.buttons["settings.workspaces.delete"]
        XCTAssertTrue(deleteButton.waitForExistence(timeout: 5))
        deleteButton.tap()

        let confirmDeleteButton = app.sheets.buttons["Delete"]
        XCTAssertTrue(confirmDeleteButton.waitForExistence(timeout: 5))
        confirmDeleteButton.tap()

        let mainValuePredicate = NSPredicate(format: "value == %@", "Main")
        expectation(for: mainValuePredicate, evaluatedWith: nameField)
        waitForExpectations(timeout: 5)
    }

    func testOpenNotchLaunchShowsInteractiveControls() {
        let app = XCUIApplication()
        app.launchArguments = [
            "--uitest-regular-activation",
            "--uitest-open-notch"
        ]
        app.launch()

        let notch = app.descendants(matching: .any)["notch.container"]
        XCTAssertTrue(notch.waitForExistence(timeout: 5))

        let newTabButton = app.buttons["New Tab"]
        XCTAssertTrue(newTabButton.waitForExistence(timeout: 5))

        let settingsButton = app.buttons["Settings"]
        XCTAssertTrue(settingsButton.waitForExistence(timeout: 5))
        settingsButton.tap()

        XCTAssertTrue(app.windows["CommandNotch Settings"].waitForExistence(timeout: 5))
    }

    @discardableResult
    private func launchIntoSettings() -> XCUIApplication {
        let app = XCUIApplication()
        app.launchArguments = [
            "--uitest-regular-activation",
            "--uitest-show-settings"
        ]
        app.launch()

        let settingsWindow = app.windows["CommandNotch Settings"]
        XCTAssertTrue(settingsWindow.waitForExistence(timeout: 5))

        let workspacesTab = app.descendants(matching: .any)["settings.tab.workspaces"]
        XCTAssertTrue(workspacesTab.waitForExistence(timeout: 5))
        workspacesTab.tap()
        return app
    }
}
