import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVRecord;

import java.io.BufferedReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;

public class CellFileReader {

    private List<Integer[]> distinctCells = new ArrayList<>();

    public CellFileReader(String filename) {

        if(!filename.matches(".*\\.csv")){
            filename = filename + ".csv";
        }

        CSVFormat cellFileFormat = CSVFormat.Builder.create()
                .setHeader("Alpha CDR3", "Beta CDR3", "Alpha CDR1", "Beta CDR1")
                .setSkipHeaderRecord(true)
                .build();

        try(//don't need to close reader bc of try-with-resources auto-closing
            BufferedReader reader = Files.newBufferedReader(Path.of(filename));
            CSVParser parser = new CSVParser(reader, cellFileFormat);
        ){
            for(CSVRecord record: parser.getRecords()) {
                Integer[] cell = new Integer[4];
                cell[0] = Integer.valueOf(record.get("Alpha CDR3"));
                cell[1] = Integer.valueOf(record.get("Beta CDR3"));
                cell[2] = Integer.valueOf(record.get("Alpha CDR1"));
                cell[3] = Integer.valueOf(record.get("Beta CDR1"));
                distinctCells.add(cell);
            }
        } catch(IOException ex){
            System.out.println("cell file " + filename + " not found.");
            System.err.println(ex);
        }
    }

    public List<Integer[]> getCells(){
        return distinctCells;
    }

    public Integer getCellCount() {
        return distinctCells.size();
    }
}
