/*
 * Copyright (C) 2017 favdb
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package storybook.ui.dialog;

import i18n.I18N;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.event.ActionEvent;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import api.mig.swing.MigLayout;
import resources.icons.ICONS;
import resources.icons.IconUtil;
import storybook.tools.LOG;
import storybook.tools.clip.ClipHtml;
import storybook.ui.MIG;

/**
 *
 * @author favdb
 */
public class ExceptionDlg extends AbstractDialog {

	private final String msg;
	private final Exception e;
	private JTextArea ta;

	public ExceptionDlg() {
		super();
		this.msg = "";
		this.e = null;
	}

	public ExceptionDlg(Exception e) {
		super();
		this.msg = "";
		this.e = e;
		initAll();
	}

	public ExceptionDlg(String msg, Exception e) {
		super();
		this.msg = msg;
		this.e = e;
		initAll();
	}

	@Override
	public void init() {
		super.initUi();
		LOG.err(msg, e);
	}

	@Override
	public void initUi() {
		setLayout(new MigLayout(MIG.get(MIG.WRAP, MIG.HIDEMODE2), "[grow]"));
		StringBuilder buf = new StringBuilder();
		if (!msg.isEmpty()) {
			buf.append(msg).append("\n\n");
		}
		if (e != null) {
			buf.append("Exception Message:\n");
			buf.append(e.getLocalizedMessage());
			buf.append("\n\nStack Trace:\n");
			for (StackTraceElement st : e.getStackTrace()) {
				buf.append(st.toString()).append("\n");
			}
		}
		buf.append(I18N.getMsg("exception.to.report"));
		//LOG.trace(buf.toString());
		ta = new JTextArea();
		ta.setEditable(false);
		ta.setText(buf.toString());
		ta.setCaretPosition(0);
		JScrollPane scroller = new JScrollPane(ta);
		scroller.setPreferredSize(new Dimension(640, 480));
		add(scroller, MIG.get(MIG.SPAN, MIG.GROW));
		JButton btCopy = new JButton(I18N.getMsg("file.info.copy.text"));
		btCopy.addActionListener((java.awt.event.ActionEvent evt) -> {
			ClipHtml selection = new ClipHtml(ta.getText());
			Clipboard clbrd = Toolkit.getDefaultToolkit().getSystemClipboard();
			clbrd.setContents(selection, selection);
			JOptionPane.showMessageDialog(this,
					I18N.getMsg("copied.title"),
					"Exception",
					JOptionPane.INFORMATION_MESSAGE);
		});
		JButton btExit = new JButton(I18N.getMsg("close"));
		btExit.setIcon(IconUtil.getIconSmall(ICONS.K.CLOSE));
		btExit.addActionListener((java.awt.event.ActionEvent evt) -> {
			dispose();
		});
		add(btCopy, MIG.get(MIG.SPLIT2, MIG.RIGHT));
		add(btExit);
		setTitle("Exception");
		setIconImage(IconUtil.getIconImageSmall(ICONS.K.ERROR));
		pack();
		this.setPreferredSize(new Dimension(320, 240));
		setLocationRelativeTo(null);
	}

	public static void show(String msg, Exception ex) {
		new ExceptionDlg(msg, ex).setVisible(true);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// empty
	}

}
