package ch.frontg8.view;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Messenger;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
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 ch.frontg8.R;
import ch.frontg8.lib.data.DataService;
import ch.frontg8.lib.data.MessageTypes;
import ch.frontg8.lib.filechooser.view.FileChooser;

public class CertImportActivity extends AppCompatActivity {
    private Activity thisActivity = this;
    private static final int REQUEST_PATH = 1;
    private static final int IMPORT_CERT = 2;
    private static final int IMPORT_KEYPAIR = 3;
    private static final int EXPORT_KEYPAIR = 4;
    private TextView textViewLog;

    // Messenger to get Contacts
    final Messenger mMessenger = new Messenger(new IncomingHandler());
    private Messenger mService;

    // Connection to DataService
    private ServiceConnection mConnection = new ServiceConnection() {

        @Override
        public void onServiceConnected(ComponentName className, IBinder binder) {
            mService = new Messenger(binder);
        }

        @Override
        public void onServiceDisconnected(ComponentName className) {
            mService = null;
        }
    };

    // Handler for Messages from DataService
    class IncomingHandler extends Handler {
        @Override
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
                case MessageTypes.MSG_ERROR:
                    textViewLog.append("Got Error from Data-Service");
                    break;
                default:
                    super.handleMessage(msg);
            }
        }
    }


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

        Button buttonImportCert = (Button) findViewById(R.id.buttonImportCert);
        Button buttonImportKeypair = (Button) findViewById(R.id.buttonImportKeypair);
        Button buttonExportKeypair = (Button) findViewById(R.id.buttonExportKeypair);
        textViewLog = (TextView) findViewById(R.id.textViewLog);

        buttonImportCert.setOnClickListener(new AdapterView.OnClickListener() {
            public void onClick(View view) {
                openFileChooser(IMPORT_CERT, false, true);
            }
        });

        buttonImportKeypair.setOnClickListener(new AdapterView.OnClickListener() {
            public void onClick(View view) {
                openFileChooser(IMPORT_KEYPAIR, false, true);
            }
        });

        buttonExportKeypair.setOnClickListener(new AdapterView.OnClickListener() {
            public void onClick(View view) {
                openFileChooser(EXPORT_KEYPAIR, true, false);
            }
        });
    }

    private void openFileChooser(int identId, boolean chooseDir, boolean showFiles){
        Intent intent = new Intent(thisActivity, FileChooser.class);
        Bundle bundle = new Bundle();
        bundle.putSerializable(FileChooser.CHOOSE_DIR, chooseDir);
        bundle.putSerializable(FileChooser.SHOW_FILES, showFiles);
//                bundle.putSerializable(FileChooser.FILE_EXTENSION, "pem");
        intent.putExtras(bundle);
        startActivityForResult(intent, identId);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_cert_import, 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);
    }

    // Listen for results.
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        // See which child activity is calling us back.
        String path;
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case IMPORT_CERT:

                    path = data.getStringExtra("GetPath") + "/" + data.getStringExtra("GetFileName");
                    textViewLog.setText("Import CA Cert from: " + path);
                    try {
                        mService.send(android.os.Message.obtain(null, MessageTypes.MSG_IMPORT_CACERT, path));
                    } catch (RemoteException e) {
                        e.printStackTrace();
                    }
                    break;
                case IMPORT_KEYPAIR:
                    path = data.getStringExtra("GetPath") + "/" + data.getStringExtra("GetFileName");
                    textViewLog.setText("Import Keypair from: " + path);
                    try {
                        mService.send(android.os.Message.obtain(null, MessageTypes.MSG_IMPORT_KEY, path));
                    } catch (RemoteException e) {
                        e.printStackTrace();
                    }
                    break;
                case EXPORT_KEYPAIR:
                    path = data.getStringExtra("GetPath") + "/exported-key.pem";
                    textViewLog.setText("Export Keypair to: " + path);
                    try {
                        mService.send(android.os.Message.obtain(null, MessageTypes.MSG_EXPORT_KEY, path));
                    } catch (RemoteException e) {
                        e.printStackTrace();
                    }
                    break;
                default:
                    textViewLog.setText("Something went wrong...");
            }
        }
    }
    @Override
    public void onResume() {
        super.onResume();
        Log.d(thisActivity.getClass().getSimpleName(), "onResume");

        // bind again to DataService:
        Intent intent = new Intent(this, DataService.class);
        bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
    }

    @Override
    protected void onPause() {
        super.onPause();
        unbindService(mConnection);
    }


}

@@ -267,24 +267,51 @@
 os.IBinder;%0A
+import android.os.Message;%0A
 import andro
@@ -3299,16 +3299,17 @@
 owFiles)
+ 
 %7B%0A      
@@ -4472,17 +4472,16 @@
 T_CERT:%0A
-%0A
         
@@ -4671,240 +4671,58 @@
     
-try %7B%0A                        mService.send(android.os.Message.obtain(null, MessageTypes.MSG_IMPORT_CACERT, path));%0A                    %7D catch (RemoteException e) %7B%0A                        e.printStackTrace();%0A                    %7D
+sendMessage(MessageTypes.MSG_IMPORT_CACERT, path);
 %0A   
@@ -4981,84 +4981,20 @@
     
-try %7B%0A                        mService.send(android.os.Message.obtain(null, 
+sendMessage(
 Mess
@@ -5027,128 +5027,10 @@
 ath)
-)
 ;%0A
-                    %7D catch (RemoteException e) %7B%0A                        e.printStackTrace();%0A                    %7D%0A
     
@@ -5265,84 +5265,20 @@
     
-try %7B%0A                        mService.send(android.os.Message.obtain(null, 
+sendMessage(
 Mess
@@ -5307,17 +5307,16 @@
 Y, path)
-)
 ;%0A      
@@ -5333,131 +5333,214 @@
     
-%7D catch (RemoteException e) %7B%0A                        e.printStackTrace();%0A                    %7D%0A             
+break;%0A                default:%0A                    textViewLog.setText(%22Something went wrong...%22);%0A            %7D%0A        %7D%0A    %7D%0A%0A    private void sendMessage(int messageType, String value) %7B%0A
        
-break;
+ try %7B
 %0A   
@@ -5552,102 +5552,216 @@
     
-    default:%0A                    textViewLog.setText(%22Something went wrong...%22);%0A            %7D
+Message message = Message.obtain(null, messageType, value);%0A            message.replyTo = mMessenger;%0A            mService.send(message);%0A        %7D catch (RemoteException e) %7B%0A            e.printStackTrace();
 %0A   
@@ -5769,24 +5769,25 @@
     %7D%0A    %7D%0A
+%0A
     @Overrid

set reply for dataHandler