package fr.free.nrw.commons.upload;


import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
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;
import fr.free.nrw.commons.contributions.ContributionsActivity;

/**
 * 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;

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

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @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.Builder builder = new AlertDialog.Builder(context);

        builder.setMessage("This file already exists in Commons. Are you sure you want to proceed?")
                .setTitle("Warning");
        builder.setPositiveButton("No", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                //Go back to ContributionsActivity
                Intent intent = new Intent(context, ContributionsActivity.class);
                context.startActivity(intent);
            }

        });
        builder.setNegativeButton("Yes", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                //No need to do anything, user remains on upload screen
            }
        });

        AlertDialog dialog = builder.create();
        dialog.show();
    }
}
@@ -2421,16 +2421,47 @@
 Yes/No%0A%0A
+        if (fileExists) %7B%0A%0A    
         
@@ -2521,24 +2521,28 @@
 (context);%0A%0A
+    
         buil
@@ -2646,16 +2646,20 @@
         
+    
 .setTitl
@@ -2672,32 +2672,36 @@
 ning%22);%0A        
+ 
+   
 builder.setPosit
@@ -2748,32 +2748,36 @@
 ickListener() %7B%0A
+    
             publ
@@ -2842,16 +2842,20 @@
         
+    
 //Go bac
@@ -2897,16 +2897,20 @@
         
+  
+  
 Intent i
@@ -2983,16 +2983,20 @@
         
+    
 context.
@@ -3030,27 +3030,34 @@
         
-%7D%0A%0A
+    %7D%0A%0A   
         
 %7D);%0A    
@@ -3048,20 +3048,25 @@
         
+ 
 %7D);%0A
+    
         
@@ -3142,32 +3142,36 @@
 ) %7B%0A            
+    
 public void onCl
@@ -3200,32 +3200,36 @@
 alog, int id) %7B%0A
+    
                 
@@ -3300,18 +3300,24 @@
     
-%7D%0A
+    %7D%0A  
         
 %7D);%0A
@@ -3316,13 +3316,19 @@
     
+  
 %7D);%0A%0A
+    
     
@@ -3378,16 +3378,20 @@
         
+    
 dialog.s
@@ -3393,19 +3393,29 @@
 log.show();%0A
+        %7D%0A
     %7D%0A%7D

Add 'if' check for file exists