package de.danoeh.antennapod.asynctask;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.util.Log;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;

import de.danoeh.antennapod.core.R;
import de.danoeh.antennapod.core.opml.OpmlWriter;
import de.danoeh.antennapod.core.preferences.UserPreferences;
import de.danoeh.antennapod.core.storage.DBReader;
import de.danoeh.antennapod.core.util.LangUtils;

/**
 * Writes an OPML file into the export directory in the background.
 */
public class OpmlExportWorker extends AsyncTask<Void, Void, Void> {
    private static final String TAG = "OpmlExportWorker";
    private static final String DEFAULT_OUTPUT_NAME = "antennapod-feeds.opml";
    public static final String EXPORT_DIR = "export/";

    private Context context;
    private File output;

    private ProgressDialog progDialog;
    private Exception exception;

    public OpmlExportWorker(Context context, File output) {
        this.context = context;
        this.output = output;
    }

    public OpmlExportWorker(Context context) {
        this.context = context;
    }

    @Override
    protected Void doInBackground(Void... params) {
        OpmlWriter opmlWriter = new OpmlWriter();
        if (output == null) {
            output = new File(
                    UserPreferences.getDataFolder(context, EXPORT_DIR),
                    DEFAULT_OUTPUT_NAME);
            if (output.exists()) {
                Log.w(TAG, "Overwriting previously exported file.");
                output.delete();
            }
        }
        OutputStreamWriter writer = null;
        try {
            writer = new OutputStreamWriter(new FileOutputStream(output), LangUtils.UTF_8);
            opmlWriter.writeDocument(DBReader.getFeedList(context), writer);
        } catch (IOException e) {
            e.printStackTrace();
            exception = e;
        } finally {
            if (writer != null) {
                try {
                    writer.close();
                } catch (IOException ioe) {
                    exception = ioe;
                }
            }
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        progDialog.dismiss();
        if (exception != null) {
            AlertDialog.Builder alert = new AlertDialog.Builder(context)
                    .setNeutralButton(android.R.string.ok,
                            (dialog, which) -> {
                                dialog.dismiss();
                            });
            alert.setTitle(R.string.export_error_label);
            alert.setMessage(exception.getMessage());
            alert.create().show();
            return;
        }

        Uri outputUri = Uri.fromFile(output);
        Intent sendIntent = new Intent(Intent.ACTION_SEND);
        sendIntent.putExtra(Intent.EXTRA_SUBJECT, "OPML Export");
        sendIntent.putExtra(Intent.EXTRA_STREAM, outputUri);
        sendIntent.setType("text/plain");
        context.startActivity(Intent.createChooser(sendIntent, context.getResources().getText(R.string.share_label)));
    }

    @Override
    protected void onPreExecute() {
        progDialog = new ProgressDialog(context);
        progDialog.setMessage(context.getString(R.string.exporting_label));
        progDialog.setIndeterminate(true);
        progDialog.show();
    }

    @SuppressLint("NewApi")
    public void executeAsync() {
        if (android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.GINGERBREAD_MR1) {
            executeOnExecutor(THREAD_POOL_EXECUTOR);
        } else {
            execute();
        }
    }

}

@@ -2500,45 +2500,8 @@
 ();%0A
-        if (exception != null) %7B%0A    
     
@@ -2565,20 +2565,16 @@
 ontext)%0A
-    
         
@@ -2648,30 +2648,217 @@
     
-    (dialog,
+new DialogInterface.OnClickListener() %7B%0A%0A                            @Override%0A                            public void onClick(DialogInterface dialog,%0A                                                int
  which)
- -%3E
  %7B%0A 
@@ -2935,18 +2935,77 @@
        %7D
-);
+%0A                        %7D);%0A        if (exception != null) %7B
 %0A       
@@ -3120,34 +3120,223 @@
     
-    alert.create().show();
+%7D else %7B%0A            alert.setTitle(R.string.opml_export_success_title);%0A            alert.setMessage(context%0A                    .getString(R.string.opml_export_success_sum)%0A                    + output.toString())
 %0A   
@@ -3348,35 +3348,211 @@
     
-return;%0A        %7D%0A%0A        
+        .setPositiveButton(R.string.share_label, new DialogInterface.OnClickListener() %7B%0A                        @Override%0A                        public void onClick(DialogInterface dialog, int which) %7B
 Uri 
@@ -3581,24 +3581,44 @@
 le(output);%0A
+                    
         Inte
@@ -3665,16 +3665,36 @@
 _SEND);%0A
+                    
         
@@ -3747,24 +3747,44 @@
 L Export%22);%0A
+                    
         send
@@ -3832,16 +3832,36 @@
 utUri);%0A
+                    
         
@@ -3890,24 +3890,44 @@
 xt/plain%22);%0A
+                    
         cont
@@ -4029,24 +4029,115 @@
 e_label)));%0A
+                        %7D%0A                    %7D);%0A        %7D%0A        alert.create().show();%0A
     %7D%0A%0A    @

make share just an option of the old dialog