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

package source;

import source.TextData.*;

import java.io.*;
import java.util.*;

public class Connection
{
	public class ExecutedData
	{
		int moduleID;
		int dataID;

		int dataInteger;
		double dataDouble;
		String dataString;
		
		int dataIntegerArray[];
		double dataDoubleArray[];
		String dataStringArray[];

		boolean executing = false;
		
		ExecutedData(){}
		
		ExecutedData(int mID, int dID)
		{
			moduleID = mID;
			dataID = dID;
		}
	}
	
	ArrayList<ExecutedData> dataList;
	
	boolean executeFlags[];
	int basePanelID;

	TextData text;
	
	public Connection(TextData tex)
	{
		text = tex;
		dataList = new ArrayList<ExecutedData>();
		initializeFlags();		
		System.out.println("DATABASE for "+text.sourceFilename+" was constructed "+dataList.size());
	}
	
	int checkData(int moduleID, int dataID)
	{
		ExecutedData ex;
		for(int i=0;i<dataList.size();i++)
		{
			ex = dataList.get(i);
			if(ex.moduleID == moduleID && ex.dataID == dataID)
				return i;
		}
		
		return -1;
	}
	
	//////DELETE DATA
	
	public boolean deleteData(int moduleID, int dataID)
	{
		int num = checkData(moduleID,dataID);
		
		if(num == -1)
		{
			System.out.println("NOT DELETE Data by module ID="+moduleID+" dataID= "+dataID+" New DATABASE size= "+dataList.size());			
			return false;
		}
		
		dataList.remove(num);
		
		System.out.println("DELETE Data by module ID="+moduleID+" dataID= "+dataID+" New DATABASE size= "+dataList.size());		

		return true;
	}	
	
	//////SET DATA
	
	int setDataCommon(int moduleID, int dataID)
	{
		int num = checkData(moduleID,dataID);
		
		if(num == -1)
		{
			dataList.add(new ExecutedData(moduleID, dataID));
			num = dataList.size()-1;
		}
		
		System.out.println("DATA Created by module ID="+moduleID+" dataID= "+dataID+" New DATABASE size= "+dataList.size());
		
		return num;
	}
	
	void setData(int moduleID, int dataID, int data)
	{
		dataList.get(setDataCommon(moduleID, dataID)).dataInteger = data;
	}
	void setData(int moduleID, int dataID, double data)
	{
		dataList.get(setDataCommon(moduleID, dataID)).dataDouble = data;	
	}
	void setData(int moduleID, int dataID, String data)
	{
		dataList.get(setDataCommon(moduleID, dataID)).dataString = data;		
	}
	void setData(int moduleID, int dataID, int data[])
	{
		dataList.get(setDataCommon(moduleID, dataID)).dataIntegerArray = data;
	}
	void setData(int moduleID, int dataID, double data[])
	{
		dataList.get(setDataCommon(moduleID, dataID)).dataDoubleArray = data;				
	}
	void setData(int moduleID, int dataID, String data[])
	{
		dataList.get(setDataCommon(moduleID, dataID)).dataStringArray = data;		
	}	

	//////GET DATA
	
	int executeModule(int moduleID)
	{
		return text.control.stealthPanel.change_panel_moduleID(moduleID);
	}
	
	void executeModule(int optionNumber, int mp)
	{
		text.control.stealthPanel.executeModule(optionNumber,mp);
	}	
	
	int getDataCommon(int moduleID, int dataID)
	{
		System.out.println("DATA Called from module ID="+moduleID+" dataID= "+dataID);
		
		int num = checkData(moduleID,dataID);
		int mp = -1;
		
		if(num == -1)
		{
			mp = executeModule(moduleID);
			num = checkData(moduleID,dataID);
		}
		
		if(num == -1 && mp != -1)
		{
			System.out.println("EXECUTE module ID="+moduleID+" dataID= "+dataID);
			executeModule(dataID,mp);			//////////////////
			num = checkData(moduleID,dataID);
		}		
		
		return num;
	}
	
	public int getDataInteger(int moduleID, int dataID)
	{
		int num = getDataCommon(moduleID, dataID);
		if(num == -1)
			return -1;
		return dataList.get(num).dataInteger;
	}
	public double getDataDouble(int moduleID, int dataID)
	{
		int num = getDataCommon(moduleID, dataID);
		if(num == -1)
			return -1.0;
		return dataList.get(num).dataDouble;
	}
	public String getDataString(int moduleID, int dataID)
	{
		int num = getDataCommon(moduleID, dataID);
		if(num == -1)
			return "NOT EXECUTED";
		return dataList.get(num).dataString;
	}	
	public int[] getDataIntegerArray(int moduleID, int dataID)
	{
		int num = getDataCommon(moduleID, dataID);
		if(num == -1)
			return null;
		return dataList.get(num).dataIntegerArray;
	}
	public double[] getDataDoubleArray(int moduleID, int dataID)
	{
		int num = getDataCommon(moduleID, dataID);
		if(num == -1)
			return null;
		return dataList.get(num).dataDoubleArray;
	}
	public String[] getDataStringArray(int moduleID, int dataID)
	{
		int num = getDataCommon(moduleID, dataID);
		if(num == -1)
			return null;		
		return dataList.get(num).dataStringArray;
	}

	//EXECUTION	// miningModule -> miningModule
	//EXECUTION	// visualizationModule -> miningModule	
	public void executeModuleWithOptionFirst(int myPanelID, int moduleID, int optionNumber)
	{
		System.out.println("EXECUTE MODULE: PanelID="+myPanelID+" -> module ID= " +moduleID+" option= "+optionNumber);
		
		if(isFlagsClear(executeFlags))
		{
			initializeFlags();			
			basePanelID = myPanelID;
			executeFlags[myPanelID] = true;
		}
		
		int mp = findModule(moduleID);
		
		if(mp != -1)
		{
			executeModuleWithOption(optionNumber,mp);	// Execute Module
			executeFlags[mp] = true;
		}
	}	
	
	
	public void executeModuleWithOption(int myPanelID, int moduleID, int optionNumber)
	{
		System.out.println("EXECUTE MODULE: PanelID="+myPanelID+" -> module ID= " +moduleID+" option= "+optionNumber);
		
		if(isFlagsClear(executeFlags))
		{
			initializeFlags();			
			basePanelID = myPanelID;
			executeFlags[myPanelID] = true;
		}
		
		int mp = findModule(moduleID);
		
		if(mp != -1)
		{
			executeModuleWithOption(optionNumber,mp);	// Execute Module
			executeFlags[mp] = true;
		}
		
		if(myPanelID == basePanelID)
			initializeFlags();
	}
	
	void initializeFlags()
	{
		executeFlags = new boolean[text.control.panelNumber];		
		for(int i=0;i<executeFlags.length;i++)
			executeFlags[i] = false;
		basePanelID = -1;
	}
	
	boolean isFlagsClear(boolean flags[])
	{
		for(int i=0;i<flags.length;i++)
			if(flags[i])
				return false;

		return true;
	}
	
	int findModule(int moduleID)
	{
		return text.control.findModule(moduleID, executeFlags);
	}
	
	int findVisualizationModule(int moduleID)
	{
		return text.control.findVisualizationModule(moduleID, executeFlags);
	}	
	
	void executeModuleWithOption(int optionNumber, int mp)
	{
		text.control.panel[mp].executeModule(optionNumber);
	}

	//DISPLAY // miningModule -> visualizationModule
	//DISPLAY // visualizationModule -> visualizationModule
	public void displayModuleWithOptionFirst(int myPanelID, int moduleID, int optionNumber)
	{
		System.out.println("DISPLAY MODULE: PanelID="+myPanelID+" -> module ID= " +moduleID+" option= "+optionNumber);
		
		if(isFlagsClear(executeFlags))
		{
			initializeFlags();		
			basePanelID = myPanelID;
			executeFlags[myPanelID] = true;
		}
		
		int mp = findVisualizationModule(moduleID);
		
		if(mp != -1)
		{
			displayModuleWithOption(optionNumber,mp);	// Display Module
			executeFlags[mp] = true;
		}
	}	
	
	public void displayModuleWithOption(int myPanelID, int moduleID, int optionNumber)
	{
		System.out.println("DISPLAY MODULE: PanelID="+myPanelID+" -> module ID= " +moduleID+" option= "+optionNumber);
		
		if(isFlagsClear(executeFlags))
		{
			initializeFlags();		
			basePanelID = myPanelID;
			executeFlags[myPanelID] = true;
		}
		
		int mp = findVisualizationModule(moduleID);
		
		if(mp != -1)
		{
			displayModuleWithOption(optionNumber,mp);	// Display Module
			executeFlags[mp] = true;
		}
		
		if(myPanelID == basePanelID)
			initializeFlags();
	}

	public void displayModuleWithOptionFirstFromXXX(int moduleID, int optionNumber)
	{
		System.out.println("DISPLAY MODULE: XXX -> module ID= " +moduleID+" option= "+optionNumber);
		
		if(isFlagsClear(executeFlags))
			initializeFlags();		
		
		int mp = findVisualizationModule(moduleID);
		
		if(mp != -1)
		{
			displayModuleWithOption(optionNumber,mp);	// Display Module
			executeFlags[mp] = true;
		}
	}	
	
	public void displayModuleWithOptionFromXXX(int moduleID, int optionNumber)
	{
		System.out.println("DISPLAY MODULE: XXX -> module ID= " +moduleID+" option= "+optionNumber);
		
		if(isFlagsClear(executeFlags))
			initializeFlags();		
		
		int mp = findVisualizationModule(moduleID);
		
		if(mp != -1)
		{
			displayModuleWithOption(optionNumber,mp);	// Display Module
			executeFlags[mp] = true;
		}

		initializeFlags();
	}	
	
	void displayModuleWithOption(int optionNumber, int mp)
	{
		text.control.panel[mp].displayModule(optionNumber);
	}	
	
}
