package io.github.shark_app.shark;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.nbsp.materialfilepicker.MaterialFilePicker;
import com.nbsp.materialfilepicker.ui.FilePickerActivity;

import java.util.regex.Pattern;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class RegisterActivity extends AppCompatActivity {
    public static final int PERMISSIONS_REQUEST_CODE = 0;
    public static final int PUBLIC_KEY_FILE_PICKER_REQUEST_CODE = 1;
    public static final int PRIVATE_KEY_FILE_PICKER_REQUEST_CODE = 2;
    private int FILE_PICKER_REQUEST_CODE;

    @BindView(R.id.nameField) EditText nameField;
    @BindView(R.id.emailField) EditText emailField;
    @BindView(R.id.publicKeyButton) Button publicKeyButton;
    @BindView(R.id.privateKeyButton) Button privateKeyButton;
    @BindView(R.id.registerButton) Button registerButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        ButterKnife.bind(this);
    }

    @OnClick(R.id.publicKeyButton)
    public void pickPublicKeyFile(View view) {
        FILE_PICKER_REQUEST_CODE = PUBLIC_KEY_FILE_PICKER_REQUEST_CODE;
        checkPermissionsAndOpenFilePicker();
    }

    @OnClick(R.id.privateKeyButton)
    public void pickPrivateKeyFile(View view) {
        FILE_PICKER_REQUEST_CODE = PRIVATE_KEY_FILE_PICKER_REQUEST_CODE;
        checkPermissionsAndOpenFilePicker();
    }

    @OnClick(R.id.registerButton)
    public void registerUser(View view) {
    }

    private void checkPermissionsAndOpenFilePicker() {
        String permission = Manifest.permission.READ_EXTERNAL_STORAGE;
        if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) {
            if (ActivityCompat.shouldShowRequestPermissionRationale(this, permission)) {
                showError();
            } else {
                ActivityCompat.requestPermissions(this, new String[]{permission}, PERMISSIONS_REQUEST_CODE);
            }
        } else {
            openFilePicker(FILE_PICKER_REQUEST_CODE);
        }
    }

    private void showError() {
        Toast.makeText(this, "Allow external storage reading", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults) {
        switch (requestCode) {
            case PERMISSIONS_REQUEST_CODE: {
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    openFilePicker(FILE_PICKER_REQUEST_CODE);
                } else {
                    showError();
                }
            }
        }
    }

    private void openFilePicker(int FILE_PICKER_REQUEST_CODE) {
        new MaterialFilePicker()
                .withActivity(this)
                .withRequestCode(FILE_PICKER_REQUEST_CODE)
                .withFilter(Pattern.compile(".*\\.txt$"))
                .withHiddenFiles(true)
                .start();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == PUBLIC_KEY_FILE_PICKER_REQUEST_CODE && resultCode == RESULT_OK) {
            FILE_PICKER_REQUEST_CODE = -1;
            String path = data.getStringExtra(FilePickerActivity.RESULT_FILE_PATH);
            if (path != null) {
                Log.d("Path: ", path);
                Toast.makeText(this, "Picked PUBLIC KEY file: " + path, Toast.LENGTH_LONG).show();
            }
        }
        else if (requestCode == PRIVATE_KEY_FILE_PICKER_REQUEST_CODE && resultCode == RESULT_OK) {
            FILE_PICKER_REQUEST_CODE = -1;
            String path = data.getStringExtra(FilePickerActivity.RESULT_FILE_PATH);
            if (path != null) {
                Log.d("Path: ", path);
                Toast.makeText(this, "Picked PRIVATE KEY file: " + path, Toast.LENGTH_LONG).show();
            }
        }

    }
}

@@ -597,16 +597,37 @@
 ivity;%0A%0A
+import java.io.File;%0A
 import j
@@ -1037,16 +1037,91 @@
 ST_CODE;
+%0A    public String publicKeyFilePath;%0A    public String privateKeyFilePath;
 %0A%0A    @B
@@ -3531,16 +3531,17 @@
 ompile(%22
+(
 .*%5C%5C.txt
@@ -3541,16 +3541,37 @@
 *%5C%5C.txt$
+%7C.*%5C%5C.gpg$%7C.*%5C%5C.asc$)
 %22))%0A    
@@ -3826,27 +3826,16 @@
 Code == 
-PUBLIC_KEY_
 FILE_PIC
@@ -3885,51 +3885,8 @@
 ) %7B%0A
-            FILE_PICKER_REQUEST_CODE = -1;%0A
     
@@ -3892,33 +3892,37 @@
          String 
-p
+fullP
 ath = data.getSt
@@ -3982,35 +3982,297 @@
        i
-f (path != null) %7B%0A
+nt index = fullPath.lastIndexOf(File.separator);%0A            String path = fullPath.substring(index + 1);%0A            if (path != null) %7B%0A                switch(FILE_PICKER_REQUEST_CODE) %7B%0A                    case 1: %7B%0A                        publicKeyFilePath = fullPath;%0A        
         
@@ -4294,32 +4294,40 @@
 Path: %22, path);%0A
+        
                 
@@ -4417,34 +4417,32 @@
             
-%7D%0A
         
 %7D%0A        el
@@ -4433,268 +4433,206 @@
     
-%7D%0A
     
-    else if (requestCode == PRIVATE_KEY_FILE_PICKER_REQUEST_CODE && resultCode == RESULT_OK) %7B%0A            FILE_PICKER_REQUEST_CODE = -1;%0A            String path = data.getStringExtra(FilePickerActivity.RESULT_FILE_PATH);%0A            if (p
+publicKeyButton.setText(%22Picked file %22 + path);%0A                        break;%0A                    %7D%0A                    case 2: %7B%0A                        privateKeyFileP
 ath 
-!
 = 
-null) %7B%0A
+fullPath;%0A        
     
@@ -4658,32 +4658,40 @@
 Path: %22, path);%0A
+        
                 
@@ -4786,28 +4786,214 @@
         
-%7D%0A        %7D%0A
+            privateKeyButton.setText(%22Picked file %22 + path);%0A                        break;%0A                    %7D%0A                %7D%0A            %7D%0A            FILE_PICKER_REQUEST_CODE = -1;%0A        %7D
 %0A    %7D%0A%7D

Update file filter, show picked file on button