package be.msyben.vinylManager.gui;

import be.msyben.vinylManager.gui.icon.Icons;
import be.msyben.vinylManager.properties.Language;
import be.msyben.vinylManager.tools.Tools;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

/**
 * fenêtre d'erreur
 * @author Mathieu Syben
 */
public class ErrorWindow extends JFrame {

    /**
     * uid
     */
    private static final long serialVersionUID = 7l;
    /** Le message */
    private String message;
    /** L'exception */
    private Exception exception;
    /**
     * Le parent
     */
    private JFrame parent;
    /** Le log panel */
    private JScrollPane logPane = new JScrollPane();
    /** Le log */
    private JTextArea log = new JTextArea();

    /**
     * Creates a new instance of ErrorWindow
     * @param parent la fenêtre principale
     * @param message Le message à afficher
     */
    public ErrorWindow(JFrame parent, String message) {
        this(parent, message, null);
    }

    /**
     * Creates a new instance of ErrorWindow
     * @param parent la fenêtre principale
     * @param message Le message à afficher
     * @param exception exception
     */
    public ErrorWindow(JFrame parent, String message, Exception exception) {

        super(Language.getInstance().get("error.window.title"));
        setIconImage(Icons.getInstance().icon.getImage());
        this.parent = parent;
        if (this.parent != null) {
            this.parent.setEnabled(false);
        }
        this.message = message;
        this.exception = exception;

        this.setSize(400, 170); //On donne une taille à notre fenêtre
        this.setLocationRelativeTo(null); //On centre la fenêtre sur l'ecran
        this.setResizable(false); //On interdit la redimensionnement de la fenêtre
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); //On dit à l'application de se fermer
        this.setLayout(null);


        putComponents();
        toFront();
        setVisible(true);

    }

    private void putComponents() {
        Container c = this.getContentPane();

        if (message.contains("|")) {
            String[] msgs = message.split("\\|");
            int h = 75 / msgs.length;
            for (int i = 0; i < msgs.length; i++) {
                JLabel txt = new JLabel(msgs[i]);
                txt.setBounds(5, 5 + (i * h), 390, h);
                txt.setHorizontalAlignment(JLabel.CENTER);
                txt.setVerticalAlignment(JLabel.CENTER);
                c.add(txt);
            }

        } else {

            JLabel txt = new JLabel(message);
            txt.setBounds(5, 5, 390, 75);
            txt.setHorizontalAlignment(JLabel.CENTER);
            txt.setVerticalAlignment(JLabel.CENTER);
            c.add(txt);
        }

        JButton ok = new JButton(Language.getInstance().get("button.ok"));
        ok.setBounds(150, 80, 100, 25);
        c.add(ok);
        ok.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                dispose();
                if (parent != null) {
                    parent.setEnabled(true);
                    parent.toFront();
                }
            }
        });

        if (exception != null) {
            final JCheckBox afficheException = new JCheckBox(Language.getInstance().get("afficher.log"));
            afficheException.setBounds(5, 100, 140, 25);
            afficheException.setSelected(false);
            c.add(afficheException);
            afficheException.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    if (afficheException.isSelected()) {
                        setSize(400, 470);
                        log.setText(Tools.getStackTrace(exception));
                        logPane.setBounds(5, 150, 385, 270);
                        logPane.getViewport().setView(log);
                        getContentPane().add(logPane);
                    } else {
                        setSize(400, 170);
                        getContentPane().remove(logPane);
                    }

                }
            });
        }

        this.addWindowListener(new WindowAdapter() {

            @Override
            public void windowClosing(WindowEvent e) {
                dispose();
                if (parent != null) {
                    parent.setEnabled(true);
                    parent.toFront();
                }
            }
        });

        KeyListener kl = new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                    dispose();
                    if (parent != null) {
                        parent.setEnabled(true);
                        parent.toFront();
                    }
                }
            }
        };

        ok.addKeyListener(kl);

        this.addKeyListener(kl);

    }
}
