package flib.core;

/*
 *
 * FFileLoader.java
 *
 *
 */



// -----------------------------------------------------------------------------
// Libraries
// -----------------------------------------------------------------------------
import processing.core.PApplet;

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

import flib.core.Core;



public class FFileLoader {
    // -----------------------------------------------------------------------------
    // Properties
    // -----------------------------------------------------------------------------
    protected static PApplet p5;

    private File folder;
    private ArrayList<String> files;
    private ArrayList<String> extensions;

    private String filepath;



    // -----------------------------------------------------------------------------
    // Constructor
    // -----------------------------------------------------------------------------
    /**
     * Instantiate FFileLoader
     *
     * @param papplet
     *        PApplet
     * @param filepath
     *        the path of the files to load
     */
    public FFileLoader(PApplet papplet, String filepath) {
        p5 = papplet;

        files = new ArrayList<String>();
        extensions = new ArrayList<String>();
        setFolder(filepath);
    }

    /**
     * Instantiate FFileLoader
     *
     * @param papplet
     *        PApplet
     * @param filepath
     *        the path of the files to load
     * @param  extension
     *        only load files of this extension type
     */
    public FFileLoader(PApplet papplet, String filepath, String extension) {
        p5 = papplet;

        files = new ArrayList<String>();
        extensions = new ArrayList<String>();
        extensions.add( extension );
        setFolder(filepath);
    }

    /**
     * Instantiate FFileLoader
     *
     * @param papplet
     *        PApplet
     * @param filepath
     *        the path of the files to load
     * @param  extension
     *        only load files of this extension type
     */
    public FFileLoader(PApplet papplet, String filepath, String[] extension) {
        p5 = papplet;

        files = new ArrayList<String>();
        extensions = new ArrayList<String>( Arrays.asList(extension) );
        setFolder(filepath);
    }



    // -----------------------------------------------------------------------------
    // Methods
    // -----------------------------------------------------------------------------

    //
    // Sets
    //
    /**
     * @param filepath
     *          the path of the files to load
     */
    public void setFolder(String filepath) {
        setFolderName(p5.sketchPath + filepath);
        setFiles( folder.list() );
    }

    /**
     * @param filepath
     *          set the file to read
     */
    private void setFolderName(String filepath) {
        this.filepath = filepath;
        folder = new File( this.filepath );
    }

    /**
     * @param filename
     *          set the file to load
     */
    public void setFile(String filename) {
        boolean isDir = new File(this.filepath + "/" + filename).isDirectory();
        boolean isDSs = filename.equals(".DS_Store");
        boolean isFil = false;
        if( extensions.size() > 0 ) {
            isFil = extensions.contains( Core.getExtension(filename) );
        }

        if( !isDSs && !isDir && isFil ) {
            files.add( this.filepath + "/" + filename );
            System.out.println( "loading file " + filename );
        }
        // else {
        //     System.out.println( filename + " is not a valid file" );
        // }

    }

    /**
     * @param filename
     *          set the files to load
     */
    public void setFiles(String[] filenames) {
        if(filenames != null || filenames.length != 0) {
            for(int i=0; i != filenames.length; i++) {
                String file = filenames[i];
                setFile(file);
            }
        }
    }


    //
    // Gets
    //
    /**
     * @return number of files
     *
     */
    public int getFileCount() {
        return files.size();
    }

    /**
     * @param index
     *          number of file to use
     *
     * return a specific file
     *
     * @return files[w]
     */
    public String getFile(int index) {
        index = (index < 0)
            ? index % files.size() + files.size()
            : index % files.size();
        return files.get(index);
    }

}


