package view;

import net.miginfocom.swing.MigLayout;
import org.sig.fw.layout.component.SButton;
import org.sig.fw.layout.component.SLabel;
import org.sig.fw.layout.component.STextArea;
import org.sig.fw.layout.contener.SDialog;
import org.sig.fw.layout.pane.SScrollPane;
import org.sig.fw.log.common.LogTools;
import org.sig.fw.log.model.Definition;
import org.sig.fw.log.model.Log;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class LogView extends SDialog 
{
	private static final long serialVersionUID = -4196356976803900482L;

	public LogView(int level) 
	{
		super("Crash de l'application", true);
		setBounds(100, 100, 700, 500);
		getContentPane().setLayout(new MigLayout("", "[][grow]", "[][][grow][]"));
		
		SLabel lblNewLabel_2 = new SLabel("", SLabel.TYPE_COMMUN);
		lblNewLabel_2.setIcon(new ImageIcon(LogView.class.getResource("/com/sun/javafx/scene/control/skin/caspian/dialog-error.png")));
		getContentPane().add(lblNewLabel_2, "cell 0 0");
		
		String text = "";
		if(level == Definition.LEVEL_FATAL)
			text = "<html>L'application a rencontré un problème et doit fermer...<br>Veuillez contacter SIG avec les informations suivantes.</hmtl>";
		else
			text = "<html>L'application a rencontré un problème...<br>Veuillez contacter SIG avec les informations suivantes.</html>";
		
		SLabel lblNewLabel = new SLabel(text, SLabel.TYPE_COMMUN_BOLD);
		getContentPane().add(lblNewLabel, "cell 1 0,alignx center");
		
		SLabel lblNewLabel_1 = new SLabel("Dernières actions effectuées sur l'application.", SLabel.TYPE_COMMUN);
		getContentPane().add(lblNewLabel_1, "cell 0 1 2 1,alignx center,aligny bottom");
		
		SScrollPane scrollPane = new SScrollPane();
		getContentPane().add(scrollPane, "cell 0 2 2 1,grow");
		
		STextArea textArea = new STextArea();
		textArea.setEditable(false);
		scrollPane.setViewportView(textArea);
		
		text = "";
		for(Log log : LogTools.logs) {
			text += log;
		}
		
		textArea.setText(text);	
		
		SButton btnNewButton = new SButton("Fermer", "");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0)
			{
				dispose();
			}
		});
		getContentPane().add(btnNewButton, "cell 0 3 2 1,alignx center");
		computePosition();
	}

}
