import java.io.*;

class responses extends database{
    
    public final String answer_filename = conf.RootDirectory+conf.ResponsesFileName;
    
    public void saveAnswers(String[] answers) throws IOException{
        String line = new String();
        for (int r = 0; r< answers.length; r++)
            if(r != 0)
                line += "\""+answers[r]+"\",";
            else
                line += "\""+answers[r]+"\",";
        
        this.add_line(answer_filename, line, this.get_total_lines(answer_filename) != 0);
    }
    
    public boolean areResponsesClear(){
        boolean result = true;
        try{
            result = (this.get_total_lines(answer_filename) == 0);
        }
        catch (Exception e){
            System.err.println(e.toString());
        }
        
        return result;
    }
    
    public void exportAnswers(String FileLocation, int SortType) throws IOException{
        /*
         Sort types: 0 -- sort by name
         *           1 -- by lastname
         *           2 -- by date (no sorting)
         */
        String[] responses = new String[this.get_total_lines(answer_filename)];
        int i = 0;
        for (int r = 0; r < this.get_total_lines(answer_filename); r++)
            responses[r] = this.get_line(answer_filename, r);
        
        String[][] SortArray = makeCSVlines(responses, SortType);
        
        if (SortType != 2) 
            SortArray = qsort(SortArray, 0, 0, SortArray.length-1);
        for (int r = 0; r < SortArray.length; r++)
            this.add_line(FileLocation+"/responses.csv", SortArray[r][1], (r!=0));
    }
    
    private String[][] makeCSVlines(String[] responses, int SortType){

        String[][] SortArray = new String[responses.length][2];
        for (int r = 0; r < responses.length; r++){
            String name = responses[r].substring(1, responses[r].indexOf("\","));
            String lastname = responses[r].substring(responses[r].indexOf("\",")+3, responses[r].indexOf("\",", responses[r].indexOf("\",")+2));
            responses[r] = responses[r].substring(responses[r].indexOf("\",", responses[r].indexOf("\",")+2)+2);
            if (lastname.compareTo("") != 0)
                responses[r] = "\""+name+" "+lastname+"\","+responses[r];
            else
                responses[r] = "\""+name+"\","+responses[r];
            if (SortType == 0)
                SortArray[r][0] = name.toLowerCase();
            if (SortType == 1)
                SortArray[r][0] = lastname.toLowerCase();
            SortArray[r][1] = responses[r].replaceAll("\",", "\""+conf.csvSeparator);
        }
        
        return SortArray;
    }
    
    private String[][] qsort(String[][] fields, int c, int l, int r){
        int j = l;
	int i = r;
	int mid = (l + r)/2;
	String x = fields[mid][c];
			
	do
	{
            while (fields[j][c].compareTo(x) < 0)
                j++;	
            while((fields[i][c].compareTo(x)) > 0)
                i--;
            if (j <= i){
                String[] tmp = fields[i];
		fields[i] = fields[j];
		fields[j] = tmp;
		j++;
		i--;
            }
        }
        while (j <= i);
		
	if(l<i) qsort(fields, c, l, i);
	if(j<r) qsort(fields, c, j, r);
        
        return fields;
    }
}
