/*
Tiamat - a role-playing game framework
ScriptTableUtilities.java - contains utility functions for ScriptTable
 
 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.scriptTable;

import java.util.ArrayList;

import javax.swing.JOptionPane;

import com.huguesjohnson.tiamat.Event;
import com.huguesjohnson.tiamat.ScriptTableValue;
import com.huguesjohnson.tiamat.StringTable;

/**
 * ScriptTableUtilities - contains utility functions for ScriptTable
 * @author Hugues Johnson
 */
public abstract class ScriptTableUtilities{

	public static String getParametersString(StringTable parameters){
		StringBuffer parametersString=new StringBuffer();
		String[][] allValues=parameters.getAll();
		if(allValues!=null){
			if(allValues.length==2){
				int length=allValues[0].length;
				if(length>0){
					parametersString.append(allValues[0][0]);
					parametersString.append("=");
					parametersString.append(allValues[1][0]);
					for(int index=1;index<length;index++){
						parametersString.append("\n");
						parametersString.append(allValues[0][index]);
						parametersString.append("=");
						parametersString.append(allValues[1][index]);
					}				
				}
			}
		}
		return(parametersString.toString());
	}

	public static String getConditionsString(ArrayList<String> conditions){
		StringBuffer conditionsString=new StringBuffer();
		if(conditions!=null){
			int length=conditions.size();
			if(length>=1){
				conditionsString.append(conditions.get(0));
				for(int index=1;index<length;index++){
					conditionsString.append("\n");
					conditionsString.append(conditions.get(index));
				}
			}
		}
		return(conditionsString.toString());
	}	

	public static String getValueString(ScriptTableValue value){
		StringBuffer valueString=new StringBuffer();
		ArrayList<Event> events=value.events;
		if(value!=null){
			int length=events.size();
			if(length>=1){
				valueString.append(events.get(0).getAction());
				String parametersString=ScriptTableUtilities.getParametersString(events.get(0).getParameters());
				if(parametersString.length()>0){
					valueString.append("\n");
					valueString.append(parametersString);
				}
				for(int index=1;index<length;index++){
					valueString.append("\n\n");
					valueString.append(events.get(index).getAction());
					parametersString=ScriptTableUtilities.getParametersString(events.get(index).getParameters());
					if(parametersString.length()>0){
						valueString.append("\n");
						valueString.append(parametersString);
					}
				}
			}
		}
		return(valueString.toString());
	}
	
	public final static boolean confirmDelete(){
		//prompt
		String message="Do you want to remove the selected row?\nThis operation can not be undone.";
		String title="Confirm Delete";
		int result=JOptionPane.showConfirmDialog(null,message,title,JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE);
		if(result==JOptionPane.NO_OPTION){
			return(false);
		} 
		return(true);
	}
}