package de.lazyzero.kkMulticopterFlashTool.gui.widgets;

import static lu.tudor.santec.i18n.Translatrix._;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.LinkedHashMap;

import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.border.TitledBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import com.jgoodies.forms.debug.FormDebugPanel;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;

import de.lazyzero.kkMulticopterFlashTool.utils.EEprom.EEprom;
import de.lazyzero.kkMulticopterFlashTool.utils.EEprom.EEpromDataItem;
import de.lazyzero.kkMulticopterFlashTool.utils.EEprom.EEpromValueException;

public class EEpromItemPanel extends FormDebugPanel {
	
	private static final long serialVersionUID = 1L;
	private CellConstraints cc;
	private EEpromDataItem item;

	public EEpromItemPanel(EEpromDataItem item) {
		this.item = item;
		switch (item.getDataType()) {
		//boolean
		case 0:
			createBooleanItem();
			break;

		default:
			break;
		}
		
	}

	private void createBooleanItem() {
		boolean isSelected = false;
		
		// create the CellContraints
		cc = new CellConstraints();

		// create the Layout for Panel this
		String panelColumns = "3dlu,fill:pref:grow,3dlu";
		String panelRows = "3dlu,pref,3dlu";
		FormLayout panelLayout = new FormLayout(panelColumns, panelRows);
		this.setLayout(panelLayout);
		
		this.setBorder(new TitledBorder(_(item.getType())));
		try {
			isSelected = item.getValue();
		} catch (EEpromValueException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		final JCheckBox checkBox = new JCheckBox(_(item.getLabel()), isSelected);
		checkBox.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				System.out.println(e.getSource());
				boolean updated = item.setValue(checkBox.isSelected());
			}
		});
		this.add(checkBox, cc.xy(2, 2));
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		EEprom eeprom = new EEprom("./eeprom_readout/eeprom.txt");
//		eeprom.readEEprom("./eeprom_readout/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, "Gyro", 0, 1, EEpromDataItem.BOOLEAN, dataMapping , "Yaw gyro inverted");
		EEpromItemPanel eei = new EEpromItemPanel(eeDataItem);
		
		JFrame jf = new JFrame();
		jf.add(eei);
		
		jf.pack();
		jf.setVisible(true);
	}

}
