#if os(watchOS)
import XCTest



extension RunnerTests {


  static let watchCrownRotationsPerViewport = 0.5

  static let watchInputSheetTimeout: TimeInterval = 3.0

  static let watchInputSheetDoneLabel = "Done"

  static let watchBackConfirmationTimeout: TimeInterval = 1.0


  static let watchCrownSettleInterval: TimeInterval = 0.35



  @MainActor
  func watchCrownScroll(app: XCUIApplication, command: Command, direction: RunnerScrollDirection) throws -> Response {
    guard direction.isVertical else {
      return Response(ok: false, error: ErrorPayload(
        code: "UNSUPPORTED_OPERATION",
        message: "scroll \(direction.rawValue) is not supported on watchOS",
        hint: "The Digital Crown scrolls vertically. Use swipe for horizontal pages."
      ))
    }
    let viewportFraction: Double
    if let pixels = command.pixels {
      let height = try resolvedTouchReferenceFrame(app: app).height
      guard height > 0 else {
        return Response(ok: false, error: ErrorPayload(message: "scroll could not resolve a usable interaction frame"))
      }
      viewportFraction = pixels / Double(height)
    } else if let amount = runnerDragCommandDefaults(command).scrollAmount {
      viewportFraction = amount
    } else {
      return Response(ok: false, error: ErrorPayload(code: "INVALID_ARGS", message: "scroll requires an amount or pixels"))
    }
    let delta = viewportFraction * Self.watchCrownRotationsPerViewport
    let timing = measureGesture {
      withBoundedInteractionIdleTimeoutIfSupported(app, waits: .bothSkipped) {
        XCUIDevice.shared.rotateDigitalCrown(delta: direction == .down ? delta : -delta)
      }
    }
    RunLoop.current.run(until: Date().addingTimeInterval(Self.watchCrownSettleInterval))
    return gestureResponse(message: "scrolled", timing: timing)
  }




  @MainActor
  func performWatchBack(app: XCUIApplication) throws -> InAppBackOutcome {

    let buttons = app.buttons.matching(identifier: Self.navigationBarBackButtonIdentifier).allElementsBoundByIndex
    guard buttons.count == 1, let button = buttons.first, button.isHittable else {
      return .refused(ErrorPayload(
        code: RunnerWireErrorCode.backSwipeNotDispatched,
        message: "This screen has no navigation back button.",
        hint: "A root screen has nothing to go back to. Otherwise press the screen's close control by its ref from a fresh snapshot."
      ))
    }
    let before = watchNavigationBarIdentity(app: app)
    let frame = button.frame
    switch try tapAt(app: app, x: frame.midX, y: frame.midY) {
    case .performed:
      break
    case .unsupported(let message, let hint), .notDispatched(let message, let hint):
      return .refused(ErrorPayload(code: RunnerWireErrorCode.backSwipeNotDispatched, message: message, hint: hint))
    }

    let popped = watchPoll(timeout: Self.watchBackConfirmationTimeout) {
      guard let before, let after = watchNavigationBarIdentity(app: app) else { return false }
      return after != before
    }
    if popped { return .performed }
    return .unconfirmed(ErrorPayload(
      code: RunnerWireErrorCode.backSwipeUnverified,
      message: "back pressed the navigation bar back button, but the navigation bar did not change within 1 s. This is an unknown outcome.",
      hint: "Take a snapshot to see where the app is."
    ))
  }




  private func watchNavigationBarIdentity(app: XCUIApplication) -> [String]? {
    guard let root = try? app.snapshot() else { return nil }
    var identity: [String] = []
    func visit(_ node: any XCUIElementSnapshot) {
      if node.elementType == .navigationBar {
        identity.append("bar|\(node.identifier)|\(node.label)")
      } else if node.identifier == Self.navigationBarBackButtonIdentifier {
        identity.append("back")
      }
      node.children.forEach(visit)
    }
    visit(root)
    return identity
  }



  @MainActor
  func watchSampledPlannedGesture(app: XCUIApplication, plan: RunnerGesturePlan) throws -> RunnerInteractionOutcome {
    guard plan.pointers.count == 1 else {
      return .unsupported(
        message: "multi-touch gestures are not supported on watchOS",
        hint: "watchOS apps have no multi-touch gestures. Use swipe, gesture pan, or scroll."
      )
    }
    guard let resolved = resolveRunnerWindowElement(app: app) else {
      return .notDispatched(message: "gesture could not resolve the app window", hint: nil)
    }
    if plan.contactOrigin == runnerScreenEdgeContactOrigin,
      let first = plan.pointers[0].samples.first,
      let last = plan.pointers[0].samples.last
    {


      let edgeX = first.point.x < Double(resolved.frame.midX)
        ? Double(resolved.frame.minX) + 1
        : Double(resolved.frame.maxX) - 1


      var outcome: Result<RunnerInteractionOutcome, Error> = .success(.performed)
      withBoundedInteractionIdleTimeoutIfSupported(app, waits: .bothSkipped) {
        outcome = Result {
          try dragAt(
            app: app,
            x: edgeX,
            y: first.point.y,
            x2: last.point.x,
            y2: last.point.y,
            holdDuration: coordinateDragHoldDuration()
          )
        }
      }
      return try outcome.get()
    }
    let pointerSamples: [[[String: NSNumber]]] = plan.pointers.map { pointer in
      pointer.samples.map { sample in
        [
          "x": NSNumber(value: sample.point.x),
          "y": NSNumber(value: sample.point.y),
          "offsetMs": NSNumber(value: sample.offsetMs),
        ]
      }
    }
    return try dispatchSynthesizedPointerSamples(
      app: app, window: resolved.window, orientation: 1, pointerSamples: pointerSamples
    )
  }



  @MainActor
  func watchTypeCommandResponse(activeApp: XCUIApplication, command: Command, text: String) throws -> Response {
    var point: CGPoint?
    if let x = command.x, let y = command.y {
      switch try tapAt(app: activeApp, x: x, y: y) {
      case .performed:
        break
      case .unsupported(let message, let hint), .notDispatched(let message, let hint):
        return Response(ok: false, error: ErrorPayload(message: message, hint: hint))
      }
      point = CGPoint(x: x, y: y)
    }
    guard watchPoll(timeout: Self.watchInputSheetTimeout, until: { activeApp.keyboards.firstMatch.exists }) else {
      let failure = TextEntryFailure.notFocused
      return Response(ok: false, error: ErrorPayload(
        code: failure.rawValue,
        message: failure.message,
        hint: "Press the text field first. On watchOS the field opens a system input sheet, and the text goes there."
      ))
    }
    withBoundedInteractionIdleTimeoutIfSupported(activeApp, waits: .bothSkipped) {
      let sheetText = activeApp.textViews.firstMatch
      if resolveTextEntryMode(command) == .replacement, sheetText.exists {
        let existing = (sheetText.value as? String) ?? ""
        if !existing.isEmpty {
          activeApp.typeText(String(repeating: XCUIKeyboardKey.delete.rawValue, count: existing.count))
        }
      }
      activeApp.typeText(text)
    }
    let done = activeApp.buttons[Self.watchInputSheetDoneLabel]
    guard done.exists else {
      return Response(ok: false, error: ErrorPayload(
        code: TextEntryFailure.commitNotObserved.rawValue,
        message: "the watchOS input sheet has no Done button to commit the text",
        hint: "The text is typed into the open input sheet. Press its commit button by ref."
      ))
    }
    withBoundedInteractionIdleTimeoutIfSupported(activeApp, waits: .bothSkipped) {
      done.tap()
    }
    guard watchPoll(timeout: Self.watchInputSheetTimeout, until: { !activeApp.keyboards.firstMatch.exists }) else {
      return Response(ok: false, error: ErrorPayload(
        code: TextEntryFailure.commitNotObserved.rawValue,
        message: "the watchOS input sheet stayed open after Done",
        hint: "Check the screen with snapshot; the text may still be in the open input sheet."
      ))
    }
    return Response(ok: true, data: DataPayload(
      message: "typed",
      x: point.map { Double($0.x) },
      y: point.map { Double($0.y) },
      textEntryRoute: "watch-input-sheet"
    ))
  }


  private func watchPoll(timeout: TimeInterval, until condition: () -> Bool) -> Bool {
    let deadline = Date().addingTimeInterval(timeout)
    repeat {
      if condition() { return true }
      RunLoop.current.run(until: Date().addingTimeInterval(0.1))
    } while Date() < deadline
    return false
  }
}
#endif
