package info.guardianproject.otr;

import info.guardianproject.otr.IOtrChatSession.Stub;
import info.guardianproject.util.Debug;
import net.java.otr4j.OtrException;
import net.java.otr4j.session.SessionID;
import net.java.otr4j.session.SessionStatus;
import android.os.RemoteException;

public class OtrChatSessionAdapter extends Stub {

    private OtrChatManager _chatManager;
    private String _localUser;
    private String _remoteUser;
    
    public OtrChatSessionAdapter(String localUser, String remoteUser, OtrChatManager chatManager) {

        _chatManager = chatManager;
        _localUser = localUser;
        _remoteUser = remoteUser;
    }
    
    public void end () throws RemoteException 
    {
        
        Debug.wrapExceptions(new Runnable() {
            @Override
            public void run() {
                _chatManager.removeSession(_chatManager.getSessionId(_localUser, _remoteUser));
            }
        });
    }

    public void startChatEncryption() throws RemoteException {
        Debug.wrapExceptions(new Runnable() {
            @Override
            public void run() {
                _chatManager.startSession(_chatManager.getSessionId(_localUser, _remoteUser));
            }
        });
    }
    
    @Override
    public void stopChatEncryption() throws RemoteException {
        Debug.wrapExceptions(new Runnable() {
            @Override
            public void run() {
                _chatManager.endSession(_chatManager.getSessionId(_localUser, _remoteUser));
            }
        });
    }

    @Override
    public boolean isChatEncrypted() throws RemoteException {

        return _chatManager.getSessionStatus(_chatManager.getSessionId(_localUser, _remoteUser)) == SessionStatus.ENCRYPTED;

    }


    @Override
    public int getChatStatus() throws RemoteException {
        
        if (_chatManager != null)
        {
            SessionStatus sessionStatus = _chatManager.getSessionStatus(_chatManager.getSessionId(_localUser, _remoteUser));
            if (sessionStatus == null)
                sessionStatus = SessionStatus.PLAINTEXT;
            return sessionStatus.ordinal();
        }
        else
        {
            return SessionStatus.PLAINTEXT.ordinal();
        }
    }

    @Override
    public void initSmpVerification(String question, String secret) throws RemoteException {

        try {
            _chatManager.initSmp(_chatManager.getSessionId(_localUser, _remoteUser), question,
                    secret);
        } catch (OtrException e) {
            OtrDebugLogger.log("initSmp", e);
            throw new RemoteException();
        }
    }

    @Override
    public void respondSmpVerification(String answer) throws RemoteException {

        try {
            _chatManager.respondSmp(_chatManager.getSessionId(_localUser, _remoteUser), answer);
            
        } catch (OtrException e) {
            OtrDebugLogger.log("respondSmp", e);
            throw new RemoteException();
        }
    }
    
    @Override
    public void verifyKey(String address) throws RemoteException {
        
        _chatManager.getKeyManager().verify(_chatManager.getSessionId(_localUser, _remoteUser));
        
    }

    @Override
    public void unverifyKey(String address) throws RemoteException {
        _chatManager.getKeyManager().unverify(_chatManager.getSessionId(_localUser, _remoteUser));
    }

    @Override
    public boolean isKeyVerified(String address) throws RemoteException {
        return _chatManager.getKeyManager().isVerified(_chatManager.getSessionId(_localUser, _remoteUser));
    }

    @Override
    public String getLocalFingerprint() throws RemoteException {
        return _chatManager.getKeyManager().getLocalFingerprint(_chatManager.getSessionId(_localUser, _remoteUser));
        
    }

    @Override
    public String getRemoteFingerprint() throws RemoteException {
        return _chatManager.getKeyManager().getRemoteFingerprint(_chatManager.getSessionId(_localUser, _remoteUser));
    }

    @Override
    public void generateLocalKeyPair() throws RemoteException {
        _chatManager.getKeyManager().generateLocalKeyPair(_chatManager.getSessionId(_localUser, _remoteUser));
    }

    @Override
    public String getLocalUserId() throws RemoteException {
        return _localUser;
    }

    @Override
    public String getRemoteUserId() throws RemoteException {
        return _remoteUser;
    }

    
}

@@ -1116,32 +1116,36 @@
                 
+if (
 _chatManager.sta
@@ -1144,74 +1144,234 @@
 ager
-.startSession(_chatManager.getSessionId(_localUser, _remoteUser));
+ != null)%0A                %7B%0A                    SessionID sid = _chatManager.getSessionId(_localUser, _remoteUser);%0A                    if (sid != null)%0A                        _chatManager.startSession(sid);%0A                %7D
 %0A   
@@ -1588,32 +1588,36 @@
                 
+if (
 _chatManager.end
@@ -1616,72 +1616,232 @@
 ager
-.endSession(_chatManager.getSessionId(_localUser, _remoteUser));
+ != null)%0A                %7B%0A                    SessionID sid = _chatManager.getSessionId(_localUser, _remoteUser);%0A                    if (sid != null)%0A                        _chatManager.endSession(sid);%0A                %7D
 %0A   
@@ -1959,45 +1959,24 @@
     
-return _chatManager.getSessionStatus(
+SessionID sid = 
 _cha
@@ -2025,38 +2025,164 @@
 ser)
-) == SessionStatus.ENCRYPTED;%0A
+;%0A        %0A        if (sid != null)%0A            return _chatManager.getSessionStatus(sid) == SessionStatus.ENCRYPTED;%0A        else%0A            return false;
 %0A   

make sure the chatmanager or sessionID is not null