package de.lazyzero.kkMulticopterFlashTool.gui;

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

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.util.Iterator;
import java.util.Vector;

import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;

import avr8_burn_o_mat.AvrdudeProgrammer;

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

import de.lazyzero.kkMulticopterFlashTool.KKMulticopterFlashTool;

public class ProgrammerPanel extends JPanel implements ActionListener, FocusListener {

	private JComboBox programmerCombobox;
	private AvrdudeProgrammer programmer;
	private Vector<AvrdudeProgrammer> programmers;
	private String port;
	private KKMulticopterFlashTool parent;
	private CellConstraints cc;
	private JLabel programmerLabel;
	private JLabel portLabel;
	private JTextField portTextField;
	
	public ProgrammerPanel(KKMulticopterFlashTool parent, Vector<AvrdudeProgrammer> programmers) {
		this.parent = parent;
		this.programmers = programmers;
		
		init();
	}
	
	private void init() {
		//create the CellContraints
		cc  = new CellConstraints();
		
		// create the Layout for Panel this
		String panelColumns = "pref,3dlu,80dlu,3dlu,fill:pref:grow";
		String panelRows = "pref,3dlu,pref";
		FormLayout panelLayout = new FormLayout(panelColumns, panelRows);
		this.setLayout(panelLayout);
		
		
		this.setBorder(new TitledBorder(_("programmer.settings")));
		
		programmerLabel = new JLabel(_("programmer"));
		programmerCombobox = new JComboBox(programmers);
		
		if (programmer!=null){
			programmerCombobox.setSelectedItem(programmer);
		} else {
			programmerCombobox.setSelectedIndex(0);
			programmer = (AvrdudeProgrammer)programmerCombobox.getSelectedItem();
			parent.setProgrammer(programmer);
		}
		programmerCombobox.addActionListener(this);
		
		portLabel = new JLabel(_("port"));
		portTextField = new JTextField(port);
		portTextField.addFocusListener(this);
		
		this.add(programmerLabel,cc.xy(1, 1));
		this.add(programmerCombobox, cc.xyw(3, 1, 3));
		this.add(portLabel, cc.xy(1, 3));
		this.add(portTextField, cc.xy(3, 3));
		
	}
	
	public JPanel getPanel() {
		return this;
	}

	@Override
	public void actionPerformed(ActionEvent action) {
		if (action.getSource().equals(programmerCombobox)){
		    programmer = (AvrdudeProgrammer)programmerCombobox.getSelectedItem();
		    parent.setProgrammer(programmer);
			System.out.println("Programmer switched to: "+programmer.getType());
			
			System.out.println(programmer.getId());
			System.out.println(programmer.getType());
			System.out.println(programmer.getDesc());
			
			
		}
	}

	@Override
	public void focusGained(FocusEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void focusLost(FocusEvent focus) {
		if (focus.getSource().equals(portTextField)) {
			port=portTextField.getText();

			System.out.println("port is now: " + port);
		}
	}

	public void setPort(String port) {
		this.port = port.trim();
		this.portTextField.setText(port);
		System.out.println("Port is set to: " + port);
	}

	public String getPort() {
		return port;
	}

	public void setProgrammer(String id) {
		Iterator<AvrdudeProgrammer> iter = programmers.iterator();
		for (; iter.hasNext();) {
			AvrdudeProgrammer progger = iter.next();
			if (progger.getId().equals(id)){
				programmer = progger;
				parent.setProgrammer(programmer);
				programmerCombobox.setSelectedItem(programmer);
			}
		}
	}

}
