import AppKit
import Foundation

public protocol SpaceEnumeratorProtocol {
    func displays() throws -> [AlignerDisplay]
}

public protocol SpaceEnumeratorDelegate: AnyObject {
    func spaceDidChange(to space: AlignerSpace, on display: AlignerDisplay)
    func displayConfigurationDidChange()
}

public protocol WindowServiceProtocol: AnyObject {
    var delegate: WindowServiceDelegate? { get set }

    func allWindows() throws -> [AlignerWindow]
    func windows(for space: AlignerSpace) throws -> [AlignerWindow]
    func refresh()
    func startObserving()
    func stopObserving()
}

public enum WindowChangeKind: Equatable, Sendable {
    case opened
    case closed
    case moved
    case resized
    case minimized
    case deminiaturized
    case hidden
    case unhidden
}

public struct WindowChange: Equatable, Sendable {
    public let kind: WindowChangeKind
    public let windowID: UInt32

    public init(kind: WindowChangeKind, windowID: UInt32) {
        self.kind = kind
        self.windowID = windowID
    }
}

public protocol WindowServiceDelegate: AnyObject {
    func windowServiceDidDetectWindowChange(_ change: WindowChange)
}

public enum WindowRestoreResult: Equatable, Sendable {
    case restored
    case windowNotFound
    case unsupported
    case activationFailed
}

public protocol WindowRestorationServiceProtocol: AnyObject {
    func restore(windowID: UInt32) throws -> WindowRestoreResult
}

public enum WindowActivationResult: Equatable, Sendable {
    case activated
    case restored
    case appActivatedOnly
    case windowNotFound
    case unsupported
    case activationFailed
    case failed(String)
}

public protocol WindowActivationServiceProtocol: AnyObject {
    func activate(window: AlignerWindow) throws -> WindowActivationResult
}

@MainActor
public protocol ScreenshotProviderProtocol {
    func screenshot(for window: AlignerWindow) async throws -> NSImage
    func resolvedScreenshot(for window: AlignerWindow, in session: ScreenshotCaptureSession) async -> ScreenshotResolution
}

@MainActor
public protocol SkeletonThumbnailProviderProtocol {
    func skeleton(for app: AlignerApp) -> NSImage
    func withOverlayTitle(_ title: String, for app: AlignerApp) -> NSImage
}

@MainActor
public protocol AppIconProviderProtocol {
    func icon(for app: AlignerApp) -> NSImage
    func fallbackIcon() -> NSImage
}
