/*
 *  Draco Econometrics
 *  Copyright (C) 2008-2009 Approximatrix, LLC
 *  http://www.approximatrix.com
 *  
 *  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 com.approximatrix.draco.application.plugin.ui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;

import org.apache.bsf.BSFException;
import org.jruby.exceptions.RaiseException;

public class PluginErrorDialog extends JDialog implements ActionListener {
	
	private static final String PLUGIN_ERROR_DEFAULT = "Plugin Error";
	private static final String[] PLUGIN_ERROR_MESSAGE = { 
		"An error has occurred while processing a Plugin.",
		"The operation cannot proceed."};
	
	private Exception e;
	private String contact;
	
	private JButton closeButton = null;
	
	private JButton detailsButton = null;
	private JPanel acceptsDetails = null;
	private JScrollPane detailsPane = null;
	private JTextArea details = null;
	
	public PluginErrorDialog(Frame owner, Exception e) {
		this(owner, PLUGIN_ERROR_DEFAULT,e);
	}
	
	public PluginErrorDialog(Frame owner, String title, Exception e) {
		super(owner,title,true);
		contact = null;
		this.e = e;
		initialize();
		this.setLocationRelativeTo(owner);
	}
	
	public PluginErrorDialog(Frame owner, String title, String contact, Exception e) {
		this(owner,title,e);
		contact = null;
	}

	private void initialize() {
		this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		this.setSize(new Dimension(400,300));
		
		this.getContentPane().setLayout(new BorderLayout());
		
		JPanel lPanel = new JPanel();
		lPanel.setBorder(BorderFactory.createEmptyBorder(10, 5, 10, 5));
		BoxLayout layout = new BoxLayout(lPanel,BoxLayout.Y_AXIS);
		lPanel.setLayout(layout);
		for(int i=0;i<PLUGIN_ERROR_MESSAGE.length;i++) {
			JPanel one = new JPanel();
			one.setLayout(new FlowLayout(FlowLayout.LEFT));
			one.add(new JLabel(PLUGIN_ERROR_MESSAGE[i]));
			lPanel.add(one);
		}
		this.getContentPane().add(lPanel,BorderLayout.NORTH);
		
		acceptsDetails = new JPanel();
		acceptsDetails.setLayout(new BorderLayout());
		acceptsDetails.setBorder(BorderFactory.createEmptyBorder(10, 5, 10, 5));
		
		JPanel bPanel2 = new JPanel();
		bPanel2.setLayout(new FlowLayout());
		((FlowLayout)bPanel2.getLayout()).setAlignment(FlowLayout.LEFT);
		detailsButton = new JButton("Details >>");
		detailsButton.addActionListener(this);
		bPanel2.add(detailsButton);
		//acceptsDetails.add(bPanel2,BorderLayout.NORTH);
		
		String message = e.getMessage();
		if(e instanceof RaiseException) {
			RaiseException re = (RaiseException)e;
			StringBuffer sbe = new StringBuffer();
			sbe.append(re.getException().message.asJavaString());
			sbe.append("\n\n");
			sbe.append("Stacktrace:\n");
			
			String stacktrace = re.getException().getBacktrace().toString().replaceAll("file:", "\nfile:");
			
			sbe.append(stacktrace);
			message = sbe.toString();
		}
		//	message = ((BSFException)e).get
		
		
		details = new JTextArea(message);
		details.setEditable(false);
		
		detailsPane = new JScrollPane(details);
		detailsPane.setMinimumSize(new Dimension(180,100));
		acceptsDetails.add(detailsPane,BorderLayout.CENTER);
		
		this.getContentPane().add(acceptsDetails,BorderLayout.CENTER);
		
		JPanel cPanel = new JPanel();
		cPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
		closeButton = new JButton("Close");
		closeButton.addActionListener(this);
		cPanel.add(closeButton);
		
		this.getContentPane().add(cPanel,BorderLayout.SOUTH);
		
	}

	public void actionPerformed(ActionEvent arg0) {
		if(arg0.getSource() == detailsButton && detailsPane.getParent() == null) {
			acceptsDetails.add(this.detailsPane,BorderLayout.CENTER);
			detailsPane.doLayout();
			this.doLayout();
		} else if(arg0.getSource() == detailsButton) {
			acceptsDetails.remove(this.detailsPane);
			acceptsDetails.repaint();
		} else if(arg0.getSource() == closeButton) {
			this.dispose();
		}
		
	}
	
}

