//
//  Scan.swift
//  JustScanIt
//
//  Created by Pradyun Setti on 03/07/2024.
//

import Foundation
import SwiftData
import UIKit
import SwiftUI

@Model
final class Scan {
    let timestamp: Date
    var images: [Data]
    
    var imageCount: Int
    
//    var uiImages: [UIImage] {
//        self.images.map { UIImage(data: $0) ?? UIImage(systemName: "exclamationmark.questionmark")! }
//    }
    
//    var suiImages: [Image] {
//        self.images.map { Image(uiImage: UIImage(data: $0) ?? UIImage(systemName: "exclamationmark.questionmark")!) }
//    }
 
    init() {
        self.timestamp = Date()
        self.images = []
        self.imageCount = 0
    }
    
    init(images: [Data]) {
        self.timestamp = Date()
        self.images = images
        self.imageCount = images.count
    }
    
    init(images: [UIImage]) {
        self.timestamp = Date()
        
        self.images = images.map{ $0.pngData() ?? Data() }
        self.imageCount = images.count
    }
    
    public func toSwiftUIImages() -> [Image] {
        self.images.map { Image(uiImage: UIImage(data: $0) ?? UIImage(systemName: "exclamationmark.questionmark")!) }
    }
    
    public func toUIImages() -> [UIImage] {
        self.images.map { UIImage(data: $0) ?? UIImage(systemName: "exclamationmark.questionmark")! }
    }
    
    public func copyImages() {
        UIPasteboard.general.images = self.toUIImages()
    }
}
