import Foundation




struct RunnerLaunchConfirmationPrompt {

  struct Wording {
    let titleFormat: String
    let open: String
    let cancel: String
  }

  static let unavailable = RunnerLaunchConfirmationPrompt(wordings: [])

  private static let appNamePlaceholder = try! NSRegularExpression(pattern: "%(?:\\d+\\$)?(?:\\[[^\\]]*\\])?@")

  private let wordings: [(title: NSRegularExpression, open: String, cancel: String)]




  init(wordings: [Wording]) {
    let placeholder = Self.appNamePlaceholder
    self.wordings = wordings.compactMap { wording in
      let format = wording.titleFormat as NSString
      let slots = placeholder.matches(in: wording.titleFormat, range: NSRange(location: 0, length: format.length))
      guard !slots.isEmpty else { return nil }
      var pattern = ""
      var from = 0
      for slot in slots {
        pattern += NSRegularExpression.escapedPattern(for: format.substring(with: NSRange(location: from, length: slot.range.location - from)))
        pattern += ".+"
        from = slot.range.location + slot.range.length
      }
      pattern += NSRegularExpression.escapedPattern(for: format.substring(from: from))
      guard let title = try? NSRegularExpression(pattern: "^\(pattern)$") else { return nil }
      return (title, Self.normalized(wording.open), Self.normalized(wording.cancel))
    }
  }

  func recognizes(title: String, buttons: [String]) -> Bool {
    let title = title.trimmingCharacters(in: .whitespacesAndNewlines)
    let titleRange = NSRange(title.startIndex..., in: title)
    let labels = buttons.map(Self.normalized)
    guard labels.count == 2 else { return false }
    return wordings.contains { wording in
      wording.title.firstMatch(in: title, range: titleRange) != nil
        && Set(labels) == [wording.open, wording.cancel]
    }
  }

  private static func normalized(_ label: String) -> String {
    label.trimmingCharacters(in: .whitespacesAndNewlines)
  }
}

#if os(iOS) && targetEnvironment(simulator)
extension RunnerLaunchConfirmationPrompt {


  static let system = fromSystemStrings(languages: Locale.preferredLanguages)

  static func fromSystemStrings(languages: [String]) -> RunnerLaunchConfirmationPrompt {
    switch RunnerSystemPromptStrings.read([RunnerSystemPromptStrings.launchConfirmationTable], languages: languages) {
    case .failure(let failure):
      RunnerSystemPromptStrings.logUnavailable(failure, subsystem: "launch-confirmation", languages: languages)
      return .unavailable
    case .success(let tables):
      guard let table = tables.first else { return .unavailable }
      let wordings = ([table.english] + table.localized.values).compactMap { strings -> Wording? in
        guard let title = strings["SCHEME_APPROVAL_PROMPT_TITLE_NO_SOURCE"],
          let open = strings["SCHEME_APPROVAL_OPEN_BUTTON"],
          let cancel = strings["SCHEME_APPROVAL_DO_NOT_OPEN_BUTTON"]
        else { return nil }
        return Wording(titleFormat: title, open: open, cancel: cancel)
      }
      return RunnerLaunchConfirmationPrompt(wordings: wordings)
    }
  }
}
#else
extension RunnerLaunchConfirmationPrompt {
  static let system = unavailable
}
#endif
