//
//  Poll.swift
//  ComposeUI
//
//  Created by Shadowfacts on 4/22/23.
//

import CoreData

@objc
public class Poll: NSManagedObject {
    
    @NSManaged public var duration: TimeInterval
    @NSManaged public var multiple: Bool
    
    @NSManaged public var draft: Draft
    @NSManaged public var options: NSMutableOrderedSet
    
    public var pollOptions: [PollOption] {
        get {
            options.array as! [PollOption]
        }
        set {
            options = NSMutableOrderedSet(array: newValue)
        }
    }
    
    public override func awakeFromInsert() {
        super.awakeFromInsert()
        self.multiple = false
        self.duration = 24 * 60 * 60 // 1 day
        if let managedObjectContext {
            self.options = [
                PollOption(context: managedObjectContext),
                PollOption(context: managedObjectContext),
            ]
        }
    }
    
}

extension Poll {
    public var hasContent: Bool {
        pollOptions.allSatisfy { !$0.text.isEmpty }
    }
}
