/*
Tiamat - a role-playing game framework
LocationEditorFrame.java - used to edit a Location
 
 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.JPanel;
import javax.swing.filechooser.FileFilter;

import com.huguesjohnson.PathResolver;
import com.huguesjohnson.fileFilter.MidiFileFilter;
import com.huguesjohnson.fileFilter.XmlFileFilter;
import com.huguesjohnson.serialization.XmlFormatter;
import com.huguesjohnson.tiamat.Location;
import com.huguesjohnson.tiamat.LocationExit;
import com.huguesjohnson.tiamat.LocationObjectData;
import com.huguesjohnson.ui.FileBrowsePanel;
import com.huguesjohnson.ui.ImageBrowsePanel;

//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
/** LocationEditorFrame - used to edit a Location
 * @author Hugues Johnson
 */
public class LocationEditorFrame 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=2145687741096787832L;

	//ui components
	FileBrowsePanel browsePanel;
	ImageBrowsePanel bgImageBrowsePanel;
	ImageBrowsePanel overlayImageBrowsePanel;
	FileBrowsePanel bgMusicBrowsePanel;
	LocationExitListPanel exitList;
	LocationObjectDataListPanel characterList;
	LocationObjectDataListPanel itemList;
	
	public LocationEditorFrame(){
		super("Location");
	}

	public LocationEditorFrame(String locationPath){
		super("Location");
		this.openLocation(locationPath);
	}
	
	public void onSave(){
		Location location=new Location();
		String absoluteDir=this.browsePanel.getDirectory();
		location.setAbsoluteDir(absoluteDir);
		//get the background image path
		String bgImagePath=this.bgImageBrowsePanel.getImagePath();
		location.setBgImagePath(PathResolver.getRelativePath(absoluteDir,bgImagePath));
		//get the overlay image path
		String overlayImagePath=this.overlayImageBrowsePanel.getImagePath();
		location.setBgImagePath(PathResolver.getRelativePath(absoluteDir,overlayImagePath));
		//get the background music path
		String bgMusicPath=this.bgMusicBrowsePanel.getFilePath();
		location.setBgMusicPath(PathResolver.getRelativePath(absoluteDir,bgMusicPath));
		//get the exits
		LocationExit[] exits=this.exitList.getAllExits();
		location.setExits(exits);
		//get the items
		LocationObjectData[] items=this.itemList.getAllObjects();
		location.setItems(items);
		//get the characters
		LocationObjectData[] characters=this.characterList.getAllObjects();
		location.setCharacters(characters);
		//now save
		XmlFormatter formatter=new XmlFormatter();
		try{
			formatter.saveTo(location,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);
	}

	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("Location File",filters,false);
		this.panelContent.add(this.browsePanel);
		//add background image panel
		this.bgImageBrowsePanel=new ImageBrowsePanel("Background Image");
		this.panelContent.add(this.bgImageBrowsePanel);
		//add overlay image panel
		this.overlayImageBrowsePanel=new ImageBrowsePanel("Overlay Image");
		this.panelContent.add(this.overlayImageBrowsePanel);
		//add background music panel
		filters=new FileFilter[1];
		filters[0]=new MidiFileFilter();
		this.bgMusicBrowsePanel=new FileBrowsePanel("Background Music",filters,true);
		this.panelContent.add(this.bgMusicBrowsePanel);
		//add lists
		JPanel listPanel=new JPanel();
		listPanel.setLayout(new BoxLayout(listPanel,BoxLayout.X_AXIS));
		this.exitList=new LocationExitListPanel();
		listPanel.add(this.exitList);
		this.itemList=new LocationObjectDataListPanel("Items");
		listPanel.add(this.itemList);
		this.characterList=new LocationObjectDataListPanel("Characters");
		listPanel.add(this.characterList);
		this.panelContent.add(listPanel);
		//pack
		this.pack();
	}
	
	public boolean openLocation(String filePath){
		boolean success=true;
		try{
			XmlFormatter formatter=new XmlFormatter();
			Location location=(Location)formatter.loadFrom(filePath);
			this.openLocation(location);
		} catch(Exception x){
			x.printStackTrace();
			JOptionPane.showMessageDialog(this,"Error opening location:"+x.getMessage(),"Error",JOptionPane.ERROR_MESSAGE);
			success=false;
		}
		if(success){
			this.browsePanel.setFilePath(filePath);
		}
		return(success);
	}

	public void openLocation(Location location){
		//set the background image
		this.bgImageBrowsePanel.setImage(location.getBgImagePathAbsolute());
		//set the overlay image
		this.overlayImageBrowsePanel.setImage(location.getOverlayImagePathAbsolute());
		//set the background music
		this.bgMusicBrowsePanel.setFilePath(location.getBgMusicPathAbsolute());
		//set the exits
		LocationExit[] exits=location.getExits();
		int length=exits.length;
		for(int index=0;index<length;index++){
			this.exitList.addExit(exits[0]);
		}
		//set the characters
		LocationObjectData[] characters=location.getCharacters();
		length=characters.length;
		for(int index=0;index<length;index++){
			this.characterList.addLocationData(characters[0]);
		}
		//set the items
		LocationObjectData[] items=location.getItems();
		length=items.length;
		for(int index=0;index<length;index++){
			this.itemList.addLocationData(items[0]);
		}
	}
	
	public static void main(String[] args){
		// TODO Auto-generated method stub
		(new LocationEditorFrame()).setVisible(true);
	}

}