package li.doerf.hacked.ui;

import android.app.Dialog;
import android.content.DialogInterface;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.DialogFragment;
import li.doerf.hacked.HackedApplication;
import li.doerf.hacked.R;
import li.doerf.hacked.db.entities.Account;
import li.doerf.hacked.remote.haveibeenpwned.HIBPCheckAccountAsyncTask;
import li.doerf.hacked.utils.AccountHelper;
import li.doerf.hacked.utils.ConnectivityHelper;

/**
 * The fragment used to add new numbers.
 *
 * Created by moo on 30/11/15.
 */
public class AddAccountDialogFragment extends DialogFragment {
    private final String LOGTAG = getClass().getSimpleName();
    private String myName;

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        // Get the layout inflater
        LayoutInflater inflater = getActivity().getLayoutInflater();

        // Inflate and set the layout for the dialog
        // Pass null as the parent view because its going in the dialog layout
        final View view = inflater.inflate(R.layout.dialog_add_account, null);

        builder.setView(view)
                // Add action buttons
                .setPositiveButton(R.string.add, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int id) {
                        myName = ((EditText) view.findViewById(R.id.account)).getText().toString();
                        addAccount(myName);
                    }
                })
                .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        myName = null;
                        AddAccountDialogFragment.this.getDialog().cancel();
                    }
                });

        AlertDialog dialog = builder.create();
        dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
        return dialog;
    }


    private void addAccount(String aName) {
        if (aName == null || aName.trim().equals("")) {
            Toast.makeText(getContext(), getString(R.string.toast_enter_valid_name), Toast.LENGTH_LONG).show();
            Log.w(LOGTAG, "account name not valid");
            return;
        }

        final String name = aName.trim();

        // TODO use some other construct here
        new AsyncTask<Void, Void, Account>() {

            @Override
            protected Account doInBackground(Void... voids) {
                Account account = AccountHelper.createAccount(getContext(), name);
                if (account == null) {
                    return null;
                }

                return account;
            }

            @Override
            protected void onPostExecute(Account account) {
                ((HackedApplication) getActivity().getApplication()).trackEvent("AddAccount");

                if ( ! ConnectivityHelper.isConnected( getContext()) ) {
                    Log.i(LOGTAG, "no network");
                    return;
                }

                new HIBPCheckAccountAsyncTask(getContext(), null).execute(account.getId());
            }
        }.execute();

    }


}

@@ -22,16 +22,48 @@
 ed.ui;%0A%0A
+import android.app.Application;%0A
 import a
@@ -2779,57 +2779,129 @@
     
-// TODO use some other construct here
+new AddAcountAsyncTask(getActivity().getApplication()).execute(name);%0A
 %0A    
+%7D%0A%0A
     
-new
+static class AddAcountAsyncTask extends
  Asy
@@ -2907,20 +2907,22 @@
 yncTask%3C
-Void
+String
 , Void, 
@@ -2933,18 +2933,16 @@
 unt%3E
-()
  %7B%0A%0A    
     
@@ -2933,24 +2933,168 @@
 unt%3E %7B%0A%0A    
+    private final Application myContext;%0A%0A        AddAcountAsyncTask(Application application) %7B%0A            myContext = application;%0A        %7D%0A%0A
         @Ove
@@ -3099,36 +3099,32 @@
 verride%0A        
-    
 protected Accoun
@@ -3144,29 +3144,26 @@
 und(
-Void... voids) %7B%0A    
+String... name) %7B%0A
     
@@ -3220,33 +3220,29 @@
 unt(
-get
+my
 Context
-()
 , name
+%5B0%5D
 );%0A
-    
     
@@ -3268,28 +3268,24 @@
  == null) %7B%0A
-    
             
@@ -3309,35 +3309,27 @@
             
-    %7D%0A%0A    
+%7D%0A%0A
             
@@ -3344,36 +3344,32 @@
 ccount;%0A        
-    
 %7D%0A%0A            @
@@ -3355,36 +3355,32 @@
      %7D%0A%0A        
-  
-  
 @Override%0A      
@@ -3365,36 +3365,32 @@
       @Override%0A
-    
         protecte
@@ -3441,20 +3441,16 @@
         
-    
 ((Hacked
@@ -3466,38 +3466,17 @@
 on) 
-getActivity().getApplication()
+myContext
 ).tr
@@ -3508,28 +3508,24 @@
             
-    
 if ( ! Conne
@@ -3551,28 +3551,25 @@
 nected( 
-get
+my
 Context
-()
 ) ) %7B%0A  
@@ -3586,24 +3586,34 @@
     
-   
- 
 Log.i(
-LOGTAG
+%22AddAcountAsyncTask%22
 , %22n
@@ -3633,36 +3633,32 @@
                 
-   
- 
 return;%0A        
@@ -3657,35 +3657,27 @@
             
-    %7D%0A%0A    
+%7D%0A%0A
             
@@ -3706,28 +3706,25 @@
 yncTask(
-get
+my
 Context
-()
 , null).
@@ -3761,35 +3761,9 @@
     
-    %7D%0A        %7D.execute();%0A
+%7D
 %0A   

refactor code