package leaptest.model;

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

/**
 * Gaat er vanuit dat eerste deel van de models easy zijn, 
 * tweede deel normal en derde deel hard
 * TODO: export uit map halen
 * @author Janne Weijkamp
 */
public class TaskManager {
    
    /*
     * bm contains a list of 9 random models: 3 easy, 3 normal, 3 hard (in that order)
     */
    private ArrayList <BlockModel> bm;
    String [] all_tasks = new String [9];
    private int counter;
    private int number_of_tasks = 9;
    
    public TaskManager(String path)
    {
        counter = 0;
        bm = new ArrayList<BlockModel>();
        
        File folder = new File(path);
        File[] listOfFiles = folder.listFiles();
        Arrays.sort(listOfFiles);
        
        String[] listOfFileNames = new String [listOfFiles.length];
        for(int i=0;i<listOfFiles.length;i++)
        {
            listOfFileNames[i] = listOfFiles[i].getName();
        }
        Arrays.sort(listOfFileNames, new LengthCompare());
        
        String[] allEasyModels = new String [listOfFileNames.length/3];
        String[] allNormalModels = new String [listOfFileNames.length/3];
        String[] allHardModels = new String [listOfFileNames.length/3];
        
        for(int i=0;i<listOfFiles.length/3;i++)
        {   
            allEasyModels[i] = listOfFileNames[i];
            allNormalModels[i] = listOfFileNames[i+listOfFiles.length/3];
            allHardModels[i] = listOfFileNames[i+ 2*(listOfFiles.length/3)];
        }
        shuffleArray(allEasyModels); 
        shuffleArray(allNormalModels);
        shuffleArray(allHardModels);
      
        for(int i=0;i<number_of_tasks/3; i++)
        {   
            all_tasks[i] = allEasyModels[i];
            bm.add(new BlockModel(path + allEasyModels[i]));
            //System.out.println(path + allEasyModels[i]);
        }
        for(int i=0;i<number_of_tasks/3; i++)
        {
            all_tasks[i+(number_of_tasks/3)] = allNormalModels[i];
            bm.add(new BlockModel(path + allNormalModels[i]));
            //System.out.println(path + allNormalModels[i]);
        }
        for(int i=0;i<3; i++)
        {
            all_tasks[i+2*(number_of_tasks/3)] = allHardModels[i];
            bm.add(new BlockModel(path + allHardModels[i]));
            //System.out.println(path + allHardModels[i]);
        }
    }

    /**
     * Start next task
     */
    public void nextTask()
    {
        counter++;
    }
    
    /**
     * Return number of current task
     * @return 
     */
    public int getTask() 
    {
        return Integer.parseInt(all_tasks[counter].replaceAll("\\D+",""));
    }
 
    private static class LengthCompare implements Comparator<String>
    {
        public int compare(String s1, String s2)
        {
            return (s1.length() - s2.length());
        }
    }
    
    static void shuffleArray(String[] ar)
    {
        Random rnd = new Random();
        for (int i = ar.length - 1; i > 0; i--)
        {
            int index = rnd.nextInt(i + 1);
            String a = ar[index];
            ar[index] = ar[i];
            ar[i] = a;
        }
    }
}


@@ -121,16 +121,39 @@
 arator;%0A
+import java.util.List;%0A
 import j
@@ -795,16 +795,20 @@
 tOfFiles
+Temp
  = folde
@@ -853,17 +853,398 @@
 tOfFiles
-)
+Temp);%0A        %0A        ArrayList%3CFile%3E listOfFiles2 = new ArrayList%3CFile%3E();%0A        for(int i =0;i%3ClistOfFilesTemp.length;i++)%0A        %7B%0A            if(!listOfFilesTemp%5Bi%5D.getName().equals(%22export.model%22));%0A            %7B%0A                listOfFiles2.add(listOfFilesTemp%5Bi%5D);%0A            %7D%0A        %7D%0A        File%5B%5D listOfFiles = listOfFiles2.toArray(new File%5BlistOfFiles2.size()%5D);
 ;%0A      
@@ -2852,24 +2852,177 @@
 ;%0A        %7D%0A
+        %0A        //for(int i=0;i%3C9; i++)%0A        //%7B%0A        //    System.out.println(Integer.parseInt(all_tasks%5Bi%5D.replaceAll(%22%5C%5CD+%22,%22%22)));%0A        //%7D%0A
     %7D%0A%0A    /

update taskmanager to filter out export.model