package presenter.io;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

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

    private DefaultTableModel model;
    private File file;

    public EvalResultLatexExport(DefaultTableModel model, File file) {
        this.model = model;
        this.file = file;
    }

    /**
     * e.g. Source: <url>https://en.wikibooks.org/wiki/LaTeX/Tables</url>
     * \begin{tabular}{l*{3}{c}}
     * Team              & P & W & D & L & F  & A & Pts \\\hline
     * Manchester United & 6 & 4 & 0 & 2 & 10 & 5 & 12  \\
     * Celtic            & 6 & 3 & 0 & 3 &  8 & 9 &  9  \\
     * Benfica           & 6 & 2 & 1 & 3 &  7 & 8 &  7  \\
     * FC Copenhagen     & 6 & 2 & 1 & 3 &  5 & 8 &  7  \\
     * \end{tabular}
     *
     * @return
     */
    private String createOutputData() {
        String split = "&";
        StringBuilder doc = new StringBuilder();
        doc.append("\\begin{tabular}{l|*{3}{r}}" + "\r\n");
        for (int i = 0; i < model.getColumnCount(); i++) {
            if (model.getValueAt(0, i).toString() != "" || model.getRowCount() > 5)
                doc.append("\\textsc{" + model.getColumnName(i) + "}" + split);
        }

        doc.deleteCharAt(doc.lastIndexOf(split));
        doc.append("\\\\\\hline\\hline" + "\r\n");

        for (int i = 0; i < model.getRowCount() - 1; i++) {
            for (int j = 0; j < model.getColumnCount(); j++) {
                if (model.getValueAt(i, j).toString() != "") {
                    if (j == 0) {
                        doc.append("\\textbf{" + model.getValueAt(i, j).toString() + "}" + split);
                    } else {
                        Double val = Double.parseDouble((String) model.getValueAt(i, j));
                        doc.append(String.format("%.3f", val) + split);
                    }
                }
            }
            if (model.getValueAt(i, 0) != "") {
                doc.deleteCharAt(doc.lastIndexOf(split));
            }
            doc.append("\\\\" + "\r\n");
        }
        doc.append("\\end{tabular}");
        return doc.toString();
    }

    public void writeFile() {
        FileWriter fileWriter = null;
        BufferedWriter bufferedWriter = null;
        try {
            fileWriter = new FileWriter(file);
            bufferedWriter = new BufferedWriter(fileWriter);
            bufferedWriter.write(createOutputData());
            SwingUtilities.invokeLater(() -> {
                JOptionPane.showMessageDialog(null, "Export war Erfolgreich!");
            });


        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                bufferedWriter.close();
                fileWriter.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

}
