package j3deditor.bin.editor.tools;

import j3deditor.bin.hierarchy.util.HierarchyEventListener;

import j3deditor.bin.hierarchy.J3DeTextureAttributes;

import j3deditor.bin.util.Utilities;

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

import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JSlider;
import javax.swing.JTabbedPane;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;


/**
 * User interface for manipulating  the attributes of
 * active <code>J3DeTextureAttributes</code> node.
 *
 * @author Risto Seene
 */
public class TextureAttributesPanel extends JPanel implements ActionListener, ChangeListener, UpdateblePanel{


	/** <code>J3DeTextureAttributes</code> this panel manipulates */
	private J3DeTextureAttributes ta;

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

	/** @see J3DeTextureAttributes#setCombineScaleFactor(int scale) */
	private JSlider csfact;

	/** @see J3DeTextureAttributes#setCombineScaleFactorMode(int mode) */
	private JComboBox csmode;

	/** @see J3DeTextureAttributes#setTextureMode(int mode) */
	private JComboBox tmode;

	/** @see J3DeTextureAttributes#setCombineMode(int mode) */
	private JComboBox cmode;

	/** @see J3DeTextureAttributes#setCombineModeTarget(int target) */
	private JComboBox cmtarget;

	/** @see J3DeTextureAttributes#setCombineColorSource(int src) */
	private JComboBox ccsource;

	/** @see J3DeTextureAttributes#setCombineColorSourceTarget(int target) */
	private JComboBox ccstarget;

	/** @see J3DeTextureAttributes#setCombineColorFunction(int function) */
	private JComboBox ccfunc;

	/** @see J3DeTextureAttributes#setCombineColorFunctionTarget(int target) */
	private JComboBox ccftarget;


	/** <code>ObjPanel</code> for manipulating texture attributes transform component */
	ObjPanel tools2;

	//private JComboBox pcorrect;


	/**
	 * Creates an instance of <code>TextureAttributesPanel</code>
	 * that manipulates the given <code>J3DeTextureAttributes</code> node.
	 *
	 * @param ta
	 *	hierarchy node this panel manipulates
	 * @param listener
	 *	listener that listens hierarchy manipulation events
	 *	made by this panel
	 */
	public TextureAttributesPanel(J3DeTextureAttributes ta, HierarchyEventListener listener){
		this.ta = ta;
		this.listener = listener;

		setLayout(new BorderLayout());
		setOpaque(false);
		initContent();
	}


	/**
	 * Initializes the content of this panel
	 */
	private void initContent(){
		add(new JSeparator(), BorderLayout.NORTH);


		JPanel tools1 = new JPanel();
		tools1.setLayout(new GridLayout(4, 4, 5, 5));

		tools1.add(new JLabel(Utilities.translator.translate(this.getClass(), "csFactor"), 4));
		csfact = new JSlider(1, 3, ta.getCombineScaleFactor());
		csfact.addChangeListener(this);
		csfact.setOpaque(false);
		tools1.add(csfact);

		csmode = new JComboBox(ta.getCombineScaleFactorModeItems());
		csmode.setToolTipText(Utilities.translator.translate(this.getClass(), "csfMode"));
		csmode.setSelectedIndex(ta.getCombineScaleFactorMode());
		csmode.addActionListener(this);
		tools1.add(csmode);

		for(int i = 0; i < 6; i++){
			tools1.add(new JLabel());
		}

		cmtarget = new JComboBox(ta.getCombineModeTargetItems());
		cmtarget.setToolTipText(Utilities.translator.translate(this.getClass(), "combModeTarget"));
		cmtarget.setSelectedIndex(ta.getCombineModeTarget());
		cmtarget.addActionListener(this);
		tools1.add(cmtarget);

		ccstarget = new JComboBox(ta.getCombineColorSourceTargetItems());
		ccstarget.setToolTipText(Utilities.translator.translate(this.getClass(), "combColSrcTarget"));
		ccstarget.setSelectedIndex(ta.getCombineColorSourceTarget());
		ccstarget.addActionListener(this);
		tools1.add(ccstarget);

		ccftarget = new JComboBox(ta.getCombineColorFunctionTargetItems());
		ccftarget.setToolTipText(Utilities.translator.translate(this.getClass(), "combColFuncTarget"));
		ccftarget.setSelectedIndex(ta.getCombineColorFunctionTarget());
		ccftarget.addActionListener(this);
		tools1.add(ccftarget);

		tmode = new JComboBox(ta.getTextureModeItems());
		tmode.setToolTipText(Utilities.translator.translate(this.getClass(), "texMode"));
		tmode.setSelectedIndex(ta.getTextureMode());
		tmode.addActionListener(this);
		tools1.add(tmode);

		cmode = new JComboBox(ta.getCombineModeItems());
		cmode.setToolTipText(Utilities.translator.translate(this.getClass(), "combMode"));
		cmode.setSelectedIndex(ta.getCombineMode());
		cmode.addActionListener(this);
		tools1.add(cmode);

		ccsource = new JComboBox(ta.getCombineColorSourceItems());
		ccsource.setToolTipText(Utilities.translator.translate(this.getClass(), "combColSrc"));
		ccsource.setSelectedIndex(ta.getCombineColorSource());
		ccsource.addActionListener(this);
		tools1.add(ccsource);

		ccfunc = new JComboBox(ta.getCombineColorFunctionItems());
		ccfunc.setToolTipText(Utilities.translator.translate(this.getClass(), "combColFunc"));
		ccfunc.setSelectedIndex(ta.getCombineColorFunction());
		ccfunc.addActionListener(this);
		tools1.add(ccfunc);

		enableCombine();


		tools2 = new ObjPanel(ta, listener);
		tools2.setOpaque(true);

		/*pcorrect = new JComboBox(ta.getPerspectiveCorrectionItems());
		pcorrect.setToolTipText(Utilities.translator.translate(this.getClass(), "persCor"));
		pcorrect.setSelectedIndex(ta.getPerspectiveCorrection());
		pcorrect.addActionListener(this);
		tools2.add(pcorrect);*/


		JTabbedPane tabs = new JTabbedPane(JTabbedPane.LEFT);
		add(tabs, BorderLayout.CENTER);
		tabs.addTab("1", tools1);
		tabs.addTab("2", tools2);
	}


	/**
	 * Enables/disables all controls depending on texture mode
	 */
	private void enableCombine(){
		boolean state = false;
		if(tmode.getSelectedIndex() == 4){
			state = true;
		}

		csfact.setEnabled(state);
		csmode.setEnabled(state);
		cmode.setEnabled(state);
		ccsource.setEnabled(state);
		ccfunc.setEnabled(state);
		cmtarget.setEnabled(state);
		ccstarget.setEnabled(state);
		ccftarget.setEnabled(state);
	}


	/**
	 * Listens action events sent by control buttons attached to this panel.
	 *
	 * @param e
	 *	event sent by a control button
	 */
	public void actionPerformed(ActionEvent e){
		if(e.getSource() == csmode){
			ta.setCombineScaleFactorMode(csmode.getSelectedIndex());
			csfact.setValue(ta.getCombineScaleFactor());
		} else if(e.getSource() == tmode){
			ta.setTextureMode(tmode.getSelectedIndex());
			enableCombine();
		}

		else if(e.getSource() == cmtarget){
			ta.setCombineModeTarget(cmtarget.getSelectedIndex());
			cmode.setSelectedIndex(ta.getCombineMode());
		} else if(e.getSource() == cmode){
			ta.setCombineMode(cmode.getSelectedIndex());
		}

		else if(e.getSource() == ccstarget){
			ta.setCombineColorSourceTarget(ccstarget.getSelectedIndex());
			ccsource.setSelectedIndex(ta.getCombineColorSource());
		} else if(e.getSource() == ccsource){
			ta.setCombineColorSource(ccsource.getSelectedIndex());
		}

		else if(e.getSource() == ccftarget){
			ta.setCombineColorFunctionTarget(ccftarget.getSelectedIndex());
			ccfunc.setSelectedIndex(ta.getCombineColorFunction());
		} else if(e.getSource() == ccfunc){
			ta.setCombineColorFunction(ccfunc.getSelectedIndex());
		}

		/*else if(e.getSource() == pcorrect){
			ta.setPerspectiveCorrection(pcorrect.getSelectedIndex());
		}*/
	}


	/**
	 * Listens action events sent by sliders attached to this panel.
	 *
	 * @param e
	 *	event sent by a slider
	 */
	public void stateChanged(ChangeEvent e){
		if(e.getSource() == csfact){
			ta.setCombineScaleFactor(csfact.getValue());
		}
	}


	/**
	 * Updates all values in <code>ObjPanel</code>
	 * attached to this panel.
	 */
	public void updateValues(){
		try{
			tools2.updateValues();
		} catch(Throwable t){
		}
	}

}
