/*
 Tiamat - a role-playing game framework
 ItemPanel.java - panel for editing 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.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import com.huguesjohnson.PathResolver;
import com.huguesjohnson.tiamat.Item;
import com.huguesjohnson.tiamat.Target;
import com.huguesjohnson.ui.ImageBrowsePanel;

/**
 * ItemPanel - panel for editing an Item
 * @author Hugues Johnson
 */
public class ItemPanel extends JPanel{

	/* 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=-5437470356458481902L;

	//private ui components
	private JCheckBox checkBoxDisappearOnUse;
	private JCheckBox checkBoxEquipable;
	private JCheckBox checkBoxEquipped;
	private JLabel labelDescription;
	private JLabel labelName;
	private JPanel panelButtons;
	private JPanel panelCheckboxes;
	private JPanel panelDescription;
	private JPanel panelFullSizeImage;
	private JPanel panelName;
	private JPanel panelTargetList;
	private JPanel panelTargets;
	private JPanel panelThumbnailImage;
	private JTextField textFieldDescription;
	private JTextField textFieldName;
	private TargetListPanel targetList;
	private ImageBrowsePanel thumbnailImagePanel;
	private ImageBrowsePanel fullSizeImagePanel;	
	
	private String absoluteDir;

	public ItemPanel(){
		super();
		this.absoluteDir=null;
		this.initComponents();
		// add the target panel
		this.targetList=new TargetListPanel();
		this.panelTargets.add(this.targetList);
		// add the image panels
		this.thumbnailImagePanel=new ImageBrowsePanel("Thumbnail Image");
		this.panelThumbnailImage.add(this.thumbnailImagePanel);
		this.fullSizeImagePanel=new ImageBrowsePanel("Full Size Image");
		this.panelFullSizeImage.add(this.fullSizeImagePanel);
	}

	public ItemPanel(String absoluteDir){
		super();
		this.absoluteDir=absoluteDir;
		this.initComponents();
		// add the target panel
		this.targetList=new TargetListPanel();
		this.panelTargets.add(this.targetList);
		// add the image panels
		this.thumbnailImagePanel=new ImageBrowsePanel("Thumbnail Image");
		this.panelThumbnailImage.add(this.thumbnailImagePanel);
		this.fullSizeImagePanel=new ImageBrowsePanel("Full Size Image");
		this.panelFullSizeImage.add(this.fullSizeImagePanel);
	}

	public ItemPanel(Item item){
		super();
		this.absoluteDir=null;
		this.initComponents(); 
		this.targetList=new TargetListPanel();
		this.panelTargets.add(this.targetList);
		this.thumbnailImagePanel=new ImageBrowsePanel("Thumbnail Image");
		this.panelThumbnailImage.add(this.thumbnailImagePanel);
		this.fullSizeImagePanel=new ImageBrowsePanel("Full Size Image");
		this.panelFullSizeImage.add(this.fullSizeImagePanel);
		this.setItem(item);
	}
	
	public ItemPanel(Item item,String absoluteDir){
		super();
		this.absoluteDir=absoluteDir;
		this.initComponents(); 
		this.targetList=new TargetListPanel();
		this.panelTargets.add(this.targetList);
		this.thumbnailImagePanel=new ImageBrowsePanel("Thumbnail Image");
		this.panelThumbnailImage.add(this.thumbnailImagePanel);
		this.fullSizeImagePanel=new ImageBrowsePanel("Full Size Image");
		this.panelFullSizeImage.add(this.fullSizeImagePanel);
		// add the item properties to the dialog
		this.setItem(item);
	}

	private void initComponents(){
		this.panelName=new JPanel();
		this.labelName=new JLabel();
		this.textFieldName=new JTextField();
		this.panelDescription=new JPanel();
		this.labelDescription=new JLabel();
		this.textFieldDescription=new JTextField();
		this.panelCheckboxes=new JPanel();
		this.checkBoxEquipable=new JCheckBox();
		this.checkBoxEquipped=new JCheckBox();
		this.checkBoxDisappearOnUse=new JCheckBox();
		this.panelFullSizeImage=new JPanel();
		this.panelThumbnailImage=new JPanel();
		this.panelTargets=new JPanel();
		this.panelTargetList=new JPanel();
		this.panelButtons=new JPanel();

		this.setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
		this.panelName.setLayout(new BoxLayout(this.panelName,BoxLayout.X_AXIS));

		this.labelName.setHorizontalAlignment(SwingConstants.RIGHT);
		this.labelName.setText("Name: ");
		this.labelName.setMaximumSize(new java.awt.Dimension(85,15));
		this.labelName.setMinimumSize(new java.awt.Dimension(85,15));
		this.labelName.setPreferredSize(new java.awt.Dimension(85,15));
		this.panelName.add(this.labelName);

		this.textFieldName.setToolTipText("Name of the Item");
		this.textFieldName.setMaximumSize(new java.awt.Dimension(2147483647,21));
		this.textFieldName.setMinimumSize(new java.awt.Dimension(300,21));
		this.panelName.add(this.textFieldName);

		this.add(this.panelName);

		this.panelDescription.setLayout(new BoxLayout(this.panelDescription,BoxLayout.X_AXIS));

		this.labelDescription.setHorizontalAlignment(SwingConstants.RIGHT);
		this.labelDescription.setText("Description: ");
		this.labelDescription.setMaximumSize(new java.awt.Dimension(85,15));
		this.labelDescription.setMinimumSize(new java.awt.Dimension(85,15));
		this.labelDescription.setPreferredSize(new java.awt.Dimension(85,15));
		this.panelDescription.add(this.labelDescription);

		this.textFieldDescription.setToolTipText("Description of the Item");
		this.textFieldDescription.setMaximumSize(new java.awt.Dimension(2147483647,21));
		this.panelDescription.add(this.textFieldDescription);

		this.add(this.panelDescription);

		this.panelCheckboxes.setLayout(new BoxLayout(this.panelCheckboxes,BoxLayout.X_AXIS));

		this.checkBoxEquipable.setText("Equipable");
		this.checkBoxEquipable.setToolTipText("Check if this Item can be equipped");
		this.checkBoxEquipable.addActionListener(new java.awt.event.ActionListener(){
			public void actionPerformed(java.awt.event.ActionEvent evt){
				checkBoxEquipableActionPerformed(evt);
			}
		});

		this.panelCheckboxes.add(this.checkBoxEquipable);

		this.checkBoxEquipped.setText("Equipped");
		this.checkBoxEquipped.setToolTipText("Check if this Item is equipped");
		this.checkBoxEquipped.setEnabled(false);
		this.panelCheckboxes.add(this.checkBoxEquipped);

		this.checkBoxDisappearOnUse.setText("Disappear on use");
		this.checkBoxDisappearOnUse.setToolTipText("Check if this Item should disappear after being used");
		this.panelCheckboxes.add(this.checkBoxDisappearOnUse);

		this.add(this.panelCheckboxes);

		this.panelFullSizeImage.setLayout(new BoxLayout(this.panelFullSizeImage,BoxLayout.X_AXIS));

		this.add(this.panelFullSizeImage);

		this.panelThumbnailImage.setLayout(new BoxLayout(this.panelThumbnailImage,BoxLayout.X_AXIS));

		this.add(this.panelThumbnailImage);

		this.panelTargets.setLayout(new BoxLayout(this.panelTargets,BoxLayout.Y_AXIS));

		this.panelTargetList.setLayout(new BoxLayout(this.panelTargetList,BoxLayout.X_AXIS));

		this.panelTargets.add(this.panelTargetList);

		this.add(this.panelTargets);

		this.panelButtons.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT));
		this.add(this.panelButtons);
	}

	public void checkBoxEquipableActionPerformed(java.awt.event.ActionEvent evt){
		if(this.checkBoxEquipable.isSelected()){
			if(!this.checkBoxEquipped.isEnabled()){
				this.checkBoxEquipped.setEnabled(true);
			}
		} else{
			this.checkBoxEquipped.setEnabled(false);
			this.checkBoxEquipped.setSelected(false);
		}
	}
	
	public void setItem(Item item){
		// add the item properties to the dialog
		this.textFieldName.setText(item.getName());
		this.textFieldDescription.setText(item.getDescription());
		if(item.getEquipable()){
			this.checkBoxEquipable.setSelected(true);
			this.checkBoxEquipped.setEnabled(true);
			this.checkBoxEquipped.setSelected(item.getEquipped());
		}
		this.checkBoxDisappearOnUse.setSelected(item.getDisappearOnUse());
		String fullImagePath=item.getFullImagePathAbsolute();
		this.fullSizeImagePanel.setImage(fullImagePath);
		String thumbnailImagePath=item.getThumbnailImagePathAbsolute();
		this.thumbnailImagePanel.setImage(thumbnailImagePath);
		int targetCount=item.getTargetCount();
		for(int index=0;index<targetCount;index++){
			this.targetList.addTarget(item.getTarget(index));
		}
	}
	
	/** get the Item represented by this dialog */
	public Item getItem(){
		String name=this.textFieldName.getText();
		String description=this.textFieldDescription.getText();
		if((this.absoluteDir==null)||(this.absoluteDir.length()<1)){
			this.absoluteDir=this.fullSizeImagePanel.getDirectory();
		}
		boolean equipable=this.checkBoxEquipable.isSelected();
		boolean equipped=this.checkBoxEquipped.isSelected();
		boolean disappearOnUse=this.checkBoxDisappearOnUse.isSelected();
		String fullImagePath=PathResolver.getRelativePath(this.absoluteDir,this.fullSizeImagePanel.getImagePath());
		String thumbnailImagePath=PathResolver.getRelativePath(this.absoluteDir,this.thumbnailImagePanel.getImagePath());
		Target[] targets=this.targetList.getAllTargets();
		return(new Item(name,description,this.absoluteDir,fullImagePath,thumbnailImagePath,targets,equipable,disappearOnUse));
	}

}