package com.data;

import android.app.Activity;
import com.security.AndroidKeyPairManager;
import crypto.Cryptography;
import data.Message;
import data.User;
import data.contents.ChatContent;
import networking.SChatClient;
import networking.SChatServer;

import java.io.IOException;
import java.security.KeyPair;

/**
 * @author Gary Ye
 * @version 12/2/13
 * One has to specify the following data:
 *  - host name (default: server name)
 *  - port ( default: port number )
 *
 */
public class ApplicationUser extends User {
    private static ApplicationUser instance = null;

    private SChatClient client;
    private AndroidSQLManager dbMangager;

    private final static String hostName = "62.178.242.13";
    // private final static String hostName = "62.178.242.13";
    private final static int portNumber = SChatServer.PORT_ADDRESS;

    private ApplicationUser() throws IOException {
        this(hostName, portNumber);
    }

    private ApplicationUser(String hostName, int portNumber) throws IOException {
        dbMangager = new AndroidSQLManager();
        dbMangager.connect();

        // pass this object as a reference so the chat listener is able to call the method 'receiveMessage'
        // client = new SChatClient(this, hostName, portNumber, dbMangager);
    }

    public static ApplicationUser getInstance() throws IOException {
        if(instance == null){
            synchronized (ApplicationUser.class){
                if (instance == null) {
                    instance = new ApplicationUser();
                }
            }
        }
        return instance;
    }

    public void connect() {
        try {
            client = new SChatClient(this, hostName, portNumber, dbMangager);
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    }

    public boolean registerToServer(){
        try {
            client.registerToServer();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return true;
    }

    public boolean loginToServer(){
        try{
            client.loginToServer();
        } catch(IOException e){
            e.printStackTrace();
        }
        return true;
    }

    public void sendMessage(ChatContent content, String receiver_id) {
        client.sendMessage(content, receiver_id);
    }

    public void initialize(Activity activity) {
        if(!dbMangager.userExists(SChatServer.SERVER_ID)) {
            User server = new User(SChatServer.SERVER_ID, new KeyPair(AndroidKeyPairManager.getServerPK(activity), null), Cryptography.gen_symm_key());
            dbMangager.insertUser(server);
        }

        if(!AndroidKeyPairManager.isKeyPairAlreadySaved(activity)) {
            KeyPair keyPair1 = Cryptography.gen_asymm_key();
            AndroidKeyPairManager.saveKeyPair(activity, keyPair1);
        }

        keyPair = AndroidKeyPairManager.getKeyPairFormSharedPref(activity);
        id = "TestUser";
    }
}

@@ -123,19 +123,22 @@
 ort 
-data.Messag
+crypto.Envelop
 e;%0Ai
@@ -184,24 +184,64 @@
 hatContent;%0A
+import data.contents.PublicKeyResponse;%0A
 import netwo
@@ -291,16 +291,47 @@
 erver;%0A%0A
+import javax.crypto.SecretKey;%0A
 import j
@@ -426,16 +426,25 @@
 /2/13%0A *
+         
  One has
@@ -477,16 +477,24 @@
 data:%0A *
+        
   - host
@@ -524,16 +524,24 @@
 name)%0A *
+        
   - port
@@ -570,11 +570,8 @@
 r )%0A
- *%0A
  */%0A
@@ -1458,16 +1458,17 @@
       if
+ 
 (instanc
@@ -1477,16 +1477,17 @@
 == null)
+ 
 %7B%0A      
@@ -1528,16 +1528,17 @@
 r.class)
+ 
 %7B%0A      
@@ -2007,32 +2007,33 @@
 gisterToServer()
+ 
 %7B%0A        try %7B%0A
@@ -2210,16 +2210,17 @@
 Server()
+ 
 %7B%0A      
@@ -2224,16 +2224,17 @@
      try
+ 
 %7B%0A      
@@ -2278,16 +2278,17 @@
  %7D catch
+ 
 (IOExcep
@@ -2294,16 +2294,17 @@
 ption e)
+ 
 %7B%0A      
@@ -2488,32 +2488,447 @@
 ver_id);%0A    %7D%0A%0A
+    @Override%0A    public void registerUser(Envelope envelope) %7B%0A        SecretKey secretKey1 = envelope.getUnwrappedKey(keyPair.getPrivate());%0A        PublicKeyResponse publicKeyResponse = envelope.%3CPublicKeyResponse%3EdecryptMessage(secretKey1).getContent();%0A        dbMangager.insertUser(new User(publicKeyResponse.getRequestId(),%0A                new KeyPair(publicKeyResponse.getPublicKey(), null), null));%0A    %7D%0A%0A
     public void 
@@ -2969,16 +2969,17 @@
       if
+ 
 (!dbMang
@@ -3236,16 +3236,17 @@
       if
+ 
 (!Androi
@@ -3529,16 +3529,12 @@
  = %22
-TestUser
+Gary
 %22;%0A 

Add to contactlist android user