//
//  CableUITestsScreenshot.swift
//  CableUITestsScreenshot
//
//  Created by Stefan Lange-Hegermann on 06.10.25.
//

import XCTest

final class CableUITestsScreenshot: XCTestCase {
    private let springboard = XCUIApplication(bundleIdentifier: "com.apple.springboard")

    override func setUpWithError() throws {
        continueAfterFailure = false
        try super.setUpWithError()
        ensureDoNotDisturbEnabled()
        dismissSystemOverlays()
    }

    override func tearDownWithError() throws {
        try super.tearDownWithError()
        dismissSystemOverlays()
    }

    @MainActor
    func testExample() throws {
        let app = XCUIApplication()
        app.launch()
        dismissSystemOverlays()
    }

    private func ensureDoNotDisturbEnabled() {
        springboard.activate()
        let pullStart = springboard.coordinate(withNormalizedOffset: CGVector(dx: 0.98, dy: 0.02))
        let pullEnd = springboard.coordinate(withNormalizedOffset: CGVector(dx: 0.98, dy: 0.30))
        pullStart.press(forDuration: 0.1, thenDragTo: pullEnd)

        let focusTile = springboard.otherElements["Focus"]
        let focusButton = springboard.buttons["Focus"]
        if focusTile.waitForExistence(timeout: 2) {
            focusTile.press(forDuration: 1.0)
        } else if focusButton.waitForExistence(timeout: 2) {
            focusButton.press(forDuration: 1.0)
        }

        let dndButton = springboard.buttons["Do Not Disturb"]
        if dndButton.waitForExistence(timeout: 1) {
            if !dndButton.isSelected {
                dndButton.tap()
            }
        } else {
            let dndCell = springboard.cells["Do Not Disturb"]
            if dndCell.waitForExistence(timeout: 1) && !dndCell.isSelected {
                dndCell.tap()
            } else {
                let dndLabel = springboard.staticTexts["Do Not Disturb"]
                if dndLabel.waitForExistence(timeout: 1) {
                    dndLabel.tap()
                }
            }
        }

        let dismissStart = springboard.coordinate(withNormalizedOffset: CGVector(dx: 0.5, dy: 0.85))
        let dismissEnd = springboard.coordinate(withNormalizedOffset: CGVector(dx: 0.5, dy: 0.15))
        dismissStart.press(forDuration: 0.1, thenDragTo: dismissEnd)
    }

    private func dismissSystemOverlays() {
        let app = XCUIApplication()
        let alertButtons = [
            "OK", "Allow", "Later", "Not Now", "Close",
            "Continue", "Remind Me Later", "Maybe Later",
        ]

        if app.alerts.firstMatch.exists {
            handleAlerts(in: app, buttons: alertButtons)
        }

        if springboard.alerts.firstMatch.exists || springboard.scrollViews.firstMatch.exists {
            handleAlerts(in: springboard, buttons: alertButtons + ["Enable Later"])
        }
    }

    private func handleAlerts(in application: XCUIApplication, buttons: [String]) {
        for buttonLabel in buttons {
            let button = application.buttons[buttonLabel]
            if button.waitForExistence(timeout: 0.5) {
                button.tap()
                return
            }
        }
        let closeButton = application.buttons.matching(NSPredicate(format: "identifier CONTAINS[c] %@", "Close")).firstMatch
        if closeButton.exists {
            closeButton.tap()
        }
    }
}
