         return true
     }
     
-    func get<T: Object>(withId idKey: String) -> T? {
+    func get<T: Object>(type: T.Type, withId idKey: String) -> T? {
         return realm?.objectForPrimaryKey(T.self, key: idKey)
     }
     
-    func get<T: Object>(filter: String?) -> [T]? {
-        return realm?.objects(T.self).toArray()
+    func getAll<T: Object>(type: T.Type) -> [T] {
+        return realm?.objects(T.self).toArray() ?? []
+    }
+    
+    func filter<T: Object>(type: T.Type, predicate: String, args: AnyObject...) -> [T] {
+        return realm?.objects(T.self).filter(predicate, args).toArray() ?? []
     }
     
     func update(object: Object) -> Bool {

    func url(baseURL: NSURL) -> NSURL {
        switch self {
        case .Post:
            return baseURL.URLByAppendingPathComponent("/flashcards")
        }
    }
}

enum DeckRoutes  {
    case Get(id:String?)
    
    func url(baseURL: NSURL) -> NSURL {
        switch self {
        case .Get(let id):
            if let id = id {
                return baseURL.URLByAppendingPathComponent("/\(id)")
            }
            return baseURL
        }
    }

struct ManagerMode: OptionSetType {
    var rawValue: Int
    
    static let Local = ManagerMode(rawValue: 1 << 0)
    static let Remote = ManagerMode(rawValue: 1 << 1)
    
}

public enum DataManagerResponseType {
    case Local, Remote
}

public enum DataManagerResponse<T> {
    case Success(obj: T, responseType: DataManagerResponseType)
    case Error(obj: NewDataManagerError, responseType: DataManagerResponseType)
}

public enum NewDataManagerError: ErrorType {
    case JSONParseError, ServerError(err: ErrorType), NoDeckWithGivenId, ErrorWithMessage(text: String), DataManagerWasDeinitialized
}
private class ModeHandler<T> {
    var local: (() -> DataManagerResponse<T>)?
    
    // wylaczenie ostrzezenia swift lint, aby moc przypisac wartosci poszczegolnym blokom bez ponownego pisania ich typu 
    // force unwrap zamiast optional, poniewaz w przypadku odwolania do serwera powinny zostac obsluzone oba przypadki
    var remote: (remoteSuccess: ((obj: JSON) -> DataManagerResponse<T>)!, //swiftlint:disable:this force_unwrapping
                remoteError: ((obj: ErrorType) -> DataManagerResponse<T>)!)?     //swiftlint:disable:this force_unwrapping

    var remoteRequest: (() -> ())?
    var activeMode: ManagerMode
    var callback: (DataManagerResponse<T>) -> ()
    
    init(withMode mode: ManagerMode, callback: (DataManagerResponse<T>) -> ()) {
        self.activeMode = mode
        self.callback = callback
    }
    
    func start() {
        if activeMode.contains(.Remote) {
            remoteRequest?()
        } else if activeMode == .Local {
            local?()
        }
    }
    
}
public class NewDataManager {
    
    let remoteDataManager = RemoteDataManager()
    let localDataManager = LocalDataManager()
    
    var mode: ManagerMode = [.Local, .Remote]
    
    private func handlerCompletion<T>(handler: ModeHandler<T>) -> (response: ServerResultType<JSON>) -> () {
    
        return { response in
            
            switch response {
            case .Success(let obj):
                if let result = handler.remote?.remoteSuccess(obj: obj) {
                    handler.callback(result)
                }
            case .Error(let obj):
                if handler.activeMode.contains(.Local), let local = handler.local {
                    handler.callback(local())
                } else if let result = handler.remote?.remoteError(obj: obj) {
                    handler.callback(result)
                }
                
            }
            handler.remoteRequest = nil
        }
    }
    
    func getDeck(withDeckId deckID: String, managerCompletion: (DataManagerResponse<Deck>)-> ()) {
       
        let modeBlocks = ModeHandler<Deck>(withMode: mode, callback: managerCompletion)
       
        modeBlocks.remoteRequest = {
            self.remoteDataManager.deck(deckID, completion: self.handlerCompletion(modeBlocks))
        }
        modeBlocks.local = {
            if let deck  = self.localDataManager.get(Deck.self, withId: deckID) {
                return .Success(obj: deck, responseType: .Local)
            } else {
                return .Error(obj: .NoDeckWithGivenId, responseType: .Local)
            }
        }
        
        modeBlocks.remote = (nil, nil)
        
        modeBlocks.remote?.remoteSuccess = {  obj in
            if let deck = Deck.withJSON(obj) {
                self.localDataManager.update(deck)
                return .Success(obj: deck, responseType: .Remote)
            } else {
                return .Error(obj: .JSONParseError, responseType: .Remote)
            }
            
        }
        
        modeBlocks.remote?.remoteError = { obj in
            return .Error(obj: .ServerError(err: obj), responseType: .Remote)
        }
        
        modeBlocks.start()
    }
}
