package li.doerf.hackedimporter;

import android.Manifest;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.ArraySet;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import static android.content.Context.ACCOUNT_SERVICE;
import static li.doerf.hackedimporter.R.id.accounts;

public class AccountListActivity extends AppCompatActivity {
    private static final int PERMISSIONS_REQUEST_GET_ACCOUNT = 1;
    private final String LOGTAG = "AccountListActivity";
    private ArrayList<String> accounts;

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

        Button importAccounts = (Button) findViewById(R.id.searchaccounts);
        importAccounts.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if ( getCallingActivity() == null ) {
                    Toast.makeText(getApplicationContext(), "Please call this app from Hacked?", Toast.LENGTH_LONG).show();
                    return;
                }

                Intent resultIntent = new Intent();
                resultIntent.putStringArrayListExtra("accounts", accounts);
                setResult(Activity.RESULT_OK, resultIntent);
                finish();

            }
        });

        AccountManager manager = (AccountManager) getSystemService(ACCOUNT_SERVICE);

        if (ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.GET_ACCOUNTS) != PackageManager.PERMISSION_GRANTED) {
            Log.v(LOGTAG, "permission READ_PHONE_STATE denied");
            // Should we show an explanation?
            if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                    Manifest.permission.GET_ACCOUNTS)) {
                Log.v(LOGTAG, "show permission rationale");
                final Activity thisActivity = this;
                new AlertDialog.Builder(this)
                        .setTitle( getString( R.string.dialog_permission_request_title))
                        .setMessage(getString( R.string.dialog_permission_request_GET_ACCOUNT))
                        .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                ActivityCompat.requestPermissions( thisActivity,
                                        new String[]{Manifest.permission.GET_ACCOUNTS},
                                        PERMISSIONS_REQUEST_GET_ACCOUNT);
                            }
                        }).show();
            } else {
                Log.i(LOGTAG, "request permission READ_PHONE_STATE");
                // No explanation needed, we can request the permission.
                ActivityCompat.requestPermissions(this,
                        new String[]{Manifest.permission.GET_ACCOUNTS},
                        PERMISSIONS_REQUEST_GET_ACCOUNT);
            }
        }

        Log.d(LOGTAG, "permission granted");

        accounts = new ArrayList<>();

        Account[] list = manager.getAccounts();
        for ( Account account : list) {
            if ( accounts.contains( account.name) ) {
                continue;
            }

            // TODO check for right type
            accounts.add(account.name);
        }

        if ( accounts.size() == 0 ) {
            accounts.add(getString(R.string.no_accounts_found));
        }

        ArrayAdapter<String> accountAdapter =
                new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, accounts);

        ListView accountList = (ListView) findViewById(R.id.accounts);
        accountList.setAdapter(accountAdapter);
    }
}

@@ -424,38 +424,8 @@
 ty;%0A
-import android.util.ArraySet;%0A
 impo
@@ -610,83 +610,40 @@
 ort 
-java.util.ArrayList;%0Aimport java.util.Collection;%0Aimport java.util.HashSet;
+com.google.common.collect.Maps;%0A
 %0Aimp
@@ -656,16 +656,21 @@
 va.util.
+Array
 List;%0Aim
@@ -688,120 +688,11 @@
 til.
-Set;%0A%0Aimport static android.content.Context.ACCOUNT_SERVICE;%0Aimport static li.doerf.hackedimporter.R.id.accounts
+Map
 ;%0A%0Ap
@@ -913,16 +913,783 @@
 ccounts;
+%0A    private static Map%3CString,String%3E myRecognizedAcocunts;%0A%0A    static %7B%0A        myRecognizedAcocunts = Maps.newHashMap();%0A        myRecognizedAcocunts.put( %22com.google%22, %22Google%22);%0A        myRecognizedAcocunts.put( %22com.facebook.auth.login%22, %22Facebook%22);%0A        myRecognizedAcocunts.put( %22com.twitter.android.auth.login%22, %22Twitter%22);%0A        myRecognizedAcocunts.put( %22com.linkedin.android%22, %22Linkedin%22);%0A        myRecognizedAcocunts.put( %22com.reddit.account%22, %22Reddit%22);%0A        myRecognizedAcocunts.put( %22com.booking.dcl%22, %22Booking%22);%0A        myRecognizedAcocunts.put( %22com.tripadvisor.tripadvisor%22, %22Tripadvisor%22);%0A        myRecognizedAcocunts.put( %22com.evernote%22, %22Evernote%22);%0A        myRecognizedAcocunts.put( %22com.getpebble.android.basalt%22, %22Pebble%22);%0A    %7D
 %0A%0A    @O
@@ -4383,24 +4383,113 @@
 t : list) %7B%0A
+            Log.d(LOGTAG, %22type: %22 + account.type + %22 - account name: %22 + account.name);%0A
             
@@ -4587,36 +4587,240 @@
     
-// TODO check for right 
+if ( ! myRecognizedAcocunts.containsKey(account.type) ) %7B%0A                Log.i( LOGTAG, %22unrecognized type: %22 + account.type);%0A                continue;%0A            %7D%0A%0A            Log.i( LOGTAG, %22recognized type: %22 + account.
 type
+);
 %0A   

check type of account