package party.bami.eqfix.other;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.util.*;

import static party.bami.eqfix.EQFix.getAppContext;


/**
 * This class handles getting a user's directory preferences
 * and returning the files and directories.
 * <p>
 */
public class ParseDirectories {
    private boolean parseSubDirectories;
    Queue<String> queue = new LinkedList<>();
    Queue<String> directories = new LinkedList<>();

    ParseDirectories(Context context) {

        //------get sharedPreferences

        SharedPreferences pref = context.getSharedPreferences("settings", Context.MODE_PRIVATE);

//-------get a value from them

//        pref.getString("NAME", "Android");

//--------modify the value

        pref.edit().putString("NAME", "Simone").apply();

//--------reset preferences

//        pref.edit().clear().apply();
    }


    /**
     * This method registers a new directory.
     *
     * @param context - current context.
     * @param value - Directory path to be added to user's preferences.
     */
    public static void RegisterDirectory(Context context,String value){
        SharedPreferences pref = context.getSharedPreferences("settings", Context.MODE_PRIVATE);
        String dirList = "";
        dirList += pref.getString("Directories", "");
        if (!dirList.equals( "")){
            dirList+=",";
        }
        pref.edit().putString("Directories", dirList +value).apply();
    }

    /**
     * Getter that returns the directories the user has set to contain music files.
     *
     * @param context - current user
     * @return - An array of directory paths that may contain music of the user.
     */
    public static String[] getDirectories(Context context){
        SharedPreferences pref = context.getSharedPreferences("settings", Context.MODE_PRIVATE);
        String dirList = pref.getString("Directories", "");
        if(dirList.equals("")){
            return null;
        }
        return dirList.split(",", -1);
    }

    /**
     * This method returns an array of files the user may want to play MP3 files are by default not playable.
     * They will have to be converted.
     *
     * @param path - Directory path to search for MP3/Wav files in
     * @return - Array of files.
     */
    public static File[] getFiles(String path){
        Collection<File> file = FileUtils.listFiles(new File(path), new String[]{"mp3","wav"}, false);
        return file.toArray(new File[0]);
    }


    /**
     * This method returns all files inside a directory the application can directly play (WAV files).
     *
     * @See getFiles
     * @param path - Directory path to search for Wav files in.
     * @return - Array of files.
     */
    public static File[] getPlayableFiles(String path){
        Collection<File> file = FileUtils.listFiles(new File(path), new String[]{"wav"}, false);
        return file.toArray(new File[0]);
    }

    /**
     * This method check's all directories of a user for playable files.
     *
     * @See getPlayableFiles
     * @param context - current application context.
     * @return Array of files.
     */
    public static File[] getAllPlayableMedia(Context context){
        String[] paths = getDirectories(context);

        if (paths != null) {
            ArrayList<File> fileList = new ArrayList<>();
            for (int i = 0; i < paths.length; i++) {
                fileList.addAll(Arrays.asList(getPlayableFiles(paths[i])));
            }
            return fileList.toArray(new File[0]);
        }else{
            return null;
        }
    }


    /**
     * This method obtains a file as preset
     */
    public static String getFilePreset(String fileName){
        String dirList = getSavePath();
        if(!dirList.equals("")){
            return dirList + "/"+ fileName;
        }else{
            return "";
        }
    }


    public static Application getApplicationUsingReflection() throws Exception {
        return (Application) Class.forName("android.app.ActivityThread")
                .getMethod("currentApplication").invoke(null, (Object[]) null);
    }


    public static String getSavePath(){

        SharedPreferences pref = null;
        try {
            pref = getApplicationUsingReflection().getApplicationContext().getSharedPreferences("settings", Context.MODE_PRIVATE);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return pref.getString("PresetPath", "");
    }

    /**
     * This method registers a new directory.
     *
     * @param context - current context.
     * @param value - Directory path to be added to user's preferences.
     */
    public static void RegisterDirectorySave(Context context,String value){
        SharedPreferences pref = context.getSharedPreferences("settings", Context.MODE_PRIVATE);

        pref.edit().putString("PresetPath", value).apply();
    }

}
