/*
 Tiamat - a role-playing game framework
 EventUtilities.java - contains static utility functions used by events
 
 Copyright  (C) 2006 Hugues Johnson
 
 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 2
 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, write to the Free Software
 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

package com.huguesjohnson.tiamat.tools.adventureStudio.event;

import java.io.File;

import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileFilter;
import com.huguesjohnson.FileCopy;
import com.huguesjohnson.FileDelete;

/** EventUtilities - contains static utility functions used by events
 * @author Hugues Johnson
 */
public abstract class EventUtilities{
	
	public final static boolean confirmDelete(String fileName,String targetDir){
		//prompt
		String message="Do you want to remove "+fileName+" from "+targetDir+"?";
		String title="Confirm Remove";
		int result=JOptionPane.showConfirmDialog(null,message,title,JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE);
		if(result==JOptionPane.NO_OPTION){
			return(false);
		} 
		FileDelete.deleteFile(targetDir+fileName);
		return(true);
	}
	
	public final static String promptForName(String fileType){
		Object nameObj=JOptionPane.showInputDialog(
				null,"Enter a name for the new "+fileType,"New "+fileType,
				JOptionPane.QUESTION_MESSAGE);
		if(nameObj==null){
			return(null);
		}
		return((String)nameObj);
	}
	
	public final static String renameFile(String fileName,String targetDir){
		//preserve the file extension
		int index=fileName.indexOf(".");
		String extension=fileName.substring(index);
		String currentName=fileName.substring(0,index);
		Object newNameObj=JOptionPane.showInputDialog(
				null,"Enter new name","Rename",
				JOptionPane.QUESTION_MESSAGE,
                new ImageIcon(EventUtilities.class.getResource("/com/huguesjohnson/tiamat/tools/images/rename.gif")),
                null,currentName);
		if(newNameObj==null){
			return(null);
		}
		String newName=(String)newNameObj+extension;
		//rename the file
		File f=new File(targetDir+fileName);
		f.renameTo(new File(targetDir+newName));
		return(newName);
	}
	
	public final static void showErrorDialog(Exception x){
		StringBuffer message=new StringBuffer();
		message.append("The following error has occured:\n");
		message.append("  ");
		message.append(x.getMessage()); 
		message.append("\n\nPartial Stack Trace:");
		StackTraceElement[] trace=x.getStackTrace();
		int length=10;
		if(trace.length<length){ length=trace.length; }
		for(int index=0;index<length;index++){
			message.append("\n  ");
			message.append(trace[index].toString());
		}
		JOptionPane.showMessageDialog(null,message.toString(),"Error",JOptionPane.ERROR_MESSAGE);
	}
	
	public final static String importFile(FileFilter filter,String targetDir){
		JFileChooser fileChooser=new JFileChooser();
	    fileChooser.setDialogTitle("Import File");
	    //do not allow multiple selections
	    fileChooser.setMultiSelectionEnabled(false);
	    //set file filters
	    fileChooser.setAcceptAllFileFilterUsed(false);
	    fileChooser.setFileFilter(filter);
	    //set the current directory
        String lastPath=System.getProperty("last-import-path");
        if(lastPath==null){
            lastPath=System.getProperty("user.dir");
        }
        fileChooser.setCurrentDirectory(new File(lastPath));
        //show the file chooser
	    int returnValue=fileChooser.showOpenDialog(null);
	    //was 'OK' selected?
	    if(returnValue==JFileChooser.APPROVE_OPTION){
	    	//copy the file
	    	String dir=fileChooser.getCurrentDirectory().getPath();
	    	if(!dir.endsWith(File.separator)){
	    		dir=dir+File.separator;
	    	}
            System.setProperty("last-import-path",dir);
            String name=fileChooser.getSelectedFile().getName();
            FileCopy.copyFile(dir+name,targetDir+name);
            return(name);
	    } 
	    return(null);
	}
}