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.*;
import java.util.regex.Pattern;

public class PlateFileWriter {
    private int size;
    private List<List<Integer[]>> wells;
    private double stdDev;
    private Double error;
    private String filename;
    private String[] headers;
    private List<Integer> concentrations;

    public PlateFileWriter(String filename, Plate plate) {
        if(!filename.matches(".*\\.csv")){
            filename = filename + ".csv";
        }
        this.filename = filename;
        this.size = plate.getSize();
        this.stdDev = plate.getStdDev();
        this.error = plate.getError();
        this.wells = plate.getWells();
        this.concentrations = Arrays.asList(plate.getConcentrations());
        concentrations.sort(Comparator.reverseOrder());
    }

    public void writePlateFile(){
        //works as is, but too many columns in csv, need to make them all rows.

        //will now redo it so that every column is a well, with well names as headers
        //need to give plate error, sample pop size, stdDev, num sections, concentration per section as comments
        Comparator<List<Integer[]>> listLengthDescending = Comparator.comparingInt(List::size);
        wells.sort(listLengthDescending.reversed());
        int maxLength = wells.get(0).size();
        List<List<String>> wellsAsStrings = new ArrayList<>();
        for (List<Integer[]> w: wells){
            List<String> tmp = new ArrayList<>();
            for(Integer[] c: w) {
                tmp.add(Arrays.toString(c));
            }
            wellsAsStrings.add(tmp);
        }
        for(List<String> w: wellsAsStrings){
            while(w.size() < maxLength){
                w.add("");
            }
        }

        //this took forever
        List<List<String>> rows = new ArrayList<>();
        List<String> tmp = new ArrayList<>();
        for(int i = 0; i < wellsAsStrings.size(); i++){//List<Integer[]> w: wells){
            tmp.add("well " + (i+1));
        }
        rows.add(tmp);
        for(int row = 0; row < maxLength; row++){
            tmp = new ArrayList<>();
            for(List<String> c: wellsAsStrings){
                tmp.add(c.get(row));
            }
            rows.add(tmp);
        }
        StringBuilder concen = new StringBuilder();
        for(Integer i: concentrations){
            concen.append(i.toString());
            concen.append(" ");
        }
        String concenString = concen.toString();

        CSVFormat plateFileFormat = CSVFormat.Builder.create().setCommentMarker('#').build();

        try(BufferedWriter writer = Files.newBufferedWriter(Path.of(filename), StandardOpenOption.CREATE_NEW);
            CSVPrinter printer = new CSVPrinter(writer, plateFileFormat);
        ){
            printer.printComment("Each row represents one well on the plate.");
            printer.printComment("Plate size: " + size);
            printer.printComment("Error rate: " + error);
            printer.printComment("Concentrations: " + concenString);
            printer.printComment("Std. dev.: " + stdDev);
            printer.printRecords(wellsAsStrings);
        } catch(IOException ex){
            System.out.println("Could not make new file named "+filename);
            System.err.println(ex);
        }
    }
}
