/*
Tiamat - a role-playing game framework
GameScriptEditorFrame.java - used to edit a GameScript
 
 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;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.filechooser.FileFilter;

import com.huguesjohnson.fileFilter.XmlFileFilter;
import com.huguesjohnson.serialization.XmlFormatter;
import com.huguesjohnson.tiamat.GameScript;
import com.huguesjohnson.tiamat.tools.scriptTable.ScriptTable;
import com.huguesjohnson.ui.FileBrowsePanel;

//TODO - this is incomplete because it's not yet used a stand-alone editor, this is only available through the Adventure Studio so it has a limited amount of functionality

/** 
 * GameScriptEditorFrame - used to edit a GameScript
 * @author Hugues Johnson
 */
public class GameScriptEditorFrame extends BaseEditorFrame{
	/* 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=-6960727304377171885L;

	private FileBrowsePanel browsePanel;
	private ScriptTable scriptTable;
	private JButton removeButton;
	private JButton editButton;
		
	public GameScriptEditorFrame(){
		super("GameScript");
	}

	public GameScriptEditorFrame(String editorName){
		super(editorName);
	}

	public GameScriptEditorFrame(String editorName,String filePath){
		super(editorName);
		this.openGameScriptFile(filePath);
	}

	@Override
	protected void initComponents(){
		this.panelContent.setLayout(new BoxLayout(this.panelContent,BoxLayout.Y_AXIS));
		//add the browse panel
		FileFilter[] filters=new FileFilter[1];
		filters[0]=new XmlFileFilter();
		this.browsePanel=new FileBrowsePanel("Configuration File",filters,false);
		this.panelContent.add(this.browsePanel);
		//add script table
		JPanel tablePanel=new JPanel();
		tablePanel.setLayout(new BoxLayout(tablePanel,BoxLayout.Y_AXIS));
		this.scriptTable=new ScriptTable();
		JScrollPane scrollPane=new JScrollPane(this.scriptTable);
		tablePanel.add(scrollPane);
		this.panelContent.add(tablePanel);
		//add buttons
		JPanel buttonPanel=new JPanel();
		buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
    	JButton addButton=new JButton();
    	addButton.setIcon(new ImageIcon(this.getClass().getResource("/com/huguesjohnson/tiamat/tools/images/plus.gif")));
    	addButton.setToolTipText("Add");
    	addButton.addActionListener(new ActionListener(){
    		public void actionPerformed(ActionEvent e){
    			onAddButtonClick();
    		}
    	});
    	addButton.setEnabled(true);
    	buttonPanel.add(addButton);
    	this.editButton=new JButton();
    	this.editButton.setIcon(new ImageIcon(this.getClass().getResource("/com/huguesjohnson/tiamat/tools/images/edit.gif")));
    	this.editButton.setToolTipText("Edit");
    	this.editButton.addActionListener(new ActionListener(){
    		public void actionPerformed(ActionEvent e){
    			onEditButtonClick();
    		}
    	});
    	this.editButton.setEnabled(false);
    	buttonPanel.add(this.editButton);
    	this.removeButton=new JButton();
    	this.removeButton.setIcon(new ImageIcon(this.getClass().getResource("/com/huguesjohnson/tiamat/tools/images/minus.gif")));
    	this.removeButton.setToolTipText("Remove");
    	this.removeButton.addActionListener(new ActionListener(){
    		public void actionPerformed(ActionEvent e){
    			onRemoveButtonClick();
    		}
    	});
    	this.removeButton.setEnabled(false);
    	buttonPanel.add(this.removeButton);
		this.panelContent.add(buttonPanel);
		//pack so everything should be visible
		this.pack();
		//disable save by default
		this.disableSave();
	}	
	
	private boolean openGameScriptFile(String filePath){
		boolean success=true;
		String currentFilePath=new String(this.browsePanel.getFilePath());
		try{
			XmlFormatter formatter=new XmlFormatter();
			GameScript script=(GameScript)formatter.loadFrom(filePath);
			this.scriptTable.loadScript(script);
			if(this.scriptTable.getRowCount()>0){
				this.enableSave();
				this.scriptTable.setRowSelectionInterval(0,0);
				this.removeButton.setEnabled(true);
				this.editButton.setEnabled(true);
			}
			//update the browse panel if needed
			if(!currentFilePath.equals(filePath)){
				this.browsePanel.setFilePath(filePath);
			}
		} catch(Exception x){
			x.printStackTrace();
			success=false;
		}
		return(success);
	}	
	
	@Override
	public void onSave(){
		try{
			GameScript updatedScript=this.scriptTable.getScript();
			(new XmlFormatter()).saveTo(updatedScript,this.browsePanel.getFilePath());
		} catch(Exception x){
			x.printStackTrace();
		}
	}

	@Override
	public void onViewXml(){
		String filePath="file:///"+this.browsePanel.getFilePath();
		this.viewXml(filePath);
	}
	
	public void onAddButtonClick(){
		this.scriptTable.onAdd();
		if(this.scriptTable.getRowCount()>0){
			this.enableSave();
			this.scriptTable.setRowSelectionInterval(0,0);
			this.removeButton.setEnabled(true);
			this.editButton.setEnabled(true);
		}
	}
	
	public void onEditButtonClick(){
		this.scriptTable.onEdit(this.scriptTable.getSelectedRow());
	}

	public void onRemoveButtonClick(){
		this.scriptTable.onDelete(this.scriptTable.getSelectedRow());
		if(this.scriptTable.getRowCount()<1){
			this.disableSave();
			this.removeButton.setEnabled(false);
			this.editButton.setEnabled(false);
		}
	}
	
//	TODO the rest of these overrides are not implemented	

	@Override
	protected void onSaveAs(){
		// TODO Auto-generated method stub

	}
	
	@Override
	protected void onNew(){
		// TODO Auto-generated method stub

	}

	@Override
	protected void onOpen(){
		// TODO Auto-generated method stub

	}

	@Override
	protected void onRecentFile(String filePath){
		// TODO Auto-generated method stub

	}

//	//for testing
//	public static void main(String[] args){
//		(new GameScriptEditorFrame("GameScript","D:\\AdventureStudioTest\\camineet.script.xml")).setVisible(true);
//	}
}