/* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*-
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package org.mozilla.focus.broadcastreceiver;

import android.app.DownloadManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.support.design.widget.Snackbar;
import android.support.v4.content.ContextCompat;
import android.support.v4.content.FileProvider;
import android.view.View;
import android.webkit.MimeTypeMap;
import android.webkit.URLUtil;

import org.mozilla.focus.BuildConfig;
import org.mozilla.focus.R;
import org.mozilla.focus.utils.IntentUtils;

import java.io.File;
import java.util.HashSet;

/**
 * BroadcastReceiver for finished downloads
 */
public class DownloadBroadcastReceiver extends BroadcastReceiver {
    private static final String FILE_SCHEME = "file://";
    private static final String FILE_PROVIDER_EXTENSION = ".fileprovider";

    private final HashSet<Long> queuedDownloadReferences = new HashSet<>();
    private final View browserContainer;
    private final DownloadManager downloadManager;

    public DownloadBroadcastReceiver(View view, DownloadManager downloadManager) {
        this.browserContainer = view;
        this.downloadManager = downloadManager;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        final long downloadReference = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
        displaySnackbar(context, downloadReference, downloadManager);
    }

    private void displaySnackbar(final Context context, long completedDownloadReference, DownloadManager downloadManager) {
        if (!isFocusDownload(completedDownloadReference)) {
            return;
        }

        final DownloadManager.Query query = new DownloadManager.Query();
        query.setFilterById(completedDownloadReference);
        try (Cursor cursor = downloadManager.query(query)) {
            if (cursor.moveToFirst()) {
                int statusColumnIndex = cursor.getColumnIndex(DownloadManager.COLUMN_STATUS);
                if (DownloadManager.STATUS_SUCCESSFUL == cursor.getInt(statusColumnIndex)) {
                    String uriString = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));

                    final String localUri = uriString.startsWith(FILE_SCHEME) ? uriString.substring(FILE_SCHEME.length()) : uriString;
                    final String fileExtension = MimeTypeMap.getFileExtensionFromUrl(localUri);
                    final String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(fileExtension);
                    final String fileName = URLUtil.guessFileName(Uri.decode(localUri), null, mimeType);

                    final File file = new File(Uri.decode(localUri));
                    final Uri uriForFile = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + FILE_PROVIDER_EXTENSION, file);
                    final Intent openFileIntent = IntentUtils.INSTANCE.createOpenFileIntent(uriForFile, mimeType);
                    showSnackbarForFilename(openFileIntent, context, fileName);
                }
            }
        }
        removeFromHashSet(completedDownloadReference);
    }

    private void showSnackbarForFilename(final Intent openFileIntent, final Context context, String fileName) {
        final Snackbar snackbar = Snackbar
                .make(browserContainer, String.format(context.getString(R.string.download_snackbar_finished), fileName), Snackbar.LENGTH_LONG);
        if (IntentUtils.INSTANCE.activitiesFoundForIntent(context, openFileIntent)) {
            snackbar.setAction(context.getString(R.string.download_snackbar_open), new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    context.startActivity(openFileIntent);
                }
            });
            snackbar.setActionTextColor(ContextCompat.getColor(context, R.color.snackbarActionText));
        }
        snackbar.show();
    }

    private boolean isFocusDownload(long completedDownloadReference) {
        return (queuedDownloadReferences.contains(completedDownloadReference));
    }

    private void removeFromHashSet(long completedDownloadReference) {
        queuedDownloadReferences.remove(completedDownloadReference);
    }

    public void addQueuedDownload(long referenceId) {
        queuedDownloadReferences.add(referenceId);
    }
}

@@ -695,72 +695,62 @@
 ew;%0A
+%0A
 import 
-android.webkit.MimeTypeMap;%0Aimport android.webkit.URL
+mozilla.components.support.utils.Download
 Util
+s
 ;%0A%0Ai
@@ -2674,68 +2674,88 @@
 ing 
-fileExtension = MimeTypeMap.getFileExtensionFromUrl(localUri
+decoded = Uri.decode(localUri);%0A                    File file = new File(decoded
 );%0A 
@@ -2801,73 +2801,169 @@
 e = 
-MimeTypeMap
+cursor
 .getS
+tr
 ing
-leton().getMimeTypeFromExtension(fileExtension
+(cursor.getColumnIndex(DownloadManager.COLUMN_MEDIA_TYPE));%0A                    String fileName = DownloadUtils.guessFileName(null, decoded, null
 );%0A 
@@ -3006,18 +3006,25 @@
 Name
+2
  = 
-URL
+Download
 Util
+s
 .gue
@@ -3038,48 +3038,218 @@
 ame(
-Uri.decode(localUri), null, mimeType);%0A%0A
+null, decoded, mimeType);%0A                    // Rename the file extension if it lacks a known MIME type and the server provided a Content-Type header.%0A                    if (!fileName.equals(fileName2)) %7B%0A   
     
@@ -3260,24 +3260,25 @@
             
+ 
 final File f
@@ -3272,32 +3272,33 @@
  final File file
+2
  = new File(Uri.
@@ -3297,30 +3297,230 @@
 ile(
-Uri.decode(localUri));
+file.getParent(), fileName2);%0A                        if (file.renameTo(file2)) %7B%0A                            file = file2;%0A                            fileName = fileName2;%0A                        %7D%0A                    %7D%0A
 %0A   

Use media type if downloaded file lacks extension