//
//  LKFFilter.swift
//  lkfc
//
//  Created by Alexander Holzer on 06.01.22.
//  Copyright © 2022 Alexander Holzer. All rights reserved.
//

import Foundation

open class LKFFilter {
    
    public init () {
        
    }
    
    open func filter (contacts:[LKFAufenthalt]) -> [LKFAufenthalt] {
        return contacts.filter { contact in
            return isIncluded(contact: contact)
        }
    }
    
    open func isIncluded (contact:LKFAufenthalt) -> Bool {
        return true
    }
}

public class LKFFilterAND : LKFFilter {
    let conditions : [LKFFilter]
    
    public init (conditions:[LKFFilter]) {
        self.conditions = conditions
    }

    public override func isIncluded(contact: LKFAufenthalt) -> Bool {
        for c in conditions {
            if !c.isIncluded(contact: contact) {
                return false
            }
        }
        return true
    }
}

public class LKFFilterOR : LKFFilter {
    let conditions : [LKFFilter]
    
    public init (conditions: [LKFFilter]) {
        self.conditions = conditions
    }
    
    public override func isIncluded(contact: LKFAufenthalt) -> Bool {
        for c in conditions {
            if (c.isIncluded(contact: contact)) {
                return true
            }
        }
        return false
    }
}

public class LKFFilterNegate : LKFFilter {
    
    let condition: LKFFilter
    
    public init (condition: LKFFilter) {
        self.condition = condition
    }
    
    public override func isIncluded(contact: LKFAufenthalt) -> Bool {
        return !condition.isIncluded(contact: contact)
    }
}
