package j3deditor.bin.editor.tools;

import j3deditor.bin.components.CenterLayout;

import j3deditor.bin.components.TransparentPanel;

import j3deditor.bin.hierarchy.util.HierarchyEvent;
import j3deditor.bin.hierarchy.util.HierarchyEventListener;

import j3deditor.bin.hierarchy.HierarchyNode;

import j3deditor.bin.hierarchy.J3DeLight;
import j3deditor.bin.hierarchy.J3DeMaterial;
import j3deditor.bin.hierarchy.J3DeObject;
import j3deditor.bin.hierarchy.J3DePrimitive;
import j3deditor.bin.hierarchy.J3DeScene;

import j3deditor.bin.hierarchy.J3DeShape3D;
import j3deditor.bin.hierarchy.J3DeTexture;
import j3deditor.bin.hierarchy.J3DeTextureAttributes;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JLabel;
import javax.swing.JTextField;


/**
 * User interface for manipulating  the attributes of
 * active <code>HierarchyNode</code> object.
 *
 * @author Risto Seene
 */
public class NodePanel extends TransparentPanel implements ActionListener{


	/** <code>HierarchyNode</code> this panel manipulates */
	private HierarchyNode node;

	/** listener that listens hierarchy manipulation events */
	private HierarchyEventListener listener;


	/** shows the name of the current node */
	private JTextField name;

	/** reference to an object attached to this panel that can be updated */
	private UpdateblePanel panel = null;


	/**
	 * Creates an instance of <code>NodePanel</code>
	 * that manipulates the given <code>HierarchyNode</code> object.
	 *
	 * @param node
	 *	hierarchy node this panel manipulates
	 * @param listener
	 *	listener that listens hierarchy manipulation events
	 *	made by this panel
	 */
	public NodePanel(HierarchyNode node, HierarchyEventListener listener){
		super(2, 8, new Color(238, 238, 238), 0.6f);
		this.node = node;
		this.listener = listener;

		setLayout(new CenterLayout(4));
		initContent();
	}


	/**
	 * Initializes the content of this panel
	 */
	private void initContent(){
		Container panel = new Container();
		panel.setLayout(new BorderLayout());
		add(panel);
		Container main = new Container();
		main.setLayout(new GridLayout(1, 2, 2, 2));
		panel.add(main, BorderLayout.NORTH);

		Container base = new Container();
		base.setLayout(new GridLayout(1, 1, 2, 2));

		if(node instanceof J3DeScene){
			name = new JTextField(node.toString());
			base.add(new JLabel());
			main.add(name);
			main.add(new JLabel());
		}
		else if(node instanceof J3DeObject){
			base = initObjPanel(main, base);
		}
		else if(node instanceof J3DeMaterial){
			main.add(new JLabel(node.toString()), 0);
			main.add(new JLabel());
			base.add(new MaterialPanel((J3DeMaterial)node));
		}
		else if(node instanceof J3DeTexture){
		          main.add(new JLabel(node.toString()), 0);
		          main.add(new JLabel());
		          base.add(new TexturePanel((J3DeTexture)node, this.listener));
		}
		else {
			main.add(new JLabel(node.toString()), 0);
			main.add(new JLabel());
			base.add(new JLabel());
		}

		panel.add(base, BorderLayout.CENTER);
		if(name != null){
			name.addActionListener(this);
		}
	}


	/**
	 * Initializes <code>ObjPanel</code> object if one is going
	 * to be attached to this panel
	 */
	private Container initObjPanel(Container main, Container base){
		if(node instanceof J3DeTextureAttributes){
			main.add(new JLabel(node.toString()), 0);
			main.add(new JLabel());
			panel = new TextureAttributesPanel((J3DeTextureAttributes)node, listener);
			base.add((TextureAttributesPanel)panel);
		}
		else {
			base = new Container();
			base.setLayout(new GridLayout(1, 2, 2, 2));
			name = new JTextField(node.toString());
			main.add(name);

			panel = new ObjPanel((J3DeObject)node, listener);
			base.add((ObjPanel)panel);
			if(node instanceof J3DePrimitive){
				main.add(new JLabel(((J3DePrimitive)node).getAppearance().toString(), 2));
				base.add(new AppearancePanel(((J3DePrimitive)node).getAppearance(), listener));
			}
			else if(node instanceof J3DeShape3D){
				main.add(new JLabel(((J3DeShape3D)node).getAppearance().toString(), 2));
				base.add(new AppearancePanel(((J3DeShape3D)node).getAppearance(), listener));
			}
			else if(node instanceof J3DeLight){
				main.add(new JLabel());
				base.add(new LightPanel((J3DeLight)node));
			}
		}

		return base;
	}


	/**
	 * Listens action events sent by controls attached to this panel.
	 *
	 * @param e
	 *	event sent by a control button
	 */
	public void actionPerformed(ActionEvent e){
		if(name != null && e.getSource() == name && !name.getText().equals("")){
			node.setName(name.getText());
			node.updateName();
		}
		if(listener != null){
			listener.hierarchyEventOccured(new HierarchyEvent(this, HierarchyEvent.OBJECT_SELECTION));
		}
	}


	/**
	 * Updates an object referenced by global variable <code>panel</code>.
	 * if not <code>null</code>
	 */
	public void updateValues(){
		if(panel != null){
			panel.updateValues();
		}
	}

}
