package de.lazyzero.kkMulticopterFlashTool.gui;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.JTabbedPane;
import javax.swing.border.TitledBorder;

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

import de.lazyzero.kkMulticopterFlashTool.KKMulticopterFlashTool;
import de.lazyzero.kkMulticopterFlashTool.gui.widgets.FirmwareFilePanel;
import de.lazyzero.kkMulticopterFlashTool.gui.widgets.FirmwareRepositoryPanel;
import de.lazyzero.kkMulticopterFlashTool.utils.ButtonsStateListener;
import de.lazyzero.kkMulticopterFlashTool.utils.XmlReaderFirmwares;

public class FirmwarePanel extends JTabbedPane implements ButtonsStateListener, PropertyChangeListener {

	private static final long serialVersionUID = 1L;
	private XmlReaderFirmwares firmwareLoader;
	private KKMulticopterFlashTool parent;
	
	private boolean buttonsEnabled = true;
	private FirmwareFilePanel firmwarefilePanel;
	private FirmwareRepositoryPanel firmwareRepositoryPanel;

	public FirmwarePanel(KKMulticopterFlashTool parent,
			XmlReaderFirmwares firmwareLoader) {
		this.parent = parent;
		this.firmwareLoader = firmwareLoader;
		init();
		this.addPropertyChangeListener(this);
	}

	private void init() {
		firmwarefilePanel = new FirmwareFilePanel(parent);
		firmwareRepositoryPanel = new FirmwareRepositoryPanel(parent, firmwareLoader);
		
		this.addTab("Repository", firmwareRepositoryPanel);
		this.addTab("File", firmwarefilePanel);
		
		this.setBorder(new TitledBorder(_("Flashing")+"..."));
		
		this.setSelectedIndex(0);
	}

	@Override
	public void setButtonsEnabled(boolean b) {
		this.buttonsEnabled = b;
	}

	@Override
	public void updateButtons() {
		firmwarefilePanel.setButtonsEnabled(this.buttonsEnabled);
		firmwareRepositoryPanel.setButtonsEnabled(this.buttonsEnabled);
		firmwarefilePanel.updateButtons();
		firmwareRepositoryPanel.updateButtons();
	}

	@Override
	public void propertyChange(PropertyChangeEvent evt) {
		if (evt.getPropertyName().equals(ControllerPanel.CONTROLLER_CHANGED)) {
			firmwareRepositoryPanel.firePropertyChange(ControllerPanel.CONTROLLER_CHANGED, 0, 1);
		}
	}

	

}
