/*
 * ReadFromTextFile.java
 */
package FrequencyListPopulations;

import java.io.FileReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;


public class ReadFromTextFile {
    
 /**
 * 
 * ReadFromTextFile is the class that handles reading from the text
 * file the information desired and stores results in ArrayLists. It then
 * sorts the list and organizes them based on frequency counts.
 *
 * @author Adam Nelson
 */

    
    RegAction action = new RegAction();
    private ArrayList<String> gtStringList = new ArrayList<String>();
    private ArrayList<String> etStringList = new ArrayList<String>();
    private ArrayList<String> ltStringList = new ArrayList<String>();
    private ArrayList<Integer> orderedList = new ArrayList<Integer>();
    private ArrayList<Integer> originalList = new ArrayList<Integer>();

  
    public ReadFromTextFile() {
        initializer();
    }

    /**
     * @param void
     * 
     * Reads the text file with a Scanner and passes each line to RegAction
     * to see that each line fits the intended format. It then sorts the
     * list and, based on its frequency count, puts each word from the text file
     * into its frequency-dependant category (i.e gtList, etList, ltList for
     * greater than, equal to and less than.
     *   
     */
    
    public void initializer() {

        try {
            //System.out.println("Please specify the file path.");
            //Scanner inputFileName = new Scanner(System.in);   
            //String fileName = inputFileName.nextLine();
            String fileName = "ui-test-text.txt"; //CHANGE PATH LATER
            FileReader reader = new FileReader(fileName);
            Scanner scan = new Scanner(reader);
            
            while (scan.hasNextLine()){ //read each line of the file and check it with regex
                action.listAdder(scan.nextLine());
            }

        } catch (Exception e) {
            //e.printStackTrace();
            System.out.println("Could not find the file specified. Please try again.");
        }

        originalList = action.getIntList();

        for (int l : originalList){ //another way to copy from originalList
            orderedList.add(l);
        }

        Collections.sort(orderedList);  //sort the fresh copy orderedList
        int index = 0; //keep an index count for matching orderedList and originalList
        final int freq_num = 0;

        for (int sortedInt : orderedList) {
            for (int unsortedInt : originalList) {

                if ((sortedInt == unsortedInt) && (sortedInt > freq_num)) {
                    getGtStringList().add(action.getStringList().get(index));
                    index = 0; //set index back to zero if a match is found
                    break;
                } else if ((sortedInt == unsortedInt) && (sortedInt == freq_num)) {
                    getEtStringList().add(action.getStringList().get(index));
                    index = 0;
                    break;
                } else if ((sortedInt == unsortedInt) && (sortedInt < freq_num)) {
                    getLtStringList().add(action.getStringList().get(index));
                    index = 0;
                    break;
                }
                index++;
            }
            index = 0;
        }
    }

    public ArrayList<String> getGtStringList() {
        return gtStringList;
    }

    public void setGtStringList(ArrayList<String> gtStringList) {
        this.gtStringList = gtStringList;
    }

    public ArrayList<String> getEtStringList() {
        return etStringList;
    }

    public void setEtStringList(ArrayList<String> etStringList) {
        this.etStringList = etStringList;
    }

    public ArrayList<String> getLtStringList() {
        return ltStringList;
    }

    public void setLtStringList(ArrayList<String> ltStringList) {
        this.ltStringList = ltStringList;
    }
}
