package com.filemanager.dialogs;

import imoblife.android.os.ModernAsyncTask;

import java.io.File;


import com.filemanager.R;
import com.filemanager.files.FileHolder;
import com.filemanager.lists.FileListFragment;
import com.filemanager.util.MediaScannerUtils;
import com.filemanager.util.UIUtils;
import com.intents.FileManagerIntents;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.widget.Toast;

public class SingleDeleteDialog extends DialogFragment {
	private FileHolder mFileHolder;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		mFileHolder = getArguments().getParcelable(FileManagerIntents.EXTRA_DIALOG_FILE_HOLDER);
	}
	
	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		return new AlertDialog.Builder(getActivity())
				.setInverseBackgroundForced(UIUtils.shouldDialogInverseBackground(getActivity()))
				.setTitle(getString(R.string.really_delete, mFileHolder.getName()))
				.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
							new RecursiveDeleteTask().execute(mFileHolder.getFile());
					}
				})
				.setIcon(mFileHolder.getIcon())
				.setNegativeButton(android.R.string.cancel, null)
				.create();
	}
	
	private class RecursiveDeleteTask extends ModernAsyncTask<File, Void, Void> {
		/**
		 * If 0 some failed, if 1 all succeeded. 
		 */
		private int mResult = 1;
		private ProgressDialog dialog = new ProgressDialog(getActivity());

		/**
		 * Recursively delete a file or directory and all of its children.
		 * 
		 * @returns 0 if successful, error value otherwise.
		 */
		private void recursiveDelete(File file) {
			File[] files = file.listFiles();
			if (files != null && files.length != 0) 
				// If it's a directory delete all children.
				for (File childFile : files) {
					if (childFile.isDirectory()) {
						recursiveDelete(childFile);
					} else {
						mResult *= childFile.delete() ? 1 : 0;
					}
				}
				
				// And then delete parent. -- or just delete the file.
				mResult *= file.delete() ? 1 : 0;
		}
		
		@Override
		protected void onPreExecute() {		
			dialog.setMessage(getActivity().getString(R.string.deleting));
			dialog.setIndeterminate(true);
			dialog.show();
		}
		
		@Override
		protected Void doInBackground(File... params) {
			recursiveDelete(params[0]);
			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			try {
				Toast.makeText(dialog.getContext(), mResult == 0 ? R.string.delete_failure : R.string.delete_success, Toast.LENGTH_LONG).show();
				((FileListFragment) getTargetFragment()).refresh();
				dialog.dismiss();
				
				MediaScannerUtils.informFileDeleted(getTargetFragment().getActivity().getApplicationContext(), mFileHolder.getFile());
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	public interface OnDeleteListener{
		public void deleted();
	}
}
@@ -932,22 +932,41 @@
 te) %7B%0A%09%09
-return
+AlertDialog.Builder adb =
  new Ale
@@ -996,21 +996,32 @@
 ivity())
+;
 %0A%09%09
-%09%09
+try %7B%0A%09%09%09adb
 .setInve
@@ -1093,25 +1093,24 @@
 vity()))%0A%09%09%09
-%09
 .setTitle(ge
@@ -1165,17 +1165,16 @@
 ame()))%0A
-%09
 %09%09%09.setP
@@ -1252,17 +1252,16 @@
 ) %7B%0A%09%09%09%09
-%09
 @Overrid
@@ -1262,17 +1262,16 @@
 verride%0A
-%09
 %09%09%09%09publ
@@ -1328,18 +1328,16 @@
  %7B%0A%09%09%09%09%09
-%09%09
 new Recu
@@ -1386,17 +1386,16 @@
 ile());%0A
-%09
 %09%09%09%09%7D%0A%09%09
@@ -1399,13 +1399,11 @@
 %0A%09%09%09
-%09
 %7D)%0A
-%09
 %09%09%09.
@@ -1433,17 +1433,16 @@
 Icon())%0A
-%09
 %09%09%09.setN
@@ -1485,21 +1485,84 @@
 l, null)
+;
 %0A%09%09
-%09%09
+%7D catch (Exception e) %7B%0A%09%09%09e.printStackTrace();%0A%09%09%7D%0A%09%09return adb
 .create(

handle java.util.FormatFlagsConversionMismatchException: %s does not support ' '