package de.lazyzero.kkMulticopterFlashTool.utils.EEprom;

import java.util.Iterator;
import java.util.LinkedHashMap;


public class EEpromDataItem {
	public static int BOOLEAN = 0;
	public static int INTEGER = 1;
	
	private String type = "";
	private int startAddress = 0;
	private int length = 1;
	private String label = "";
	private int dataType = 0;
	private Short[] value;
	private LinkedHashMap<Short, String> dataMapping;
	
	public EEpromDataItem(EEprom eeprom, String type, int startAddress, int length, int dataType, LinkedHashMap<Short, String> dataMapping, String label) {
		setType(type);
		setStartAddress(startAddress);
		setLength(length);
		setDataType(dataType);
		setDataMapping(dataMapping);
		setLabel(label);
		setValues(eeprom);
	}
	
	public boolean getValue() throws EEpromValueException{
		if (value.length == 1) {
			return Boolean.valueOf(dataMapping.get(value[0]));
		} else {
			new EEpromValueException(EEpromValueException.VALUE_TOO_MUCH_ELEMENTS);
			return false;
		}
		 
	}
	
	public boolean setValue(boolean bool) {
		if(dataMapping.containsValue(bool+"")){
			
			for (Iterator iterator = dataMapping.keySet().iterator(); iterator.hasNext();) {
				Short key = (Short) iterator.next();
				if (dataMapping.get(key).equals(bool+"")){
					value[0] = key;
					return true;
				}
			}
		} 

		return false;
	}

	private void setValues(EEprom eeprom) {
		value = eeprom.getCells(startAddress, length);
	}

	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	public int getStartAddress() {
		return startAddress;
	}
	public void setStartAddress(int startAddress) {
		this.startAddress = startAddress;
	}
	public int getLength() {
		return length;
	}
	public void setLength(int length) {
		this.length = length;
	}
	public String getLabel() {
		return label;
	}
	public void setLabel(String label) {
		this.label = label;
	}
	public int getDataType() {
		return dataType;
	}
	public void setDataType(int dataType) {
		this.dataType = dataType;
	}

	private void setDataMapping(LinkedHashMap<Short, String> dataMapping) {
		this.dataMapping = dataMapping;
	}

}
