package presenter.io;

import model.Line;
import model.LineModel;
import com.opencsv.CSVWriter;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Observable;

/**
 * Implementierung verschiedener Algorithmen zur Berechnung von Ausgleichsgeraden.
 *
 * @Author: Armin Wolf
 * @Email: a_wolf28@uni-muenster.de
 * @Date: 03.08.2017.
 */
public class DataExporter extends Observable {

    private LineModel lineModel;
    private File file;

    public DataExporter(LineModel model, File file) {
        this.file = file;
        this.lineModel = model;
    }

    public void export() {
        CSVWriter writer = null;

        try {
            writer = new CSVWriter(new FileWriter(file), ',');
            // feed in your array (or convert your data to an array)
            String[] entries = new String[3];
            for (Line line : lineModel.getLines()) {
                entries[0] = line.getId();
                entries[1] = line.getM().toString();
                Double tmp = (-1) * line.getB();
                entries[2] = tmp.toString();
                writer.writeNext(entries);
            }
            writer.close();
            String[] ret = {"export", "Das aktuelle Modell wurde erfolgreich unter: " + file.getAbsolutePath() + " gespeichert."};
            setChanged();
            notifyObservers(ret);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}
