package de.lazyzero.kkMulticopterFlashTool.utils.EEprom;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.StringWriter;
import java.util.LinkedHashMap;
import java.util.Vector;

import de.lazyzero.kkMulticopterFlashTool.KKMulticopterFlashTool;

public class EEprom {
	
	static boolean DEBUG = false;
	private Vector<Short> eeprom = new Vector<Short>();
	
	private String ifileName = KKMulticopterFlashTool.getTempFolder() + "tmp_i_eeprom.txt";
	private String ofileName = KKMulticopterFlashTool.getTempFolder() + "tmp_o_eeprom.txt";
	
	public EEprom() {
		readEEprom(this.ifileName);
	}

	public EEprom(String ifileName) {
		this.ifileName = ifileName;
		readEEprom(this.ifileName);
	}

	private void readEEprom(String ifileName) {
		String rom = new String();
		try {
			 FileInputStream fs = new FileInputStream(ifileName);
			 DataInputStream in = new DataInputStream(fs);
			 BufferedReader br = new BufferedReader(new InputStreamReader(in));
			 String s;
			 while ((s = br.readLine()) != null)   {
				 rom = rom.concat(s);
			 }
			 in.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		decodeEEprom(rom);
	}
	
	private void writeEEprom(String ofileName) {
		try {
			FileOutputStream fos = new FileOutputStream(ofileName);
			
			String cells = encodeEEprom();
			fos.write(cells.getBytes());
			fos.write(System.getProperty("line.separator").getBytes());
			
			fos.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
			 
	}

	private void decodeEEprom(String rom) {
		eeprom = new Vector<Short>();
		String[] cells = rom.split(",");
		for (int i = 0; i < cells.length; i++) {
			short s = Short.decode(cells[i]);
			eeprom.add(s);
			if (DEBUG) System.out.println(i + " : " + s + " : " + String.format("0x%02x", s));
		}
	}
	
	private String encodeEEprom() {
		String cells = "";
		for (int i = 0; i < eeprom.size(); i++) {
			cells = cells.concat(String.format("0x%02x", eeprom.get(i)));
			if (i < eeprom.size()-1) {
				cells = cells.concat(",");
			}
		}
		return cells;
	}
	
	public Short[] getCells(int startAddress, int length) {
		Short[] s = new Short[length];
		
		for (int i = 0; i < length; i++) {
			s[i] = eeprom.get(i+startAddress);
		}
		return s;
	}

	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		DEBUG = true;
		
		EEprom eeprom = new EEprom("./eeprom_readout/eeprom.txt");
//		eeprom.readEEprom("./eeprom_readout/eeprom.txt");
		
		System.out.println(eeprom.encodeEEprom());
		
		System.out.println(eeprom.eeprom.size());
		eeprom.writeEEprom("./eeprom_readout/o_eeprom.txt");
		
		
		LinkedHashMap<Short, String> dataMapping = new LinkedHashMap<Short, String>();
		dataMapping.put((short) 127, "true");
		dataMapping.put((short) 255, "false");
		
		EEpromDataItem eeDataItem = new EEpromDataItem(eeprom, "Yaw gyro", 0, 1, EEpromDataItem.BOOLEAN, dataMapping , "invert");
		try {
			System.out.println(eeDataItem.getValue());
		} catch (EEpromValueException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	
}
