 }
 
 enum NewDataManagerError: ErrorType {
-    case JSONParseError, ServerError, NoDeckWithGivenId, ErrorWith(message: String)
+    case JSONParseError, ServerError, NoLocalData, ErrorWith(message: String)
 }
 
 public class NewDataManager {

                                                "flashcardsCount": flashcardsCount,
                                                "name": name]
        request(.GET, url: url(fromRouter: Router.Deck(child: .All)), parameters: parameters).responseJSON {
            switch $0.result {
            case .Success(let value):
                let json = JSON(value).arrayValue
                completion(.Success(obj: json))
            case .Failure(let error):
                completion(.Error(err: error))
            }
            
        }
    }
    
    func addFlashcard(deckId: String, flashcardInJSON json: JSON, completion: (ServerResultType<JSON>) -> ()) {
        
        request(.POST, url: url(fromRouter: Router.Flashcards(child: .Post, deckId: deckId))).responseJSON {
            switch $0.result {
            case .Success(let value):
                completion(self.wrongRequest(responseValue: value))
            case .Failure(let error):
                completion(.Error(err: error))
            }
        }
        
    }

    
    // Metoda sprawdza czy odpowiedź serwera zawiera pole 'message' - jeśli tak oznacza to, że coś poszło nie tak,
    // w przypadku jego braku dostajemy dane o które prosiliśmy
    private func handleResponse<T>(responseResult result: Alamofire.Result<AnyObject, NSError>,
                                               completion: (ServerResultType<T>)->(),
                                               successAction: ((JSON) -> (T))) {
        switch result {
        case .Success(let val):
            let json = JSON(val)
            if let message = json.dictionary?["message"]?.string {
                completion(.Error(err: ServerError.ErrorWithMessage(text: message)))
            } else {
                completion(.Success(obj: successAction(json)))
            }
            
        case .Failure(let err):
            completion(.Error(err: err))
        }
        
    }
    
    private func handleResponse(responseResult result: Alamofire.Result<AnyObject, NSError>,
                                completion: (ServerResultType<JSON>)->(),
                                successAction: ((JSON) -> ())? = nil ) {
        handleResponse(responseResult: result, completion: completion) { json in
            return json
        }
    }
    
    // Jeśli udało się zalogować metoda zwróci ServerResultType.Success z obiektem nil,
    // w przeciwnym wypadku obiekt to String z odpowiedzią serwera (powód błędu logowania)
    public func login(username: String, password: String, completion: (ServerResultType<JSON>)->()) {
        request(.GET, url: url(fromRouter: Router.User(child: .Current))).responseJSON {
            self.handleResponse(responseResult: $0.result, completion: completion) { json in
                self.username = username
                self.password = password
                self.defaults.setObject(username, forKey: UserDefaultsKeys.LoggedUserUsername.rawValue)
                self.defaults.setObject(password, forKey: UserDefaultsKeys.LoggedUserPassword.rawValue)
                return json
            }
        }
    }
    
    
    func deck(deckID: String, completion: (ServerResultType<JSON>) -> ()) {
        let router = Router.Deck(child: .Single(id: deckID))
        request(.GET, url: url(fromRouter: router)).responseJSON {
            self.handleResponse(responseResult: $0.result, completion: completion)
        }
    }
    
