import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVPrinter;

import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.List;

public class CellFileWriter {

    private String[] headers = {"Alpha CDR3", "Beta CDR3", "Alpha CDR1", "Beta CDR1"};
    List<Integer[]> cells;
    String filename;
    Integer cdr1Freq;

    public CellFileWriter(String filename, CellSample cells) {
        if(!filename.matches(".*\\.csv")){
            filename = filename + ".csv";
        }
        this.filename = filename;
        this.cells = cells.getCells();
        this.cdr1Freq = cells.getCdr1Freq();
    }

    public void writeCellsToFile() {
        CSVFormat cellFileFormat = CSVFormat.Builder.create()
                .setCommentMarker('#')
                .setHeader(headers)
                .build();
        try(BufferedWriter writer = Files.newBufferedWriter(Path.of(filename), StandardOpenOption.CREATE_NEW);
            CSVPrinter printer = new CSVPrinter(writer, cellFileFormat);
        ){
            printer.printComment("Sample contains 1 unique CDR1 for every " + cdr1Freq + "unique CDR3s.");
            printer.printRecords(cells);
        } catch(IOException ex){
            System.out.println("Could not make new file named "+filename);
            System.err.println(ex);
        }
    }
}
