//
// Core Program for TETDM 
// Select.java Version 0.31
// Copyright(C) 2011 2012 TETDM CORE MEMBER ALL RIGHTS RESERVED.
// This program is provided under the license.
//



package source;

import source.TextData.*;


import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JToggleButton;
import javax.swing.border.SoftBevelBorder;
import javax.swing.border.BevelBorder;
import javax.swing.border.TitledBorder;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;

import java.awt.*;
import javax.swing.*;

public class Select extends JPanel implements ActionListener
{
	TextData text;
	private int panelID;
	int defaultPanelSizeX, defaultPanelSizeY;
	
	boolean setModule = false;
	
	ModuleData moduleData;

	//Modules
	Class<?> module[];	
	MiningModule mining[];

	String module_names[];
	int miningIDs[];
	String miningModuleNamesInJapanese[];
	int module_num = 0;
	int selected_module = 0;
	boolean make_panel[];
	
	Class<?> visu_module[];
	String visu_module_names[];
	int visualizationIDs[];
	String visualizationModuleNamesInJapanese[];
	int visu_module_num = 0;

	int pair_visu[];
	boolean pairingVisualizationModules[][];
	
    SoftBevelBorder bb = new SoftBevelBorder(SoftBevelBorder.RAISED, Color.orange, Color.orange);
	TitledBorder border;	
	
    DummyPanel dummyp;

	CardLayout layout;
	JPanel modulep = new JPanel(layout = new CardLayout());	// module panel
	JPanel buttonp = new JPanel(new GridLayout());	//lower button panel
	
	JPanel visu_buttonp = new JPanel(new GridLayout());	//lower button panel
	JPanel module_buttonp = new JPanel(new GridLayout());	//lower button panel
	
	JToggleButton bt[];
	ButtonGroup bt_group = new ButtonGroup();

	JToggleButton visu_bt[];
	ButtonGroup visu_bt_group = new ButtonGroup();
	
	String menuInJapanese[];
	
	Select(TextData tex, int id, ModuleData moduleD, String inJapanese[], int panelSizeX, int panelSizeY)
	{
		setLayout(new BorderLayout());

		setBackground(Color.white);

		text = tex;
		panelID = id;
		moduleData = moduleD;
		menuInJapanese = inJapanese;
		defaultPanelSizeX = panelSizeX;
		defaultPanelSizeY = panelSizeY;
		
		
		module = moduleData.msub.selectedMining;
		module_names = moduleData.msub.selectedMiningNames;
		miningIDs = moduleData.msub.selectedMiningIDs;
		miningModuleNamesInJapanese = moduleData.msub.selectedMiningModuleNamesInJapanese;
		visu_module = moduleData.msub.selectedVisualization;
		visu_module_names = moduleData.msub.selectedVisualizationNames;
		visualizationIDs = moduleData.msub.selectedVisualizationIDs;
		visualizationModuleNamesInJapanese = moduleData.msub.selectedVisualizationModuleNamesInJapanese;
		
		pair_visu = new int[moduleData.msub.selectedPairDefault.length];
		for(int i=0;i<pair_visu.length;i++)
			pair_visu[i] = moduleData.msub.selectedPairDefault[i];
		
		pairingVisualizationModules	= moduleData.msub.selectedPairingVisualizationModules;
		
		module_num = module.length;
		visu_module_num = visu_module.length;		
		
		
		mining = new MiningModule[module_num];
		make_panel = new boolean[module_num];
		for(int i=0;i<module_num;i++)
			make_panel[i] = true;
	
		initModules();

		for(int i=0;i<module_num;i++)
			mining[i].setPanelID(panelID);		

		dummyp = new DummyPanel();
		dummyp.setBackground(Color.white);
		modulep.add(dummyp,"MODULEX");
		layout.show(modulep, "MODULEX");

		border = new TitledBorder(bb);
		setTitle();

		init_visu_buttonpanel(false);
		init_buttonpanel(false);
		
		add(buttonp, BorderLayout.SOUTH);
		buttonp.setLayout(new GridLayout(2,1));
	}

	class DummyPanel extends JPanel
	{
	    Toolkit tk=getToolkit();
	    Image image1 = tk.getImage(text.absolutePath+"source"+File.separator+"guide.png");
	    int sizex, sizey, imageSizex, imageSizey;
		
		public void paintComponent(Graphics g)
		{
			Graphics2D g2 = (Graphics2D)g;
			sizex = getWidth();
			sizey = getHeight();
			imageSizex = image1.getWidth(this);
			imageSizey = image1.getHeight(this);

			if(sizex >= imageSizex)
			    g2.drawImage(image1,0,sizey-imageSizey,this);
			else
			    g2.drawImage(image1,0,sizey-imageSizey,sizex,imageSizey,this);			    
		}
	}
	
	void setPanelID(int id)
	{
		panelID = id;
		
		for(int i=0;i<module_num;i++)
		{
			mining[i].setPanelID(panelID);
			mining[i].setVisualizationPanelID();
		}
	}
	
	void setTitle()
	{
	    String title;
		if(text.control.sp1.inJapanese)
		    title = menuInJapanese[0]+" "+(panelID+1)+" : "+menuInJapanese[1]+" ("+new File(text.sourceFilename).getName()+")";
		else
		    title = "PANEL "+(panelID+1)+" : UNSELECTED ("+new File(text.sourceFilename).getName()+")";

		setBorder(BorderFactory.createTitledBorder(border, title, TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, new Font("Dialog", Font.BOLD, 12), Color.blue));
	}
	
	void setTitle2()
	{
	    String title;
	    
		if(text.control.sp1.inJapanese)	
//		    title = menuInJapanese[0]+" "+(panelID+1)+"("+(mining[selected_module].getPanelID()+1)+","+(mining[selected_module].visual[pair_visu[selected_module]].getPanelID()+1)+") : "+miningModuleNamesInJapanese[selected_module]+" + "+visualizationModuleNamesInJapanese[pair_visu[selected_module]] +" ("+new File(text.sourceFilename).getName()+")";
		    title = menuInJapanese[0]+" "+(panelID+1)+" : "+miningModuleNamesInJapanese[selected_module]+" + "+visualizationModuleNamesInJapanese[pair_visu[selected_module]] +" ("+new File(text.sourceFilename).getName()+")";			
		else
		    title = "PANEL "+(panelID+1)+" : "+module_names[selected_module]+" + "+visu_module_names[pair_visu[selected_module]]+" ("+new File(text.sourceFilename).getName()+")";

		setBorder(BorderFactory.createTitledBorder(bb, title, TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, new Font("Dialog", Font.BOLD, 12), Color.blue ));
	}

	void initModules()
	{		
		add(modulep);

		try {
			for(int i=0;i<module_num;i++)
				mining[i] = (MiningModule)module[i].newInstance();
		} catch (InstantiationException e) {
			throw new RuntimeException(e);
		} catch (IllegalAccessException e) {
			throw new RuntimeException(e);
		}

		for(int i=0;i<module_num;i++)
			modulep.add(mining[i],"MODULE"+i);
	}
	
	void init_buttonpanel(boolean setSelected)
	{
		//Button panel
		module_buttonp.setBackground(Color.PINK);

		bt = new JToggleButton[module_num];
		for(int i=0;i<module_num;i++)
		{
			//Buttons to select panels
			bt[i] = new JToggleButton();
			bt[i].setFont(new Font("Dialog", Font.BOLD, 10));
			bt[i].setBackground(Color.PINK);
			bt[i].addActionListener(this);
			bt_group.add(bt[i]);
			module_buttonp.add(bt[i]);
		}
		
		setButtonLabel();
		
		bt[selected_module].setSelected(setSelected);

		buttonp.add(module_buttonp);
	}
	
	void init_visu_buttonpanel(boolean setSelected)
	{
		//Button panel
		visu_buttonp.setBackground(Color.CYAN);
		
		visu_bt = new JToggleButton[visu_module_num];		
		for(int i=0;i<visu_module_num;i++)
		{
			//Buttons to select panels
			visu_bt[i] = new JToggleButton(visu_module_names[i]);
			visu_bt[i].setFont(new Font("Dialog", Font.BOLD, 10));
			visu_bt[i].setBackground(Color.CYAN);
			visu_bt[i].addActionListener(this);
			visu_bt_group.add(visu_bt[i]);
			visu_buttonp.add(visu_bt[i]);
		}
		setVisuButtonLabel();		
		
		visu_bt[pair_visu[selected_module]].setSelected(setSelected);
		
		for(int i=0;i<visu_module_num;i++)
//		    visu_bt[i].setEnabled( pairingVisualizationModules[selected_module][i] );
		    visu_bt[i].setEnabled( false );			
		
		buttonp.add(visu_buttonp);
	}	
	
	void setButtonLabel()
	{
		for(int i=0;i<module_num;i++)
			if(text.control.sp1.inJapanese)
				bt[i].setText(miningModuleNamesInJapanese[i]);
			else
				bt[i].setText(module_names[i]);
	}
	
	void setVisuButtonLabel()
	{
		for(int i=0;i<visu_module_num;i++)
			if(text.control.sp1.inJapanese)
				visu_bt[i].setText(visualizationModuleNamesInJapanese[i]);
			else
				visu_bt[i].setText(visu_module_names[i]);
	}
	
	void reset_font_size()
	{
		if(mining[selected_module].visual != null) //UNSELECTED PANEL
			mining[selected_module].setFont();
	}	

	//
	void resetAll(TextData tex)
	{
		text = tex;
		
//		resetAll_for_stealth(tex);
		
		if(setModule == false)
		{
			for(int i=0;i<module_num;i++)
				mining[i].text = text;
			return;
		}
		
		for(int i=0;i<module_num;i++)
			if(make_panel[i] == false)
				mining[i].resetFirstTime();
		
		mining[selected_module].selected(text);		
	}	

/*	
	// FOCUS
	void resetMiningByTouchFocus()
	{
		mining[selected_module].resetFirstTimeByTouchFocus();
		mining[selected_module].selectedByTouchFocus();		
	}	
	
	// FOCUS
	void setDisplayByTouchFocus()
	{
		mining[selected_module].setDisplayByTouchFocus();
	}	

	// FOCUS
	void resetMiningByClickFocus()
	{
		mining[selected_module].resetFirstTimeByClickFocus();
		mining[selected_module].selectedByClickFocus();		
	}	
	
	// FOCUS
	void setDisplayByClickFocus()
	{
		mining[selected_module].setDisplayByClickFocus();
	}	
*/
	// FOCUS*
	void resetMiningByTouchFocus()
	{
		mining[selected_module].selectedByTouchFocus();		
	}	
	
	// FOCUS*
	void setDisplayByTouchFocus()
	{
		mining[selected_module].setDisplayByTouchFocus();
	}	
	
	// FOCUS*
	void resetMiningByClickFocus()
	{
		mining[selected_module].selectedByClickFocus();		
	}	
	
	// FOCUS*
	void setDisplayByClickFocus()
	{
		mining[selected_module].setDisplayByClickFocus();
	}	
	
	
	void change_panel(int k)
	{
		setModule = true;
		
		selected_module = k;
		if(make_panel[selected_module])
		{
			mining[selected_module].init_panel(text, visu_module, pair_visu[selected_module], text.absolutePath, defaultPanelSizeX, defaultPanelSizeY);			
			make_panel[selected_module] = false;
		}	
		mining[selected_module].selected(text);
		
		visu_bt[pair_visu[selected_module]].setSelected(true);
		
		for(int i=0;i<visu_module_num;i++)
		    visu_bt[i].setEnabled( pairingVisualizationModules[selected_module][i] );
		
		setTitle2();
		layout.show(modulep, "MODULE"+selected_module);			
	}
	
	void change_visu_panel(int i)
	{	
		pair_visu[selected_module] = i;
		mining[selected_module].setVisu(pair_visu[selected_module]);
		mining[selected_module].selected(text);

		setTitle2();
		layout.show(modulep, "MODULE"+selected_module);		
	}
	
	void changePanel(int mid, int vid)
	{
		int MID = selected_module;
		int VID = pair_visu[selected_module];
		
		for(int i=0;i<module_num;i++)
			if(miningIDs[i] == mid)
			{
				MID = i;
				selected_module = MID;
				VID = pair_visu[selected_module];
				
				for(int j=0;j<visu_module_num;j++)
					if(visualizationIDs[j] == vid)
					{
						VID = j;
						pair_visu[selected_module] = j;
					}
			}

		bt[MID].setSelected(true);
		visu_bt[VID].setSelected(true);		
		
		for(int i=0;i<visu_module_num;i++)
		    visu_bt[i].setEnabled( pairingVisualizationModules[selected_module][i] );		
		visu_bt[VID].setEnabled(true);	
		
		setModule = true;
		
		if(make_panel[selected_module])
		{
			mining[selected_module].init_panel(text, visu_module, pair_visu[selected_module], text.absolutePath, defaultPanelSizeX, defaultPanelSizeY);			
			make_panel[selected_module] = false;
		}	

		mining[selected_module].selected(text);
		
		setTitle2();
		layout.show(modulep, "MODULE"+selected_module);
	}	
	
	//Action listener
	public void actionPerformed(ActionEvent e)
	{
		for(int i=0;i<module_num;i++)
			if(e.getSource() == bt[i])
				change_panel(i);
		
		for(int i=0;i<visu_module_num;i++)
			if(e.getSource() == visu_bt[i])
				change_visu_panel(i);				
	}	
	
	
	///////////////FOR STEALTH PANEL ONLY////////////////
	int change_panel_moduleID(int mid)
	{
		int pair=0;		//Find VisualizationStyle
		for(int i=0;i<moduleData.visu_module_names.length;i++)
			if(moduleData.visuModuleIDs[i] == 0)
				pair = i;
		
		for(int i=0;i<moduleData.module_names.length;i++)
			if(moduleData.moduleIDs[i] == mid)
			{
				moduleData.mining[i].init_panel(text, moduleData.visu_module, pair, text.absolutePath, defaultPanelSizeX, defaultPanelSizeY);
				moduleData.mining[i].selected(text);
				return i;
			}
		return -1;
	}
	
	void executeModule(int optionNumber, int mp)
	{
		moduleData.mining[mp].miningOperations(optionNumber);
	}
	
	void resetAll_for_stealth(TextData tex)
	{
		text = tex;		
		
		for(int i=0;i<moduleData.module_names.length;i++)
		{
			moduleData.mining[i].text = tex;
			moduleData.mining[i].firstTime = true;
		}
	}
	///////////////FOR STEALTH PANEL ONLY////////////////
	
	///////FOR MODULE EXECUTION
	void executeModule(int option)
	{
		mining[selected_module].miningOperations(option);
	}	
	
	///////FOR MODULE DISPLAY
	void displayModule(int option)
	{
		mining[selected_module].displayOperations(option);
	}
}
