package fr.free.nrw.commons.upload;


import android.content.Context;
import android.content.DialogInterface;
import android.os.AsyncTask;
import android.util.Log;

import org.mediawiki.api.ApiResult;
import org.mediawiki.api.MWApi;

import java.io.IOException;
import java.util.ArrayList;

import android.support.v7.app.AlertDialog;

import fr.free.nrw.commons.CommonsApplication;

/**
 * Sends asynchronous queries to the Commons MediaWiki API to check that file doesn't already exist
 * Returns true if file exists, false if it doesn't
 */
public class ExistingFileAsync extends AsyncTask<Void, Void, Boolean> {

    private static final String TAG = fr.free.nrw.commons.upload.ExistingFileAsync.class.getName();

    private String fileSHA1;
    private Context context;

    private AlertDialog alertDialog;

    public ExistingFileAsync(String fileSHA1, Context context) {
        super();
        this.fileSHA1 = fileSHA1;
        this.context = context;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        alertDialog = new AlertDialog.Builder(ExistingFileAsync.this);
    }

    @Override
    protected Boolean doInBackground(Void... voids) {
        MWApi api = CommonsApplication.createMWApi();
        ApiResult result;

        // https://commons.wikimedia.org/w/api.php?action=query&list=allimages&format=xml&aisha1=801957214aba50cb63bb6eb1b0effa50188900ba
        try {
            result = api.action("query")
                    .param("format", "xml")
                    .param("list", "allimages")
                    .param("aisha1", fileSHA1)
                    .get();
            Log.d(TAG, "Searching Commons API for existing file: " + result.toString());
        } catch (IOException e) {
            Log.e(TAG, "IO Exception: ", e);
            return false;
        }

        ArrayList<ApiResult> resultNodes = result.getNodes("/api/query/allimages/img");
        Log.d(TAG, "Result nodes: " + resultNodes);

        boolean fileExists;
        if (!resultNodes.isEmpty()) {
            fileExists = true;
        } else {
            fileExists = false;
        }

        Log.d(TAG, "File already exists in Commons:" + fileExists);
        return fileExists;
    }

    @Override
    protected void onPostExecute(Boolean fileExists) {
        super.onPostExecute(fileExists);
        //TODO: Add Dialog here to tell user file exists, do you want to continue? Yes/No

        alertDialog.setTitle("The Process");
        alertDialog.setIcon(R.drawable.success);
        alertDialog.setCanceledOnTouchOutside(false);
        alertDialog.setMessage("All done!");
        alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        Intent A = new Intent(DownloadActivity.this, Menu_activity.class);
                        startActivity(A);
                        finish();
                    }
                });
        alertDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                Intent A = new Intent(DownloadActivity.this, Menu_activity.class);
                startActivity(A);
                finish();
            }
        });
        alertDialog.show();
    }
}
@@ -792,16 +792,24 @@
 rtDialog
+.Builder
  alertDi
@@ -1100,30 +1100,15 @@
 der(
-ExistingFileAsync.this
+context
 );%0A 
@@ -2455,76 +2455,22 @@
 .set
-Title(%22The Process%22);%0A        alertDialog.setIcon(R.drawable.success
+Message(%22test%22
 );%0A 
@@ -2501,23 +2501,11 @@
 ncel
-edOnTouchOutsid
+abl
 e(fa
@@ -2537,109 +2537,28 @@
 .set
-Message(%22All done!%22);%0A        alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, %22OK%22,%0A               
+PositiveButton(%22ok%22,
  new
@@ -2602,32 +2602,24 @@
             
-        
 public void 
@@ -2658,13 +2658,10 @@
 int 
-which
+id
 ) %7B%0A
@@ -2680,196 +2680,47 @@
     
-        Intent A = new Intent(DownloadActivity.this, Menu_activity.class);%0A                        startActivity(A);%0A                        finish();%0A                    %7D%0A       
+dialog.cancel();%0A            %7D%0A
         
  %7D);
@@ -2711,25 +2711,24 @@
   %7D%0A        
- 
 %7D);%0A        
@@ -2719,33 +2719,33 @@
     %7D);%0A        
-a
+A
 lertDialog.setOn
@@ -2742,342 +2742,62 @@
 alog
-.setOnDismissListener(new DialogInterface.OnDismissListener() %7B%0A            @Override%0A            public void onDismiss(DialogInterface dialog) %7B%0A                Intent A = new Intent(DownloadActivity.this, Menu_activity.class);%0A                startActivity(A);%0A                finish();%0A            %7D%0A        %7D);%0A        alertDialog
+ alertLogin = alertDialog.create();%0A        alertLogin
 .sho

Add base code for AlertDialog