package gr.softaware.lib_1_3.data.convert.csv;

import java.util.List;

/**
 *
 * @author siggouroglou
 * @param <T> A class that implements the CsvGenerationModel interface.
 */
final public class CsvGenerator<T extends CsvGenerationModel> {

    private final List<CsvGenerationModel> modelList;

    public CsvGenerator(List<T> modelList) {
        this.modelList = (List<CsvGenerationModel>)modelList;
    }
    
    public StringBuilder getContent() {
        return getContent("\t", "\\t");
    }
    
    public StringBuilder getContent(String separator, String separatorAsString) {
        StringBuilder builder = new StringBuilder();
        
        // First line contains the separator.
        builder.append("sep=").append(separatorAsString).append("\n");
        
        // Get the header.
        if(!modelList.isEmpty()) {
            builder.append(modelList.get(0).toCsvFormattedHeader(separator)).append("\n");
        }
        
        // Get the data.
        modelList.stream().forEach((model) -> {
            builder.append(model.toCsvFormattedRow(separator)).append("\n");
        });
        
        return builder;
    }
}
