/*
Tiamat - a role-playing game framework
ItemEditorFrame.java - used to edit an Item
 
 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 javax.swing.BoxLayout;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileFilter;

import com.huguesjohnson.PathResolver;
import com.huguesjohnson.fileFilter.XmlFileFilter;
import com.huguesjohnson.serialization.XmlFormatter;
import com.huguesjohnson.tiamat.Item;
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

/** ItemEditorFrame - used to edit an Item
 * @author Hugues Johnson
 */
public class ItemEditorFrame 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=6194666927467329422L;

	private ItemPanel itemPanel;
	private FileBrowsePanel browsePanel;
	
	public ItemEditorFrame(){
		super("Item");
	}

	public ItemEditorFrame(String itemPath){
		super("Item");
		this.openItem(itemPath);
	}
	
	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("Item File",filters,false);
		this.panelContent.add(this.browsePanel);
		//add the item panel
		this.itemPanel=new ItemPanel();		
		this.panelContent.add(this.itemPanel);
		//pack so everything should be visible
		this.pack();
		//disable save by default
		this.disableSave();
	}
	
	public void onSave(){
		//get the item from the panel
		Item item=this.itemPanel.getItem();
		//fix up the paths
		String absoluteDir=this.browsePanel.getDirectory();
		String thumbnailPathAbs=item.getThumbnailImagePathAbsolute();
		item.setThumbnailImagePath(PathResolver.getRelativePath(absoluteDir,thumbnailPathAbs));
		String fullPathAbs=item.getFullImagePathAbsolute();
		item.setFullImagePath(PathResolver.getRelativePath(absoluteDir,fullPathAbs));
		item.setAbsoluteDir(absoluteDir);
		//now save
		XmlFormatter formatter=new XmlFormatter();
		try{
			formatter.saveTo(item,this.browsePanel.getFilePath());
		} catch(Exception x){
			x.printStackTrace();
			JOptionPane.showMessageDialog(this,"Error saving item:"+x.getMessage(),"Error",JOptionPane.ERROR_MESSAGE);
		}		
	}

	@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

	}

	public void onViewXml(){
		String filePath="file:///"+this.browsePanel.getFilePath();
		this.viewXml(filePath);
	}

	public boolean openItem(String itemPath){
		boolean success=true;
		try{
			XmlFormatter formatter=new XmlFormatter();
			Item item=(Item)formatter.loadFrom(itemPath);
			this.openItem(item);
		} catch(Exception x){
			x.printStackTrace();
			JOptionPane.showMessageDialog(this,"Error opening item:"+x.getMessage(),"Error",JOptionPane.ERROR_MESSAGE);
			success=false;
		}
		if(success){
			this.browsePanel.setFilePath(itemPath);
		}
		return(success);
	}
	
	public void openItem(Item item){
		this.itemPanel.setItem(item);
	}
	
//	public static void main(String[] args){
//		(new ItemEditorFrame()).setVisible(true);
//	}

}