/*
 * Copyright (c) 2019
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.acra.dialog;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import org.acra.ACRA;
import org.acra.config.CoreConfiguration;
import org.acra.data.CrashReportData;
import org.acra.file.BulkReportDeleter;
import org.acra.file.CrashReportPersister;
import org.acra.interaction.DialogInteraction;
import org.acra.scheduler.SchedulerStarter;
import org.json.JSONException;

import java.io.File;
import java.io.IOException;
import java.io.Serializable;

import static org.acra.ACRA.LOG_TAG;
import static org.acra.ReportField.USER_COMMENT;
import static org.acra.ReportField.USER_EMAIL;

/**
 * @author f43nd1r
 * @since 5.4.0
 */
public class CrashReportDialogHelper {

    private final File reportFile;
    private final CoreConfiguration config;
    private final Context context;

    /**
     * Call this in your {@link android.app.Activity#onCreate(Bundle)}.
     * The intent must contain two extras:
     * <ol>
     * <li>{@link DialogInteraction#EXTRA_REPORT_FILE}</li>
     * <li>{@link DialogInteraction#EXTRA_REPORT_CONFIG}</li>
     * </ol>
     *
     * @param context a context
     * @param intent  the intent which started this activity
     * @throws IllegalArgumentException if the intent cannot be parsed or does not contain the correct data
     */
    public CrashReportDialogHelper(@NonNull Context context, @NonNull Intent intent) throws IllegalArgumentException {
        this.context = context;
        final Serializable sConfig = intent.getSerializableExtra(DialogInteraction.EXTRA_REPORT_CONFIG);
        final Serializable sReportFile = intent.getSerializableExtra(DialogInteraction.EXTRA_REPORT_FILE);

        if ((sConfig instanceof CoreConfiguration) && (sReportFile instanceof File)) {
            config = (CoreConfiguration) sConfig;
            reportFile = (File) sReportFile;
        } else {
            ACRA.log.w(LOG_TAG, "Illegal or incomplete call of " + getClass().getSimpleName());
            throw new IllegalArgumentException();
        }
    }


    /**
     * Cancel any pending crash reports.
     */
    public void cancelReports() {
        new Thread(() -> new BulkReportDeleter(context).deleteReports(false, 0)).start();
    }


    /**
     * Send crash report given user's comment and email address.
     *
     * @param comment   Comment (may be null) provided by the user.
     * @param userEmail Email address (may be null) provided by the user.
     */
    public void sendCrash(@Nullable String comment, @Nullable String userEmail) {
        new Thread(() -> {
            final CrashReportPersister persister = new CrashReportPersister();
            try {
                if (ACRA.DEV_LOGGING) ACRA.log.d(LOG_TAG, "Add user comment to " + reportFile);
                final CrashReportData crashData = persister.load(reportFile);
                crashData.put(USER_COMMENT, comment == null ? "" : comment);
                crashData.put(USER_EMAIL, userEmail == null ? "" : userEmail);
                persister.store(crashData, reportFile);
            } catch (IOException | JSONException e) {
                ACRA.log.w(LOG_TAG, "User comment not added: ", e);
            }

            // Start the report sending task
            new SchedulerStarter(context, config).scheduleReports(reportFile, false);
        }).start();
    }

    /**
     * Provides the configuration
     *
     * @return the main config
     */
    public CoreConfiguration getConfig() {
        return config;
    }
}

@@ -779,24 +779,72 @@
 n.Nullable;%0A
+import android.support.annotation.WorkerThread;%0A
 import org.a
@@ -853,16 +853,16 @@
 a.ACRA;%0A
-
 import o
@@ -1548,16 +1548,56 @@
 context;
+%0A    private CrashReportData reportData;
 %0A%0A    /*
@@ -2794,32 +2794,551 @@
        %7D%0A    %7D%0A%0A
+    /**%0A     * loads the current report data%0A     *%0A     * @return report data%0A     * @throws IOException if there was a problem with the report file%0A     */%0A    @WorkerThread%0A    @NonNull%0A    public CrashReportData getReportData() throws IOException %7B%0A        if (reportData == null) %7B%0A            try %7B%0A                reportData = new CrashReportPersister().load(reportFile);%0A            %7D catch (JSONException e) %7B%0A                throw new IOException(e);%0A            %7D%0A        %7D%0A        return reportData;%0A    %7D%0A%0A
 %0A    /**%0A     * 
@@ -3854,87 +3854,8 @@
 %3E %7B%0A
-            final CrashReportPersister persister = new CrashReportPersister();%0A
     
@@ -4018,33 +4018,22 @@
 a = 
-persister.load(reportFile
+getReportData(
 );%0A 
@@ -4207,17 +4207,32 @@
     
-p
+new CrashReportP
 ersister
 .sto
@@ -4227,16 +4227,18 @@
 ersister
+()
 .store(c

add getter for reportdata in dialoghelper