/*
 * InputDataSlot.java
 *
 * Created on Ketvirtadienis, 2007, Sausio 25, 17.12
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package Main.data;

import java.util.*;

public class InputDataSlot
{
	
	private float[] data;
	
	public final static String[] DATA_NAMES_CONST={"P", "G", "Q"};
	
	public static String[] DATA_NAMES= {"P", "G", "Q"};
		
	public InputDataSlot()
	{
		data= new float[DATA_NAMES.length];
	}
	
	public InputDataSlot(float[] data)
	{
		this.data= data;
	}
	
	/*public void setP(float p)
	{
		data[getIndexOf("P")]= p;
	}
	public float getP()
	{
		return data[getIndexOf("P")];
	}
	
	public void setQ(float q)
	{
		data[getIndexOf("Q")]= q;
	}
	public float getQ()
	{
		return data[getIndexOf("Q")];
	}
	
	public void setG(float g)
	{
		data[getIndexOf("G")]= g;
	}
	public float getG()
	{
		return data[getIndexOf("G")];
	}*/
	
	public void setData(String name, float value)
	{
		data[getIndexOf(name)]= value;
	}
	
	public float getData(String name)
	{
		return data[getIndexOf(name)];
	}
	
	public float getDataAt(int index)
	{
		return data[index];
	}
	
	public void setDataAt(int index, float value)
	{
		data[index]= value;
	}
	
	public void addDataFields(int number)
	{
		if(number > 0)
		{
			float tmpData[]= new float[data.length+number-1];
			System.arraycopy(data, 0, tmpData, 0, data.length);
			data= tmpData;
		}
	}
	
	public void removeDataFields(int[] indexes)
	{
		float tmpData[]= new float[data.length-indexes.length];
		int pos= 0;
		int j= 0;
		for(int i= 0; i< indexes.length; i++)
		{
			System.arraycopy(data, pos, tmpData, j, indexes[i]-pos);
			j+= indexes[i]-pos;
			pos= indexes[i]+1;
		}
		
		System.arraycopy(data, pos, tmpData, j, data.length- pos);
		data= tmpData;
	}
	
	public void removeDataField(int index)
	{
		float tmpData[]= new float[data.length-1];
		System.arraycopy(data, 0, tmpData, 0, index);
		System.arraycopy(data, index+1, tmpData, index, data.length- index- 1);
		data= tmpData;
	}
	
	public void removeData(String name)
	{
		int index= getIndexOf(name);
		if(index != -1)
		{
			String tmpArray[]= new String[DATA_NAMES.length -1];
			float tmpData[]= new float[tmpArray.length];
			System.arraycopy(DATA_NAMES, 0, tmpArray, 0, index);
			System.arraycopy(DATA_NAMES, index+1, tmpArray, index, DATA_NAMES.length- index- 1);
			System.arraycopy(data, 0, tmpData, 0, index);
			System.arraycopy(data, index+1, tmpData, index, data.length- index- 1);
			
			DATA_NAMES= tmpArray;
			data= tmpData;
		}
	}
	
	public static String createRepresentationString()
	{
		String ret= "";
		for(int i= 0; i< DATA_NAMES.length; i++)
		{
			ret= ret.concat(DATA_NAMES[i]).concat(" ");
		}
		return ret;
	}
	
	public String createDataString()
	{
		String ret= "";
		for(int i= 0; i< DATA_NAMES.length; i++)
		{
			ret= ret +getData(DATA_NAMES[i])+" ";
		}
		return ret;
	}
	
	public static int getIndexOf(String name)
    {
		for(int i= 0; i<DATA_NAMES.length; i++)
		{
			if(DATA_NAMES[i].equals(name))
				return i;
		}
		return -1;
	}
	
	private static void addColumn(String name)
	{
		String tmpArray[]= new String[DATA_NAMES.length + 1];
		System.arraycopy(DATA_NAMES, 0, tmpArray, 0, DATA_NAMES.length);
		tmpArray[tmpArray.length-1]= name;
		DATA_NAMES= tmpArray;
	}
	
	private static int removeColumn(String name)
	{
		int index= getIndexOf(name);
		if(index != -1)
		{
			String tmpArray[]= new String[DATA_NAMES.length -1];
			System.arraycopy(DATA_NAMES, 0, tmpArray, 0, index);
			System.arraycopy(DATA_NAMES, index+1, tmpArray, index, DATA_NAMES.length- index- 1);
			DATA_NAMES= tmpArray;
		}
		return index;
	}
	
	/**
	*  returns indexes of removed columns
	*/
	public static int[] updateColumnNames(String[] names)
	{
		int[] indexes= new int[0];
		List removed= new ArrayList();
		for(int i= 0; i< DATA_NAMES.length;i++)
		{
			boolean found= false;
			for(int j= 0; j< names.length; j++)
			{
				if(names[j].equals(DATA_NAMES[i]))
				{
					found= true;
					break;
				}
				
			}
			if(!found)
			{
				removed.add(DATA_NAMES[i]);
				int[] tmpArray= new int[indexes.length+1];
				System.arraycopy(indexes, 0, tmpArray, 0, indexes.length);
				tmpArray[tmpArray.length-1]= i;
				indexes= tmpArray;
			}
		}
		
		for(int i= 0; i< removed.size(); i++)
		{
			removeColumn((String)removed.get(i));
		}
		
		for(int i= 0; i< names.length; i++)
		{
			boolean found= false;
			for(int j= 0; j< DATA_NAMES.length; j++ )
			{
				if(names[i].equals(DATA_NAMES[j]))
				{
					found= true;
					break;
				}
			}
			if(!found)
			{
				addColumn(names[i]);
			}
			
		}

		return indexes;
	}
	
}
