//
//  KMTransitionInfo.swift
//  PDF Reader Pro
//
//  Created by liujiajie on 2024/1/17.
//

import Foundation

let SKPasteboardTypeTransition = "net.sourceforge.skim-app.pasteboard.transition"

class KMTransitionInfo: NSObject, NSPasteboardReading, NSPasteboardWriting {
    var transitionStyle: SKAnimationTransitionStyle = .noTransition
    var duration: Float = 0.0
    var shouldRestrict = false {
        didSet {
//            KMPrint("")
        }
    }
    var thumbnail: KMThumbnail?
    var label: String = ""
    var title: String {
        get {
            return NSLocalizedString("Page Transition", comment: "Box title")
        }
    }
    
    var properties: NSDictionary?{
        get{
            return [SKStyleNameKey: SKTransitionController.name(for: transitionStyle) ?? "", SKDurationKey: NSNumber(value: duration), SKShouldRestrictKey: NSNumber(value: shouldRestrict)] as [String : Any] as NSDictionary
        }
        set{
            if let value = newValue?[SKStyleNameKey] {
                transitionStyle = SKTransitionController.style(forName: value as? String)
            }
            if let value = newValue?[SKDurationKey] {
                duration = value as! Float
            }
            if let value = newValue?[SKShouldRestrictKey] {
                shouldRestrict = value as! Bool
            }
        }
    }
    override init() {
        super.init()
        
        self.transitionStyle = .noTransition
        self.duration = 1.0
        self.shouldRestrict = false
        self.thumbnail = nil
        self.label = ""
    }
    
    override var description: String {
        return "<\(type(of: self)) \(Unmanaged.passUnretained(self).toOpaque())> \(properties)"
    }
    
    static func readingOptions(forType type: NSPasteboard.PasteboardType, pasteboard: NSPasteboard) -> NSPasteboard.ReadingOptions {
        if type.rawValue == SKPasteboardTypeTransition {
            return .asPropertyList
        }
        return .asData
    }
    
    static func readableTypes(for pasteboard: NSPasteboard) -> [NSPasteboard.PasteboardType] {
        let type = NSPasteboard.PasteboardType(SKPasteboardTypeTransition)
        return [type]
    }
    
    required convenience init?(pasteboardPropertyList propertyList: Any, ofType type: NSPasteboard.PasteboardType) {
        self.init()
        if type.rawValue == SKPasteboardTypeTransition {
            self.properties = propertyList as? NSDictionary ?? [:]
        }
    }
    
    func writableTypes(for pasteboard: NSPasteboard) -> [NSPasteboard.PasteboardType] {
        let type = NSPasteboard.PasteboardType(SKPasteboardTypeTransition)
        return [type]
    }
    
    func pasteboardPropertyList(forType type: NSPasteboard.PasteboardType) -> Any? {
        if type.rawValue == SKPasteboardTypeTransition {
            return properties
        }
        return nil
    }
}