package com.marverenic.music.dialog;

import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.widget.TextView;

import com.marverenic.music.R;

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

public class DirectoryDialogFragment extends DialogFragment {

    private static final String TAG_DIALOG = "DirectoryDialogFragment_Dialog";

    private File mDirectory;
    private List<File> mSubDirectories;
    private String[] mSubDirectoryNames;
    private OnDirectoryPickListener mCallback;

    public DirectoryDialogFragment setDirectory(File directory) {
        mDirectory = directory;
        return this;
    }

    public DirectoryDialogFragment setDirectoryPickListener(OnDirectoryPickListener callback) {
        mCallback = callback;
        return this;
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        if (mDirectory == null) {
            setDirectory(Environment.getExternalStorageDirectory());
        }

        scanSubDirs();

        AlertDialog.Builder builder = new AlertDialog.Builder(getContext())
                .setTitle(mDirectory.getAbsolutePath())
                .setNegativeButton(R.string.action_cancel, null)
                .setPositiveButton(R.string.action_select, (dialogInterface, i) -> {
                    if (mCallback != null) {
                        mCallback.onDirectoryChosen(mDirectory);
                    }
                })
                .setNeutralButton(R.string.action_navigate_up, (dialogInterface, i) -> {
                    new DirectoryDialogFragment()
                            .setDirectory(mDirectory.getParentFile())
                            .show(getFragmentManager(), TAG_DIALOG);
                });

        if (mSubDirectories.isEmpty()) {
            builder.setMessage(R.string.empty_directory);
        } else {
            builder.setItems(mSubDirectoryNames, (dialog, which) -> {
                File directory = mSubDirectories.get(which);
                new DirectoryDialogFragment()
                        .setDirectory(directory)
                        .show(getFragmentManager(), TAG_DIALOG);
            });
        }

        AlertDialog dialog = builder.show();

        File parent = mDirectory.getParentFile();
        if (parent == null || !parent.canRead()) {
            dialog.getButton(DialogInterface.BUTTON_NEUTRAL).setEnabled(false);
        }

        // Ellipsize the header at the beginning to show a more informative file path
        TextView header = (TextView) dialog.findViewById(R.id.alertTitle);
        if (header != null) {
            header.setEllipsize(TextUtils.TruncateAt.START);
        }

        return dialog;
    }

    private void scanSubDirs() {
        mSubDirectories = new ArrayList<>();
        for (File f : mDirectory.listFiles()) {
            if (f.isDirectory() && f.canRead()) {
                mSubDirectories.add(f);
            }
        }

        mSubDirectoryNames = new String[mSubDirectories.size()];
        for (int i = 0; i < mSubDirectoryNames.length; i++) {
            mSubDirectoryNames[i] = mSubDirectories.get(i).getName();
        }
    }

    public interface OnDirectoryPickListener {
        void onDirectoryChosen(File directory);
    }
}

@@ -198,16 +198,60 @@
 onNull;%0A
+import android.support.annotation.Nullable;%0A
 import a
@@ -284,24 +284,24 @@
 ogFragment;%0A
-
 import andro
@@ -643,16 +643,78 @@
 alog%22;%0A%0A
+    private static final String KEY_DIRECTORY = %22Directory%22;%0A%0A
     priv
@@ -1115,16 +1115,16 @@
 llback;%0A
-
         
@@ -1139,24 +1139,611 @@
 his;%0A    %7D%0A%0A
+    @Override%0A    public void onCreate(@Nullable Bundle savedInstanceState) %7B%0A        super.onCreate(savedInstanceState);%0A%0A        if (savedInstanceState != null) %7B%0A            String path = savedInstanceState.getString(KEY_DIRECTORY);%0A            if (path != null) %7B%0A                mDirectory = new File(path);%0A            %7D%0A        %7D%0A    %7D%0A%0A    @Override%0A    public void onSaveInstanceState(Bundle outState) %7B%0A        super.onSaveInstanceState(outState);%0A%0A        if (mDirectory != null) %7B%0A            outState.putString(KEY_DIRECTORY, mDirectory.getAbsolutePath());%0A        %7D%0A    %7D%0A%0A
     @NonNull

Save current directory on configuration changes