 
 // MARK: Talk initiation
 extension SecureChat {
-    private func initiateTalk(withCardsSet cardsSet: RecipientCardsSet, completion: @escaping (SecureTalk?, Error?)->()) throws {
+    private func initiateTalk(withCardsSet cardsSet: RecipientCardsSet, completion: @escaping (SecureTalk?, Error?)->()) {
         // FIXME: Check for existing session
         guard let identityCard = self.identityCard else {
             completion(nil, NSError(domain: SecureChat.ErrorDomain, code: -1, userInfo: [NSLocalizedDescriptionKey: "Identity card missing. Probably, SecureChat was not initialized."]))

        completion(secureTalk, nil)
    }
    
    private func initiateTalk(withRecipientWithCard card: VSSCard, completion: @escaping (SecureTalk?, Error?)->()) throws {
        // FIXME: Check for existing session
        guard let identityCard = self.identityCard else {
            completion(nil, NSError(domain: SecureChat.ErrorDomain, code: -1, userInfo: [NSLocalizedDescriptionKey: "Identity card missing. Probably, SecureChat was not initialized."]))
            return
        }
        
        // FIXME: Error handling
        // FIXME: Check is session active
        let session = try self.sessionHelper.getSessionState(forRecipientCardId: card.identifier, crypto: self.preferences.crypto)
        
        if let session = session as? InitiatorSessionState {
            let ephKeyName = session.ephKeyName
            let ephPrivateKey = try self.keyHelper.getEphPrivateKey(withKeyEntryName: ephKeyName)
            
            let identityCardEntry = SecureTalk.CardEntry(identifier: card.identifier, publicKeyData: card.publicKeyData)
            let ltCardEntry = SecureTalk.CardEntry(identifier: session.recipientLongTermCardId, publicKeyData: session.recipientLongTermPublicKey)
            let otCardEntry = SecureTalk.CardEntry(identifier: session.recipientOneTimeCardId, publicKeyData: session.recipientOneTimePublicKey)
            
            let secureTalk = SecureTalkInitiator(crypto: self.preferences.crypto, myPrivateKey: self.preferences.myPrivateKey, myIdCard: identityCard, ephPrivateKey: ephPrivateKey, recipientIdCard: identityCardEntry, recipientLtCard: ltCardEntry, recipientOtCard: otCardEntry, wasRecovered: true)
            
            completion(secureTalk, nil)
        }
        else if let session = session as? ResponderSessionState {
//            let initiatorCardEntry = SecureTalk.CardEntry(identifier: session., publicKeyData: <#T##Data#>)
//            SecureTalkResponder(crypto: self.preferences.crypto, myPrivateKey: self.preferences.myPrivateKey, secureChatKeyHelper: self.keyHelper, initiatorCardEntry: <#T##SecureTalk.CardEntry#>)
            
            
            // FIXME
        }
        else {
            //FIXME
        }
    }
    
    public func initiateTalk(withRecipientWithCardId cardId: String, completion: @escaping (SecureTalk?, Error?)->()) throws {
        self.virgilClient.getCard(withId: cardId) { card, error in
            guard error == nil else {
                completion(nil, error)

                completion(secureTalk, error)
            }
        }
    }
}

// MARK: Talk recovering
extension SecureChat {
    fileprivate func recoverInitiatorTalk(withRecipientWithCard card: VSSCard, myIdentityCard: VSSCard, initiatorSessionState: InitiatorSessionState, completion: @escaping (SecureTalk?, Error?)->())  {
        let ephKeyName = initiatorSessionState.ephKeyName
        let ephPrivateKey: VSSPrivateKey
        do {
            ephPrivateKey = try self.keyHelper.getEphPrivateKey(withKeyEntryName: ephKeyName)
        }
        catch {
            completion(nil, NSError(domain: SecureChat.ErrorDomain, code: -1, userInfo: [NSLocalizedDescriptionKey: "Error getting ephemeral key from storage."]))
            return
        }
        
        let identityCardEntry = SecureTalk.CardEntry(identifier: card.identifier, publicKeyData: card.publicKeyData)
        let ltCardEntry = SecureTalk.CardEntry(identifier: initiatorSessionState.recipientLongTermCardId, publicKeyData: initiatorSessionState.recipientLongTermPublicKey)
        let otCardEntry = SecureTalk.CardEntry(identifier: initiatorSessionState.recipientOneTimeCardId, publicKeyData: initiatorSessionState.recipientOneTimePublicKey)
        
        let secureTalk = SecureTalkInitiator(crypto: self.preferences.crypto, myPrivateKey: self.preferences.myPrivateKey, myIdCard: myIdentityCard, ephPrivateKey: ephPrivateKey, recipientIdCard: identityCardEntry, recipientLtCard: ltCardEntry, recipientOtCard: otCardEntry, wasRecovered: true)
        
        completion(secureTalk, nil)
    }
    
    fileprivate func recoverResponderTalk(withRecipientWithCard card: VSSCard, myIdentityCard: VSSCard, responderSessionState: ResponderSessionState, completion: @escaping (SecureTalk?, Error?)->())  {
        let initiatorCardEntry = SecureTalk.CardEntry(identifier: card.identifier, publicKeyData: card.publicKeyData)
        let secureTalk = SecureTalkResponder(crypto: self.preferences.crypto, myPrivateKey: self.preferences.myPrivateKey, secureChatKeyHelper: self.keyHelper, initiatorCardEntry: initiatorCardEntry, wasRecovered: true)
        
        completion(secureTalk, nil)
    }
}

