package stack3d.providers;


import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * @author KAM
 */

public class LocalDiskProvider extends MediaProvider {

    private String[] imageFileNames = null;
    private String dir = "";

    public LocalDiskProvider(String dir, String[] names) {
        this.dir = dir + "/";
        this.imageFileNames = names;
    }

    public LocalDiskProvider(String[] files) {
        imageFileNames = files;
        System.out.println("len = " + files.length);
        for(String file : imageFileNames) {
            System.out.println(file);
        }
    }

    @Override
    public void requestMedia(long firstId, long lastId, Callback callback) {
        MediaInfo[] media = new MediaInfo[(int)Math.min(lastId - firstId + 1, imageFileNames.length - firstId)];
        for (int i = (int)firstId; i <= lastId && i < imageFileNames.length; i++) {
            media[i] = new LocalMediaInfo(i, dir + imageFileNames[i]);
        }
        callback.mediaLoaded(media);

        if (lastId >= imageFileNames.length) {
            callback.endOfMedia();
        }
        //        var dir = "{mediaServer}resources/photos/car";
//        var images:String[] = new File(new URI(dir)).list();
//
//        return for(image in images[(page - 1) * mediaPerPage..<page * mediaPerPage]) [
//            LocalMediaInfo { mediumURL: "{dir}/{image}" }
//        ];

    }
    
//
//    override function getPageOfMedia(page: Integer): MediaInfo[] {
//        var photos:MediaInfo[];
//
//        ////////////////////////////////////////////////////////////////////////////
//        var mediaServer = null;
//        // either applet or didn't pass any arguments to the application.
//        if(FX.getArgument("javafx.applet") != null) {
//            mediaServer = FX.getArgument("mediaServer") as String;
//        } else {
//            mediaServer = Helper.getRootURI("Demo.root");
//        }
//
//    //    if(mediaServer == null) {
//    //        mediaServer = "http://localhost:8080/";
//    //    }
//        FX.println("mediaServer: {mediaServer}");
//        ////////////////////////////////////////////////////////////////////////////
//
//        var dir = "{mediaServer}resources/photos/car";
//        var images:String[] = new File(new URI(dir)).list();
//
//        return for(image in images[(page - 1) * mediaPerPage..<page * mediaPerPage]) [
//            LocalMediaInfo { mediumURL: "{dir}/{image}" }
//        ];
//    }
//
//}
//
}

