/*
 * 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 com.nononsenseapps.filepicker;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.os.FileObserver;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.core.content.FileProvider;
import androidx.loader.content.AsyncTaskLoader;
import androidx.loader.content.Loader;
import androidx.recyclerview.widget.SortedList;
import androidx.recyclerview.widget.SortedListAdapterCallback;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.citra.emu.R;

/**
 * An implementation of the picker which allows you to select a file from the internal/external
 * storage (SD-card) on a device.
 */
public class FilePickerFragment extends AbstractFilePickerFragment<File> {

    protected static final int PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE = 1;
    protected boolean showHiddenItems = false;
    private File mRequestedPath = null;

    public FilePickerFragment() {}

    /**
     * This method is used to dictate whether hidden files and folders should be shown or not
     *
     * @param showHiddenItems whether hidden items should be shown or not
     */
    public void showHiddenItems(boolean showHiddenItems) {
        this.showHiddenItems = showHiddenItems;
    }

    /**
     * Returns if hidden items are shown or not
     *
     * @return true if hidden items are shown, otherwise false
     */

    public boolean areHiddenItemsShown() {
        return showHiddenItems;
    }

    /**
     * @return true if app has been granted permission to write to the SD-card.
     */
    @Override
    protected boolean hasPermission(@NonNull File path) {
        return PackageManager.PERMISSION_GRANTED ==
            ContextCompat.checkSelfPermission(getContext(),
                                              Manifest.permission.WRITE_EXTERNAL_STORAGE);
    }

    /**
     * Request permission to write to the SD-card.
     */
    @Override
    protected void handlePermission(@NonNull File path) {
        //         Should we show an explanation?
        //        if (shouldShowRequestPermissionRationale(
        //                Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
        //             Explain to the user why we need permission
        //        }

        mRequestedPath = path;
        requestPermissions(new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE},
                           PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE);
    }

    /**
     * This the method that gets notified when permission is granted/denied. By default,
     * a granted request will result in a refresh of the list.
     *
     * @param requestCode  the code you requested
     * @param permissions  array of permissions you requested. empty if process was cancelled.
     * @param grantResults results for requests. empty if process was cancelled.
     */
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                           @NonNull int[] grantResults) {
        // If arrays are empty, then process was cancelled
        if (permissions.length == 0) {
            // Treat this as a cancel press
            if (mListener != null) {
                mListener.onCancelled();
            }
        } else { // if (requestCode == PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE) {
            if (PackageManager.PERMISSION_GRANTED == grantResults[0]) {
                // Do refresh
                if (mRequestedPath != null) {
                    refresh(mRequestedPath);
                }
            } else {
                Toast
                    .makeText(getContext(), R.string.nnf_permission_external_write_denied,
                              Toast.LENGTH_SHORT)
                    .show();
                // Treat this as a cancel press
                if (mListener != null) {
                    mListener.onCancelled();
                }
            }
        }
    }

    /**
     * Return true if the path is a directory and not a file.
     *
     * @param path either a file or directory
     * @return true if path is a directory, false if file
     */
    @Override
    public boolean isDir(@NonNull final File path) {
        return path.isDirectory();
    }

    /**
     * @param path either a file or directory
     * @return filename of path
     */
    @NonNull
    @Override
    public String getName(@NonNull File path) {
        return path.getName();
    }

    /**
     * Return the path to the parent directory. Should return the root if
     * from is root.
     *
     * @param from either a file or directory
     * @return the parent directory
     */
    @NonNull
    @Override
    public File getParent(@NonNull final File from) {
        if (from.getPath().equals(getRoot().getPath())) {
            // Already at root, we can't go higher
            return from;
        } else if (from.getParentFile() != null) {
            return from.getParentFile();
        } else {
            return from;
        }
    }

    /**
     * Convert the path to the type used.
     *
     * @param path either a file or directory
     * @return File representation of the string path
     */
    @NonNull
    @Override
    public File getPath(@NonNull final String path) {
        return new File(path);
    }

    /**
     * @param path either a file or directory
     * @return the full path to the file
     */
    @NonNull
    @Override
    public String getFullPath(@NonNull final File path) {
        return path.getPath();
    }

    /**
     * Get the root path.
     *
     * @return the highest allowed path, which is "/" by default
     */
    @NonNull
    @Override
    public File getRoot() {
        return new File("/");
    }

    /**
     * Convert the path to a URI for the return intent
     *
     * @param file either a file or directory
     * @return a Uri
     */
    @NonNull
    @Override
    public Uri toUri(@NonNull final File file) {
        return FileProvider.getUriForFile(
            getContext(), getContext().getApplicationContext().getPackageName() + ".provider",
            file);
    }

    /**
     * Get a loader that lists the Files in the current path,
     * and monitors changes.
     */
    @NonNull
    @Override
    public Loader<SortedList<File>> getLoader() {
        return new AsyncTaskLoader<SortedList<File>>(getActivity()) {
            FileObserver fileObserver;

            @Override
            public SortedList<File> loadInBackground() {
                File[] listFiles = mCurrentPath.listFiles();
                if (listFiles == null && mStartPath != null) {
                    String path = mCurrentPath.getPath();
                    if (mStartPath.startsWith(path)) {
                        int length = path.length();
                        if (!path.endsWith(File.separator)) {
                            length += 1;
                        }
                        String subpath = mStartPath.substring(length);
                        int index = subpath.indexOf(File.separator);
                        if (index != -1) {
                            subpath = subpath.substring(0, index);
                        }
                        File file = new File(path + File.separator + subpath);

                        if (path.equals(File.separator)) {
                            List<String> sdcards = getSdCardPaths(getContext());
                            List<File> files = new ArrayList<>();
                            if (file.exists()) {
                                files.add(file);
                            }
                            for (String sdcard : sdcards) {
                                file = new File(sdcard);
                                if (file.exists()) {
                                    files.add(file);
                                }
                            }
                            listFiles = files.toArray(new File[0]);
                        } else if (file.exists()) {
                            listFiles = new File[]{file};
                        }
                    }
                }
                final int initCap = listFiles == null ? 0 : listFiles.length;

                SortedList<File> files = new SortedList<>(
                    File.class, new SortedListAdapterCallback<File>(getDummyAdapter()) {
                        @Override
                        public int compare(File lhs, File rhs) {
                            return compareFiles(lhs, rhs);
                        }

                        @Override
                        public boolean areContentsTheSame(File file, File file2) {
                            return file.getAbsolutePath().equals(file2.getAbsolutePath()) &&
                                (file.isFile() == file2.isFile());
                        }

                        @Override
                        public boolean areItemsTheSame(File file, File file2) {
                            return areContentsTheSame(file, file2);
                        }
                    }, initCap);

                files.beginBatchedUpdates();
                if (listFiles != null) {
                    for (java.io.File f : listFiles) {
                        if (isItemVisible(f)) {
                            files.add(f);
                        }
                    }
                }
                files.endBatchedUpdates();

                return files;
            }

            /**
             * Handles a request to start the Loader.
             */
            @Override
            protected void onStartLoading() {
                super.onStartLoading();

                // handle if directory does not exist. Fall back to root.
                if (mCurrentPath == null || !mCurrentPath.isDirectory()) {
                    mCurrentPath = getRoot();
                }

                // Start watching for changes
                fileObserver = new FileObserver(
                    mCurrentPath.getPath(), FileObserver.CREATE | FileObserver.DELETE |
                                                FileObserver.MOVED_FROM | FileObserver.MOVED_TO) {
                    @Override
                    public void onEvent(int event, String path) {
                        // Reload
                        onContentChanged();
                    }
                };
                fileObserver.startWatching();

                forceLoad();
            }

            /**
             * Handles a request to completely reset the Loader.
             */
            @Override
            protected void onReset() {
                super.onReset();

                // Stop watching
                if (fileObserver != null) {
                    fileObserver.stopWatching();
                    fileObserver = null;
                }
            }
        };
    }

    /**
     * Used by the list to determine whether a file should be displayed or not.
     * Default behavior is to always display folders. If files can be selected,
     * then files are also displayed. Set the showHiddenFiles property to show
     * hidden file. Default behaviour is to hide hidden files. Override this method to enable other
     * filtering behaviour, like only displaying files with specific extensions (.zip, .txt, etc).
     *
     * @param file to maybe add. Can be either a directory or file.
     * @return True if item should be added to the list, false otherwise
     */
    protected boolean isItemVisible(final File file) {
        if (!showHiddenItems && file.isHidden()) {
            return false;
        }
        return super.isItemVisible(file);
    }

    /**
     * Compare two files to determine their relative sort order. This follows the usual
     * comparison interface. Override to determine your own custom sort order.
     * <p/>
     * Default behaviour is to place directories before files, but sort them alphabetically
     * otherwise.
     *
     * @param lhs File on the "left-hand side"
     * @param rhs File on the "right-hand side"
     * @return -1 if if lhs should be placed before rhs, 0 if they are equal,
     * and 1 if rhs should be placed before lhs
     */
    protected int compareFiles(@NonNull File lhs, @NonNull File rhs) {
        if (lhs.isDirectory() && !rhs.isDirectory()) {
            return -1;
        } else if (rhs.isDirectory() && !lhs.isDirectory()) {
            return 1;
        } else {
            return lhs.getName().compareToIgnoreCase(rhs.getName());
        }
    }

    // Unofficial hacks to get a list of SD cards that are not the main "external storage".
    private static List<String> getSdCardPaths(final Context context) {
        // Q is the last version that will support normal file access.
        List<String> list = null;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && Build.VERSION.SDK_INT <= Build.VERSION_CODES.Q) {
            // Log.i(TAG, "getSdCardPaths: Trying KitKat method");
            list = getSdCardPaths19(context);
        }

        if (list == null) {
            // Log.i(TAG, "getSdCardPaths: Attempting fallback");
            // Try another method.
            list = new ArrayList<String>();
            File[] fileList = new File("/storage/").listFiles();
            if (fileList != null) {
                String removableStoragePath = Environment.getExternalStorageDirectory().getAbsolutePath();
                for (File file : fileList) {
                    if (!file.getAbsolutePath().equalsIgnoreCase(removableStoragePath) &&
                            file.isDirectory() &&
                            file.canRead()) {
                        list.add(file.getAbsolutePath());
                    }
                }
            }
        }

        // TODO: On older devices, try System.getenv("EXTERNAL_SDCARD_STORAGE")

        return list;
    }

    /**
     * returns a list of all available sd cards paths, or null if not found.
     */
    private static List<String> getSdCardPaths19(final Context context) {
        final File[] externalCacheDirs = context.getExternalCacheDirs();
        if (externalCacheDirs == null || externalCacheDirs.length==0)
            return null;
        if (externalCacheDirs.length == 1) {
            if (externalCacheDirs[0] == null)
                return null;
            final String storageState = Environment.getStorageState(externalCacheDirs[0]);
            if (!Environment.MEDIA_MOUNTED.equals(storageState))
                return null;
            if (Environment.isExternalStorageEmulated())
                return null;
        }
        final List<String> result = new ArrayList<>();
        if (externalCacheDirs.length == 1)
            result.add(getRootOfInnerSdCardFolder(externalCacheDirs[0]));
        for (int i = 1; i < externalCacheDirs.length; ++i) {
            final File file=externalCacheDirs[i];
            if (file == null)
                continue;
            final String storageState = Environment.getStorageState(file);
            if (Environment.MEDIA_MOUNTED.equals(storageState))
                result.add(getRootOfInnerSdCardFolder(externalCacheDirs[i]));
        }
        if (result.isEmpty())
            return null;
        return result;
    }

    /** Given any file/folder inside an sd card, this will return the path of the sd card */
    private static String getRootOfInnerSdCardFolder(File file) {
        if (file == null)
            return null;
        final long totalSpace = file.getTotalSpace();
        while (true) {
            final File parentFile = file.getParentFile();
            if (parentFile == null || !parentFile.canRead()) {
                break;
            }
            if (parentFile.getTotalSpace() != totalSpace) {
                break;
            }
            file = parentFile;
        }
        return file.getAbsolutePath();
    }
}
