package view.panels;

import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.util.List;

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

    private JTextPane output;
    private JScrollPane scrollPane;
    private StringBuilder content;

    public InfoPanel() {
        this.setBorder(new TitledBorder("Ausgabekanal"));
        this.setLayout(new BorderLayout());
        output = new JTextPane();
        output.setEditable(false);
        output.setContentType("text/html");

        content = new StringBuilder();

        scrollPane = new JScrollPane(output);
        scrollPane.setWheelScrollingEnabled(true);
        this.add(scrollPane, BorderLayout.CENTER);

    }


    public void appendParagraph(String p) {

        content.append("<p>" + p + "</p>");

        output.setText(content.toString());
    }

    public void appendParagraphWithHeading(String h1) {

        content.append("<h1>" + h1 + "</h1>");

        output.setText(content.toString());
    }

    public void appendParagraphRed(String p) {

        content.append("<p style=\" color:red \"><em><strong>" + p + "</strong></em></p>");

        output.setText(content.toString());
    }

    public void appendParagraphGreen(String p) {

        content.append("<p style=\" color:green \"><em><strong>" + p + "</strong></em></p>");

        output.setText(content.toString());
    }

    public void logTable(List<String> heading, List<List<String>> rows) {
        content.append("<center>");
        content.append("<table style=\" width:80%; border: 1px solid black; \">");
        content.append("<tr>");
        if (heading.size() > 1) {
            for (String str : heading) {
                content.append("<th style=\" border: 1px solid black; \">" + str + "</th>");
            }
        } else {
            content.append("<th style=\" border: 1px solid black;\" colspan=\"" + rows.get(0).size() + "\"; >" + heading.get(0) + "</th>");
        }

        content.append("</tr>");
        for (List<String> row : rows) {
            content.append("<tr>");
            for (String entry : row) {
                content.append("<td style=\" border: 1px solid black; \">" + entry + "</td>");
            }
            content.append("</tr>");
        }
        content.append("</table>");
        content.append("</center>");
        output.setText(content.toString());
    }


}
