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


package source;

import source.TextData.*;

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

public abstract class MiningModule extends JPanel implements ActionListener
{
	private int moduleID = -1;
	private int panelID;
	
	public TextData text;
	
	public String myModulePath;
	
	boolean firstTime = true;
	
	public boolean focusTouchExecute = false;
	public boolean focusClickExecute = false;
	
	CardLayout layout;	
	JPanel visualPanel = new JPanel(layout = new CardLayout());
	public JPanel operationPanel = new JPanel(new GridLayout());
	JPanel combinationPanel = new JPanel(new GridLayout());
	
//	public boolean panelSet = false;
	public boolean panelSet = true;
	public JButton panelDeleteButton;
	public JButton panelRecordButton;	
	public JButton panelLoadButton;
    public JButton panelBackButton;
	public JButton panelSetButton;
	private int setPanelModuleID[];
	private int setPanelVisualizationID[];
	private int lastSetPanelModuleID[];
	private int lastSetPanelVisualizationID[];
	public int defaultSetPanelModuleID[];
	public int defaultSetPanelVisualizationID[];
	String absolutePath;

	Class<?> visuModule[];
//    	public VisualizationModule visualization;	
	private VisualizationModule visualization;
	VisualizationModule visual[];
	int visuModuleNumber;
	
	public int pairingVisualizationID[];
	public int pairingVisuID[];		//For SettingPanel
	
	public MiningModule(){}
	
	public final void setModuleID(int moduleID)
	{
		this.moduleID = moduleID;
	}
	
	public final int getModuleID()
	{
		return moduleID;
	}	
	
	public final void setPanelID(int panelID)
	{
		this.panelID = panelID;
	}
	
	public final int getPanelID()
	{
		return panelID;
	}	
	
	public final void setVisualizationPanelID()
	{
		if(visual != null)
		for(int i=0;i<visual.length;i++)
//			if(visual[i] != null)
				visual[i].setPanelID(panelID);		
	}

	public final void init_panel(TextData tex, Class<?> vis[], int selectedVisu, String absolute, int defaultPanelSizeX, int defaultPanelSizeY)
	{
		text = tex;
		
		setLayout(new BorderLayout());
		setBackground(Color.white);

		this.visuModule = vis;
		visuModuleNumber = visuModule.length;		
		
		initializeVisuModules();
		add(visualPanel);
		
		setVisualizationPanelID();
		
		for(int i=0;i<visuModuleNumber;i++)
		{
			visual[i].sizeX = defaultPanelSizeX;
			visual[i].sizeY = defaultPanelSizeY;			
		}
			
		absolutePath = absolute;		
		myModulePath = absolutePath + this.getClass().getPackage().getName().replaceAll("\\.","/") + File.separator;

		
		if(panelSet)	// SET PANEL COMBINATION
		{
			if(defaultSetPanelModuleID == null)		// NOT GIVEN IN THE CONSTRUCTOR
				defaultSetPanel();		//SET DEFAULT PANEL COMBINATION
			
			String filename = absolutePath + "source/JapaneseMining.txt";					
			File jpname = new File(filename);
			String buttonNamesInJapanese[];
			buttonNamesInJapanese = FILEIO.TextFileAllReadCodeArray(jpname);
			
			
			if(isMenuInJapanese())
			{
				panelDeleteButton = new JButton(buttonNamesInJapanese[0],new ImageIcon(absolutePath+"/source/delete.png"));				
				panelRecordButton = new JButton(buttonNamesInJapanese[1],new ImageIcon(absolutePath+"/source/record.png"));
				panelLoadButton = new JButton(buttonNamesInJapanese[2],new ImageIcon(absolutePath+"/source/load.png"));
				panelBackButton = new JButton(buttonNamesInJapanese[3],new ImageIcon(absolutePath+"/source/back.png"));
				panelSetButton = new JButton(buttonNamesInJapanese[4],new ImageIcon(absolutePath+"/source/set.png"));
			}
			else
			{
				panelDeleteButton = new JButton("DELETE",new ImageIcon(absolutePath+"/source/delete.png"));				
				panelRecordButton = new JButton("REC",new ImageIcon(absolutePath+"/source/record.png"));
				panelLoadButton = new JButton("MYSET",new ImageIcon(absolutePath+"/source/load.png"));
				panelBackButton = new JButton("BACK",new ImageIcon(absolutePath+"/source/back.png"));
				panelSetButton = new JButton("SET",new ImageIcon(absolutePath+"/source/set.png"));				
			}
			panelDeleteButton.addActionListener(this);			
			panelRecordButton.addActionListener(this);
			panelLoadButton.addActionListener(this);
			panelBackButton.addActionListener(this);
			panelSetButton.addActionListener(this);
			
			combinationPanel.add(panelDeleteButton);			
			//			combinationPanel.add(panelRecordButton);			
			//			combinationPanel.add(panelLoadButton);
			combinationPanel.add(panelBackButton);
			combinationPanel.add(panelSetButton);
			
			combinationPanel.setBackground(Color.green);
			operationPanel.add(combinationPanel);
			
			if(!text.control.moduleData.miningPanelSet[text.control.moduleData.findModule(moduleID)].panelRecorded)
				panelLoadButton.setEnabled(false);
			if(!text.control.moduleData.miningPanelSet[text.control.moduleData.findModule(moduleID)].panelBack)
				panelBackButton.setEnabled(false);			
		}
		
		initializePanel();					// Step.1
		operationPanel.setBackground(Color.RED);		
		add(operationPanel, BorderLayout.SOUTH);
		
		setVisu(selectedVisu);
		
		System.out.println("Mining: "+this.getClass().getName());	
	}

	public void insideOfActionPerformed(ActionEvent e)
	{
		if(e.getSource() == panelDeleteButton)
			panelDelete();		
		
		if(e.getSource() == panelRecordButton)
			panelRecord();
	
		if(e.getSource() == panelLoadButton)
			panelLoad();

		if(e.getSource() == panelBackButton)
			panelBack();
	
		if(e.getSource() == panelSetButton)
			panelSet();			
	}
	
	public void insideOfActionPerformedShort(ActionEvent e)
	{
		if(e.getSource() == panelDeleteButton)
			panelDelete();		
		
		if(e.getSource() == panelRecordButton)
			panelRecord();
		
		if(e.getSource() == panelLoadButton)
			panelLoad();

		if(e.getSource() == panelBackButton)
			panelBack();		
	}	
	
	//Action listener
	public void actionPerformed(ActionEvent e)
	{
		insideOfActionPerformed(e);	
	}
	
	public final void initializeVisuModules()
	{
		visual = new VisualizationModule[visuModuleNumber];
		try {
			for(int i=0;i<visuModuleNumber;i++)
				visual[i] = (VisualizationModule)visuModule[i].newInstance();
		} catch (InstantiationException e) {
			throw new RuntimeException(e);
		} catch (IllegalAccessException e) {
			throw new RuntimeException(e);
		}
		
		for(int i=0;i<visuModuleNumber;i++)
			visualPanel.add(visual[i],"VISUAL"+i);	
	}
	
	public final void selected(TextData tex)
	{
		text = tex;
		if(firstTime)
		{
			initializeData();				// Step.2	Set moduleID
			firstTime = false;
		}
								
		if(visualization.firstTime)
		{
//			visualization.setPanelID(panelID);/**/
			visualization.selected(text);		// (to Step.3,4)
			miningOperations(0);	// Step.5
			setFont();							// (to Step.6)
			visualization.firstTime = false;
		}	
	}
	
	//FOCUS
	public final void focusSelected(TextData tex, int operationID)
	{
		text = tex;
		initializeData();				// Step.2	Set moduleID
		visualization.focusSelected(text);		// (to Step.4)
		miningOperations(operationID);	// Step.5 case 4501, 4502
		visualization.setFont();
		displayOperations(0);				// Step.6
	}	
	
	public final void createInitialData(){}// for check only 
	public final void setInitialData(){}// for check only
	
	public final void setVisu(int selectedVisu)
	{
		visualization = visual[selectedVisu];
		layout.show(visualPanel, "VISUAL"+selectedVisu);		
	}
	
	public void initializePanel(){};		// #Step.1
	public void initializeData(){};				// #Step.2

	public abstract void miningOperations(int n);			// #Step.5
	
	//display for visualization
	public final void setFont()
	{
		visualization.setFont();
		displayOperations(0);				// Step.6
	}
	
	public final void displayOperations(int optionNumber)
	{
		visualization.displayOperations(optionNumber);
	}	
	
	public final void resetFirstTime()
	{
			firstTime = true;
			visualization.firstTime = true;
	}

	public String fileRead()
	{	
		return FILEIO.TextFileAllReadCode(new File(myModulePath+"Japanese.txt"));
	}	
	
	public String fileRead(String filename)
	{	
		return FILEIO.TextFileAllReadCode(new File(filename));
	}
	
	public String[] fileReadArray()
	{	
		return FILEIO.TextFileAllReadCodeArray(new File(myModulePath+"Japanese.txt"));
	}
	
	public String[] fileReadArray(String filename)
	{	
		return FILEIO.TextFileAllReadCodeArray(new File(filename));
	}
	
	public void fileWrite(String filename, String writeText)
	{	
		FILEIO.TextFileWrite2(new File(filename), writeText);
	}
	
	public void fileWriteArray(String filename, String writeText[], int lineNumber)
	{	
		FILEIO.TextFileWrite(new File(filename), writeText, lineNumber);
	}	
	
/*	
	//FOCUS
	public final void resetFirstTimeByTouchFocus()
	{
		if(focusTouchExecute)
		{
			firstTime = true;
			visualization.firstTime = true;
		}
	}
	//FOCUS	
	public final void selectedByTouchFocus()
	{
		if(focusTouchExecute)
			selected(text);
	}
	//FOCUS
	public final void resetFirstTimeByClickFocus()
	{
		if(focusClickExecute)
		{
			firstTime = true;
			visualization.firstTime = true;
		}
	}
	//FOCUS	
	public final void selectedByClickFocus()
	{
		if(focusClickExecute)
			selected(text);
	}	
	
	//FOCUS
	public final void setDisplayByTouchFocus()
	{
		visualization.setDisplayByTouchFocus();
	}
	//FOCUS
	public final void setDisplayByClickFocus()
	{
		visualization.setDisplayByClickFocus();
	}	
*/

	//FOCUS*
	public final void selectedByTouchFocus()
	{
		if(focusTouchExecute)
			focusSelected(text, 4501);
	}
	//FOCUS*
	public final void selectedByClickFocus()
	{
		if(focusClickExecute)
			focusSelected(text, 4502);
	}	
	
	//FOCUS*
	public final void setDisplayByTouchFocus()
	{
		visualization.setDisplayByTouchFocus();
	}
	//FOCUS*
	public final void setDisplayByClickFocus()
	{
		visualization.setDisplayByClickFocus();
	}
	
	
	//SET DATA
	public final void setData(int optionNumber, int data)
	{
		text.connection.setData(moduleID, optionNumber, data);
		visualization.setData(optionNumber, data);
	}
	public final void setData(int optionNumber, double data)
	{
		text.connection.setData(moduleID, optionNumber, data);		
		visualization.setData(optionNumber, data);		
	}
	public final void setData(int optionNumber, String data)
	{
		text.connection.setData(moduleID, optionNumber, data);		
		visualization.setData(optionNumber, data);	
	}
	public final void setData(int optionNumber, int data[])
	{
		text.connection.setData(moduleID, optionNumber, data);		
		visualization.setData(optionNumber, data);	
	}
	public final void setData(int optionNumber, double data[])
	{
		text.connection.setData(moduleID, optionNumber, data);		
		visualization.setData(optionNumber, data);	
	}
	public final void setData(int optionNumber, String data[])
	{
		text.connection.setData(moduleID, optionNumber, data);		
		visualization.setData(optionNumber, data);	
	}
	
	//GET DATA
	public final int getDataInteger(int getModuleID, int dataID)
	{
		return text.connection.getDataInteger(getModuleID, dataID);
	}
	
	public final double getDataDouble(int getModuleID, int dataID)
	{
		return text.connection.getDataDouble(getModuleID, dataID);
	}
	
	public final String getDataString(int getModuleID, int dataID)
	{
		return text.connection.getDataString(getModuleID, dataID);
	}	
	
	public final int[] getDataIntegerArray(int getModuleID, int dataID)
	{
		return text.connection.getDataIntegerArray(getModuleID, dataID);
	}
	
	public final double[] getDataDoubleArray(int getModuleID, int dataID)
	{
		return text.connection.getDataDoubleArray(getModuleID, dataID);
	}
	
	public final String[] getDataStringArray(int getModuleID, int dataID)
	{
		return text.connection.getDataStringArray(getModuleID, dataID);
	}
	
	//GET NEW DATA
	public final int getDataIntegerNew(int getModuleID, int dataID)
	{
		text.connection.deleteData(getModuleID, dataID);
		return text.connection.getDataInteger(getModuleID, dataID);
	}
	
	public final double getDataDoubleNew(int getModuleID, int dataID)
	{
		text.connection.deleteData(getModuleID, dataID);		
		return text.connection.getDataDouble(getModuleID, dataID);
	}
	
	public final String getDataStringNew(int getModuleID, int dataID)
	{
		text.connection.deleteData(getModuleID, dataID);		
		return text.connection.getDataString(getModuleID, dataID);
	}	
	
	public final int[] getDataIntegerArrayNew(int getModuleID, int dataID)
	{
		text.connection.deleteData(getModuleID, dataID);		
		return text.connection.getDataIntegerArray(getModuleID, dataID);
	}
	
	public final double[] getDataDoubleArrayNew(int getModuleID, int dataID)
	{
		text.connection.deleteData(getModuleID, dataID);		
		return text.connection.getDataDoubleArray(getModuleID, dataID);
	}
	
	public final String[] getDataStringArrayNew(int getModuleID, int dataID)
	{
		text.connection.deleteData(getModuleID, dataID);		
		return text.connection.getDataStringArray(getModuleID, dataID);
	}	
	
	//EXECUTION
	public final void executeOtherModuleFirst(int executeModuleID, int optionNumber)
	{
		text.connection.executeModuleWithOptionFirst(panelID, executeModuleID, optionNumber);
	}	
	
	public final void executeOtherModule(int executeModuleID, int optionNumber)
	{
		text.connection.executeModuleWithOption(panelID, executeModuleID, optionNumber);
	}

	//DISPLAY
	public final void displayOtherModuleFirst(int displayModuleID, int optionNumber)
	{
		text.connection.displayModuleWithOptionFirst(panelID, displayModuleID, optionNumber);
	}	
	
	public final void displayOtherModule(int displayModuleID, int optionNumber)
	{
		text.connection.displayModuleWithOption(panelID, displayModuleID, optionNumber);
	}
	
	public final void displayModuleFirst(int displayModuleID, int optionNumber)
	{
		text.connection.displayModuleWithOptionFirstFromXXX(displayModuleID, optionNumber);
	}	
	
	public final void displayModule(int displayModuleID, int optionNumber)
	{
		text.connection.displayModuleWithOptionFromXXX(displayModuleID, optionNumber);
	}	

	//PANEL SET
	final void defaultSetPanel()
	{
		defaultSetPanelModuleID= new int[pairingVisualizationID.length];
		defaultSetPanelVisualizationID = new int[pairingVisualizationID.length];			
		for(int i=0;i<pairingVisualizationID.length;i++)
		{
			defaultSetPanelModuleID[i] = moduleID;
			defaultSetPanelVisualizationID[i] = pairingVisualizationID[i];
		}
	}

    final void recPanelConstitution(int MID[], int VID[])
    {
	//MID = new int[text.control.panelNumber];
	//	VID = new int[text.control.panelNumber];
	for(int i=0;i<text.control.panelNumber;i++)
	    {
		MID[i] = text.control.moduleData.msub.selectedMiningIDs[text.control.panel[i].selected_module];
		VID[i] = text.control.moduleData.msub.selectedVisualizationIDs[text.control.panel[i].pair_visu[text.control.panel[i].selected_module]];
	    }
    }
	
	final void panelDelete()
	{
		text.control.delete(panelID);
	}

	final void panelRecord()
	{
	    	    	setPanelModuleID = new int[text.control.panelNumber];
	    setPanelVisualizationID = new int[text.control.panelNumber];
	    /*		for(int i=0;i<text.control.panelNumber;i++)
		{
			setPanelModuleID[i] = text.control.moduleData.msub.selectedMiningIDs[text.control.panel[i].selected_module];
			setPanelVisualizationID[i] = text.control.moduleData.msub.selectedVisualizationIDs[text.control.panel[i].pair_visu[text.control.panel[i].selected_module]];
		}
	    */
	    recPanelConstitution(setPanelModuleID, setPanelVisualizationID);

		if(compare(setPanelModuleID, defaultSetPanelModuleID) && compare(setPanelVisualizationID, defaultSetPanelVisualizationID))
		    {
			text.control.moduleData.miningPanelSet[text.control.moduleData.findModule(moduleID)].panelRecorded = false;
			panelLoadButton.setEnabled(false);
			return;
		    }

		text.control.moduleData.miningPanelSet[text.control.moduleData.findModule(moduleID)].setPanelData(setPanelModuleID, setPanelVisualizationID);
		panelLoadButton.setEnabled(true);
	}

		boolean compare(int a[], int b[])
		{
		    if(a.length != b.length)
			return false;
		    for(int i=0;i<a.length;i++)
			if(a[i] != b[i])
			    return false;
		    return true;
		}
	
	final void panelLoad()
	{
	    lastSetPanelModuleID = new int[text.control.panelNumber];
	    lastSetPanelVisualizationID = new int[text.control.panelNumber];
	    recPanelConstitution(lastSetPanelModuleID, lastSetPanelVisualizationID);

	    text.control.moduleData.miningPanelSet[text.control.moduleData.findModule(moduleID)].setBackPanelData(lastSetPanelModuleID, lastSetPanelVisualizationID);
	    panelBackButton.setEnabled(true);

	    //		if(text.control.moduleData.miningPanelSet[text.control.moduleData.findModule(moduleID)].panelRecorded)
			text.control.panelSetByMiningModule(moduleID);
			//		else
			//	text.control.panelSet(defaultSetPanelModuleID, defaultSetPanelVisualizationID);
	}
	
	public final void panelSet()
	{
	    lastSetPanelModuleID = new int[text.control.panelNumber];
	    lastSetPanelVisualizationID = new int[text.control.panelNumber];
	    recPanelConstitution(lastSetPanelModuleID, lastSetPanelVisualizationID);

	    text.control.moduleData.miningPanelSet[text.control.moduleData.findModule(moduleID)].setBackPanelData(lastSetPanelModuleID, lastSetPanelVisualizationID);
	    panelBackButton.setEnabled(true);

		text.control.panelSet(defaultSetPanelModuleID, defaultSetPanelVisualizationID);
	}	

    public final void panelBack()
    {
	text.control.moduleData.miningPanelSet[text.control.moduleData.findModule(moduleID)].panelBack = false;
	panelBackButton.setEnabled(false);

	text.control.backPanelSetByMiningModule(moduleID);
    }

	//JAPANESE
	public boolean isMenuInJapanese()
	{
		return text.settingData.inJapanese;
	}
}
