//
//  TimerIdentifierAppEntity.swift
//  LeCountdown
//
//  Created by Laurent Morvillier on 07/03/2023.
//

import Foundation
import AppIntents

@available(iOS 16.0, macOS 13.0, watchOS 9.0, tvOS 16.0, *)
struct TimerIdentifierAppEntity: AppEntity {
    static var typeDisplayRepresentation = TypeDisplayRepresentation(name: "Timer Identifier")

    struct TimerIdentifierAppEntityQuery: EntityQuery {
        func entities(for identifiers: [TimerIdentifierAppEntity.ID]) async throws -> [TimerIdentifierAppEntity] {
            
            let timers = identifiers.compactMap { IntentDataProvider.main.timer(id: $0) }
            return timers.map { TimerIdentifierAppEntity(id: $0.stringId, displayString: $0.displayName) }
            // TODO: return TimerIdentifierAppEntity entities with the specified identifiers here.
        }

        func suggestedEntities() async throws -> [TimerIdentifierAppEntity] {
            // TODO: return likely TimerIdentifierAppEntity entities here.
            // This method is optional; the default implementation returns an empty array.
            let timers = try IntentDataProvider.main.timers()
            return timers.map { TimerIdentifierAppEntity(id: $0.stringId, displayString: $0.displayName) }
        }
    }
    
    static var defaultQuery = TimerIdentifierAppEntityQuery()

    var id: String // if your identifier is not a String, conform the entity to EntityIdentifierConvertible.
    var displayString: String
    var displayRepresentation: DisplayRepresentation {
        DisplayRepresentation(title: "\(displayString)")
    }

    init(id: String, displayString: String) {
        self.id = id
        self.displayString = displayString
    }
}

