/*
Tiamat - a role-playing game framework
ScriptTableEntryDialog.java - dialog for editing a ScriptTableKey and its resulting ScriptTableValues
 
 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.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.TitledBorder;

import com.huguesjohnson.tiamat.ScriptTableKey;
import com.huguesjohnson.tiamat.ScriptTableValue;
import com.huguesjohnson.tiamat.tools.ConditionsPanel;
import com.huguesjohnson.tiamat.tools.EventDialog;
import com.huguesjohnson.tiamat.tools.EventPanel;

/**
 * ScriptTableEntryDialog - dialog for editing a ScriptTableKey and its resulting ScriptTableValues
 * @author Hugues Johnson
 */
public abstract class ScriptTableEntryDialog extends JDialog{
	/* Determines if a de-serialized file is compatible with this class. See <a href="http://java.sun.com/j2se/1.5.0/docs/guide/serialization/spec/class.html">Java Object Serialization Specifications</a>. */
	private static final long serialVersionUID=8119099600467487449L;

	private boolean cancel;
	public void setCancel(boolean cancel){ this.cancel=cancel; }
	public boolean getCancel(){ return(this.cancel); }

	private EventPanel eventPanel;
	private ConditionsPanel conditionsPanel;
	private ScriptTableValueTable valueTable;
	private EventDialog eventDialog;
	private int valueTableRowIndex;
	
	private JButton valueEditButton;
	private JButton valueRemoveButton;
	
	public ScriptTableEntryDialog(){
		this.initComponents();
	}	
	
	public ScriptTableEntryDialog(ScriptTableKey key,ScriptTableValue value){
		this.initComponents();
		this.setEntry(key,value);
	}
	
	@SuppressWarnings("serial")
	private void initComponents(){
		//set properties
		this.setTitle("Script Entry");
		this.setModal(true);
		this.setResizable(false);
		this.getContentPane().setLayout(new BoxLayout(this.getContentPane(),BoxLayout.Y_AXIS));
		//add listener for the window closing event
    	this.addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent windowEvent){
        		setCancel(true);
            	setVisible(false);
            }
        });	
    	//add panel for the script table key
    	JPanel scriptTableKeyPanel=new JPanel();
    	scriptTableKeyPanel.setLayout(new BoxLayout(scriptTableKeyPanel,BoxLayout.Y_AXIS));
    	scriptTableKeyPanel.setBorder(new TitledBorder("Key"));
    	this.eventPanel=new EventPanel();
    	scriptTableKeyPanel.add(this.eventPanel);
    	this.conditionsPanel=new ConditionsPanel();
    	scriptTableKeyPanel.add(this.conditionsPanel);
    	this.getContentPane().add(scriptTableKeyPanel);
    	//add panel for the script table value
    	JPanel scriptTableValuePanel=new JPanel();
    	scriptTableValuePanel.setLayout(new BoxLayout(scriptTableValuePanel,BoxLayout.Y_AXIS));
    	scriptTableValuePanel.setBorder(new TitledBorder("Value"));
    	this.valueTable=new ScriptTableValueTable(new Dimension(80,70));
    	scriptTableValuePanel.add(new JScrollPane(this.valueTable));
    	JPanel valueButtonPanel=new JPanel();
    	valueButtonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
    	JButton addValueButton=new JButton();
    	addValueButton.setIcon(new ImageIcon(this.getClass().getResource("/com/huguesjohnson/tiamat/tools/images/plus.gif")));
    	addValueButton.setToolTipText("Add");
    	addValueButton.addActionListener(new ActionListener(){
    		public void actionPerformed(ActionEvent e){
    			onValueAdd();
    		}
    	});
    	addValueButton.setEnabled(true);
    	valueButtonPanel.add(addValueButton);
    	this.valueEditButton=new JButton();
    	this.valueEditButton.setIcon(new ImageIcon(this.getClass().getResource("/com/huguesjohnson/tiamat/tools/images/edit.gif")));
    	this.valueEditButton.setToolTipText("Edit");
    	this.valueEditButton.addActionListener(new ActionListener(){
    		public void actionPerformed(ActionEvent e){
    			onValueEdit();
    		}
    	});
    	this.valueEditButton.setEnabled(false);
    	valueButtonPanel.add(this.valueEditButton);
    	this.valueRemoveButton=new JButton();
    	this.valueRemoveButton.setIcon(new ImageIcon(this.getClass().getResource("/com/huguesjohnson/tiamat/tools/images/minus.gif")));
    	this.valueRemoveButton.setToolTipText("Remove");
    	this.valueRemoveButton.addActionListener(new ActionListener(){
    		public void actionPerformed(ActionEvent e){
    			onValueDelete();
    		}
    	});
    	this.valueRemoveButton.setEnabled(false);
    	valueButtonPanel.add(this.valueRemoveButton);
    	scriptTableValuePanel.add(valueButtonPanel);
    	this.getContentPane().add(scriptTableValuePanel);
    	//add the OK and Cancel buttons
    	JPanel buttonPanel=new JPanel();
    	buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
    	JButton okButton=new JButton("OK");
    	okButton.setMnemonic('O');
    	okButton.addActionListener(new ActionListener(){
    		public void actionPerformed(ActionEvent e){
        		setCancel(false);
            	setVisible(false);
    		}
    	});
    	buttonPanel.add(okButton);
    	JButton cancelButton=new JButton("Cancel");
    	cancelButton.setMnemonic('C');
    	cancelButton.addActionListener(new ActionListener(){
    		public void actionPerformed(ActionEvent e){
        		setCancel(true);
            	setVisible(false);
    		}
    	});
    	buttonPanel.add(cancelButton);
    	this.getContentPane().add(buttonPanel);
    	//pack so everything is visible
    	this.pack();
	}
	
	public void setEntry(ScriptTableKey key,ScriptTableValue value){
		this.eventPanel.setEvent(key.event);
		this.conditionsPanel.setConditions(key.conditions);
		this.valueTable.setScriptTableValue(value);
		if(value.events.size()>0){
			this.valueTable.setRowSelectionInterval(0,0);
			this.valueEditButton.setEnabled(true);
			this.valueRemoveButton.setEnabled(true);
		}
	}

	public ScriptTableKey getKey(){
		return(new ScriptTableKey(this.eventPanel.getEvent(),this.conditionsPanel.getConditions()));
	}
	
	public ScriptTableValue getValue(){
		return(this.valueTable.getScriptTableValue());
	}

	@Override
	public void setVisible(boolean visible){
		super.setVisible(visible);
		if(visible==false){
			this.onHide();
		}
	}

	@SuppressWarnings("serial")
	public void onValueAdd(){
		this.valueTableRowIndex=-1;
		this.eventDialog=new EventDialog(null){
			public void onOk(){ onEventDialogOk(); }
			public void onCancel(){ onEventDialogCancel(); }
		};
		this.eventDialog.setModal(true);
		this.eventDialog.setVisible(true);
	}
	
	@SuppressWarnings("serial")
	public void onValueEdit(){
		this.valueTableRowIndex=this.valueTable.getSelectedRow();
		this.eventDialog=new EventDialog(null){
			public void onOk(){ onEventDialogOk(); }
			public void onCancel(){ onEventDialogCancel(); }
		};
		this.eventDialog.setModal(true);
		this.eventDialog.setEvent(this.valueTable.getSelectedItem());
		this.eventDialog.setVisible(true);
	}
	
	public void onValueDelete(){
		int selectedRow=this.valueTable.getSelectedRow();
		this.valueTable.deleteSelectedItem();
		if(this.valueTable.getRowCount()<=0){
			this.valueEditButton.setEnabled(false);
			this.valueRemoveButton.setEnabled(false);
		} else{
			if(selectedRow>0){
				selectedRow--;
				this.valueTable.setRowSelectionInterval(selectedRow,selectedRow);
			} else{
				this.valueTable.setRowSelectionInterval(0,0);
			}
		}
	}
	
	public void onEventDialogOk(){
		if(this.valueTableRowIndex<0){
			this.valueTable.addItem(this.eventDialog.getEvent());
			this.valueEditButton.setEnabled(true);
			this.valueRemoveButton.setEnabled(true);
			int rowIndex=this.valueTable.getRowCount()-1;
			this.valueTable.setRowSelectionInterval(rowIndex,rowIndex);
		} else{
			this.valueTable.setItem(this.valueTableRowIndex,this.eventDialog.getEvent());
		}
		this.eventDialog.setVisible(false);
	}
	
	public void onEventDialogCancel(){
		this.eventDialog.setVisible(false);
	}

	//override this in implementing class
	public abstract void onHide();
}