package ch.frontg8.view;

import android.app.Activity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.method.ScrollingMovementMethod;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.util.UUID;

import ch.frontg8.R;
import ch.frontg8.bl.Contact;
import ch.frontg8.bl.Message;
import ch.frontg8.lib.connection.TlsTest;
import ch.frontg8.lib.crypto.KeystoreHandler;
import ch.frontg8.lib.crypto.LibCrypto;
import ch.frontg8.lib.dbstore.ContactsDataSource;

public class DeveloperActivity extends AppCompatActivity {
    private Activity thisActivity;
    private ContactsDataSource datasource = new ContactsDataSource(this);
    private TextView textViewLog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_developer);

        thisActivity = this;

        Button buttonKeyGen = (Button) findViewById(R.id.buttonKeyGen);
        Button buttonClearDB = (Button) findViewById(R.id.buttonClearDB);
        Button buttonLoadTestData = (Button) findViewById(R.id.buttonLoadTestData);
        Button buttonTlsTest = (Button) findViewById(R.id.buttonTlsTest);

        datasource.open();

        buttonKeyGen.setOnClickListener(new AdapterView.OnClickListener() {
            public void onClick(View view) {
                try {
                    LibCrypto.generateNewKeys(new KeystoreHandler(thisActivity), thisActivity);
                    Toast toast = Toast.makeText(thisActivity, "Keys generated", Toast.LENGTH_SHORT);
                    toast.show();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

        buttonClearDB.setOnClickListener(new AdapterView.OnClickListener() {
            public void onClick(View view) {
                datasource.deleteAllContacts();
                datasource.deleteAllMessages();
                Toast toast = Toast.makeText(thisActivity, "all data deleted", Toast.LENGTH_SHORT);
                toast.show();
            }
        });

        buttonLoadTestData.setOnClickListener(new AdapterView.OnClickListener() {
            public void onClick(View view) {
                String keyA = "MIGbMBAGByqGSM49AgEGBSuBBAAjA4GGAAQBf72KrjMTpU60csP6cVefgJocZpj+OOTF8sNueIPU8krHCycTozNoycoguqLkI6jU66pTXtnx/nxgXprVqg6bEyMBB5oCXoPNQSrb8GBkL5p764is9dn27q57cJ/Mw1zp1W/cNKJj2uWtuyFxXcwEhjVh8Vja47BaJCbFg7drjrzTxZM=";
                String keyB = "MIGbMBAGByqGSM49AgEGBSuBBAAjA4GGAAQBGp6vB5n7L2+6QdKY5ETFnY0QhIFIUqtVW0QaSDEUYJoLceBjKLyiLiQhiYo8tANXsBlrB+F/wQPARoYbaaFKX/cBUTYioTcVWpa4r2lupMyBwZ7x3v8cznfY4aSRWcQKIOtQxpHm7sDQWnViAVmKI4Xgw50ZE0ONxgIjNBioosO3K6I=";
                String keyC = "MIGbMBAGByqGSM49AgEGBSuBBAAjA4GGAAQBRg1Zd0pBm6ST8dULpAuwWxq+llhdguXfRkuf5stzPVYOgwgjO4j3qDe6z5tgK/iS9wMSAjqYuzvr6UkQAJtAXHABEoheuPgoKp2PI3Nozn8E+zsH0RQWhKHV+XiUp4tv6TAPuyTelHVsNJYOCjS5IkzCGROQqEVzMonlyRN2HNXvaIs=";
                Contact a = datasource.createContact(new Contact(UUID.fromString("11111111-1111-1111-1111-111111111111"), "The", "Other", keyA, 2, false));
                Contact b = datasource.createContact(new Contact(UUID.fromString("22222222-2222-2222-2222-222222222222"), "Tobias", "Stauber", keyB, 1, false));
                Contact c = datasource.createContact(new Contact(UUID.fromString("33333333-3333-3333-3333-333333333333"), "Ueli", "Bosshard", keyC, 1, false));
                Contact d = datasource.createContact(new Contact(UUID.fromString("44444444-4444-4444-4444-444444444444"), "Flix", "", "", 0, false));
                Contact e = datasource.createContact(new Contact(UUID.fromString("55555555-5555-5555-5555-555555555555"), "Benny", "", "", 0, false));
                datasource.insertMessage(a, new Message("bla"));
                datasource.insertMessage(b, new Message("blb"));
                datasource.insertMessage(c, new Message("blc"));
                datasource.insertMessage(d, new Message("bld"));
                datasource.insertMessage(d, new Message("bld"));
                a.incrementUnreadMessageCounter();
                datasource.updateContact(a);
                Toast toast = Toast.makeText(thisActivity, "demo data inserted", Toast.LENGTH_SHORT);
                toast.show();
            }
        });

        buttonTlsTest.setOnClickListener(new AdapterView.OnClickListener() {
            public void onClick(View view) {
                textViewLog = (TextView) findViewById(R.id.textViewLog);
                textViewLog.setText("");
                textViewLog.setMovementMethod(new ScrollingMovementMethod());
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        TlsTest tlstest = new TlsTest(thisActivity);
                        tlstest.RunTlsTest();
                    }
                }).start();
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_developer, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

}
@@ -384,16 +384,160 @@
 Toast;%0A%0A
+import java.security.NoSuchAlgorithmException;%0Aimport java.security.NoSuchProviderException;%0Aimport java.security.spec.InvalidKeySpecException;%0A
 import j
@@ -4383,32 +4383,257 @@
 essage(%22bld%22));%0A
+%0A                try %7B%0A                    LibCrypto.negotiateSessionKeys(a.getContactId(), a.getPublicKeyString().getBytes(), new KeystoreHandler(thisActivity), thisActivity);%0A                    a.setValidPubkey(true);%0A    
                 
@@ -4663,24 +4663,28 @@
 eCounter();%0A
+    
             
@@ -4712,24 +4712,191 @@
 Contact(a);%0A
+                %7D catch (NoSuchProviderException %7C NoSuchAlgorithmException %7C InvalidKeySpecException f) %7B%0A                    f.printStackTrace();%0A                %7D%0A%0A
             

generate valid pubkey for 'the other'