package de.danoeh.antennapod.adapter;

import android.app.Dialog;
import android.content.Context;
import android.text.format.Formatter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.RadioButton;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

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

import de.danoeh.antennapod.R;
import de.danoeh.antennapod.core.preferences.UserPreferences;
import de.danoeh.antennapod.core.util.StorageUtils;
import de.danoeh.antennapod.dialog.ChooseDataFolderDialog;


public class DataFolderAdapter extends RecyclerView.Adapter<DataFolderAdapter.ViewHolder> {
    private final ChooseDataFolderDialog.RunnableWithString selectionHandler;
    private final String currentPath;
    private final List<StoragePath> entries;
    private final String freeSpaceString;
    private Dialog dialog;

    public DataFolderAdapter(Context context, ChooseDataFolderDialog.RunnableWithString selectionHandler) {
        this.entries = getStorageEntries(context);
        this.currentPath = getCurrentPath();
        this.selectionHandler = selectionHandler;
        this.freeSpaceString = context.getString(R.string.choose_data_directory_available_space);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View entryView = inflater.inflate(R.layout.choose_data_folder_dialog_entry, parent, false);
        return new ViewHolder(entryView);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        StoragePath storagePath = entries.get(position);
        Context context = holder.root.getContext();
        String freeSpace = Formatter.formatShortFileSize(context, storagePath.getAvailableSpace());
        String totalSpace = Formatter.formatShortFileSize(context, storagePath.getTotalSpace());

        holder.path.setText(storagePath.getShortPath());
        holder.size.setText(String.format(freeSpaceString, freeSpace, totalSpace));
        holder.progressBar.setProgress(storagePath.getUsagePercentage());
        holder.root.setOnClickListener((View v) -> selectAndDismiss(storagePath));
        holder.radioButton.setOnClickListener((View v) -> selectAndDismiss(storagePath));
        if (storagePath.getFullPath().equals(currentPath)) {
            holder.radioButton.toggle();
        }
    }

    @Override
    public int getItemCount() {
        if (currentPath == null) {
            return 0;
        } else {
            return entries.size();
        }
    }

    public void setDialog(Dialog dialog) {
        this.dialog = dialog;
    }

    private String getCurrentPath() {
        File dataFolder = UserPreferences.getDataFolder(null);
        if (dataFolder != null) {
            return dataFolder.getAbsolutePath();
        }
        return null;
    }

    private List<StoragePath> getStorageEntries(Context context) {
        File[] mediaDirs = ContextCompat.getExternalFilesDirs(context, null);
        final List<StoragePath> entries = new ArrayList<>(mediaDirs.length);
        for (File dir : mediaDirs) {
            if (!isWritable(dir)) {
                continue;
            }
            entries.add(new StoragePath(dir.getAbsolutePath()));
        }
        if (entries.isEmpty() && isWritable(context.getFilesDir())) {
            entries.add(new StoragePath(context.getFilesDir().getAbsolutePath()));
        }
        return entries;
    }

    private boolean isWritable(File dir) {
        return dir != null && dir.exists() && dir.canRead() && dir.canWrite();
    }

    private void selectAndDismiss(StoragePath storagePath) {
        selectionHandler.run(storagePath.getFullPath());
        dialog.dismiss();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        private final View root;
        private final TextView path;
        private final TextView size;
        private final RadioButton radioButton;
        private final ProgressBar progressBar;

        ViewHolder(View itemView) {
            super(itemView);
            root = itemView.findViewById(R.id.root);
            path = itemView.findViewById(R.id.path);
            size = itemView.findViewById(R.id.size);
            radioButton = itemView.findViewById(R.id.radio_button);
            progressBar = itemView.findViewById(R.id.used_space);
        }
    }

    static class StoragePath {
        private final String path;

        StoragePath(String path) {
            this.path = path;
        }

        String getShortPath() {
            int prefixIndex = path.indexOf("Android");
            return (prefixIndex > 0) ? path.substring(0, prefixIndex) : path;
        }

        String getFullPath() {
            return this.path;
        }

        long getAvailableSpace() {
            return StorageUtils.getFreeSpaceAvailable(path);
        }

        long getTotalSpace() {
            return StorageUtils.getTotalSpaceAvailable(path);
        }

        int getUsagePercentage() {
            return 100 - (int) (100 * getAvailableSpace() / (float) getTotalSpace());
        }
    }
}
@@ -2708,86 +2708,8 @@
 ) %7B%0A
-        if (currentPath == null) %7B%0A            return 0;%0A        %7D else %7B%0A    
     
@@ -2731,34 +2731,24 @@
 ies.size();%0A
-        %7D%0A
     %7D%0A%0A    p

Allow to select new folder even if current folder is null