/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.ctecinf;

import br.com.ctecinf.swing.OptionPane;
import br.com.ctecinf.text.DateFormatter;
import java.awt.BorderLayout;
import java.awt.Desktop;
import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;
import javax.swing.JEditorPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;

/**
 *
 * @author Cássio Conceição
 * @since 29/05/2019
 * @version 201905
 * @see http://ctecinf.com.br/
 */
public class Log {

    public static void create(Object message) {

        System.err.println(message);

        if (message instanceof Exception) {

            Date dt = new Date();

            String msg = "<br /><br /><br /><p>" + DateFormat.getTimeInstance().format(dt) + "> " + message + "</p>\n";

            for (StackTraceElement stack : ((Exception) message).getStackTrace()) {
                if (stack.getClassName().contains("ctecinf") && !stack.getClassName().contains("$")) {
                    String url = new File("src").getAbsolutePath();
                    msg += "<p>(" + stack.getLineNumber() + ") <a href=\"" + url + File.separator + stack.getClassName().replace(".", File.separator).concat(".java") + "\">" + stack.getClassName() + "</a> -> " + stack.getMethodName() + "()</p>";
                    System.err.println("(" + stack.getLineNumber() + ") " + stack.getClassName() + " -> " + stack.getMethodName() + "()");
                }
            }

            try {
                Utils.writeFile("log" + File.separator + DateFormatter.format().format(dt).replace("/", "_") + ".html", msg, true, true);
            } catch (IOException ex) {
                System.err.println(ex);
            }

        }
    }

    public static void show() throws IOException {
        show(null);
    }

    public static void show(Date date) throws IOException {

        if (date == null) {
            date = new Date();
        }

        OptionPane.show(get(date), 600, 400);
    }

    public static JPanel get(Date date) throws IOException {

        String path = "log" + File.separator + DateFormat.getDateInstance(DateFormat.DEFAULT, new Locale("pt", "BR")).format(date).replace("/", "_").concat(".html");

        JEditorPane editor = new JEditorPane("text/html", Utils.readFile(path));
        editor.setEditable(false);
        editor.addHyperlinkListener(new HyperlinkListener() {

            @Override
            public void hyperlinkUpdate(HyperlinkEvent e) {
                if (e.getEventType().equals(HyperlinkEvent.EventType.ACTIVATED)) {
                    try {
                        Desktop.getDesktop().open(new File(e.getDescription()));
                    } catch (IllegalArgumentException | IOException ex) {
                        OptionPane.error(ex);
                    }
                }
            }
        });

        JPanel panel = new JPanel(new BorderLayout());
        panel.add(new JScrollPane(editor));

        return panel;
    }
}
