package de.lazyzero.kkMulticopterFlashTool.gui.widgets;

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

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;

import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListCellRenderer;

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

import de.lazyzero.kkMulticopterFlashTool.utils.Firmware;

public class FirmwareListCellRenderer implements ListCellRenderer {

	private static final long serialVersionUID = 1L;
	
	private boolean hasPot;
	private boolean hasLCD;
	private boolean hasPIEZO;
	private boolean hasMEMS;
	private boolean hasACC;
	private boolean hasCPPM;

	@Override
    public Component getListCellRendererComponent(
        JList list,
        Object value,
        int index,
        boolean isSelected,
        boolean cellHasFocus)
    {
		JPanel panel = new JPanel();
		// create the CellContraints
		CellConstraints cc = new CellConstraints();

		// create the Layout for Panel this
		String panelColumns = "3dlu,fill:pref:grow";
		String panelRows = "1dlu,pref:grow,2dlu,pref:grow,2dlu,pref:grow,1dlu";
		FormLayout panelLayout = new FormLayout(panelColumns, panelRows);
		panel.setLayout(panelLayout);
		
    	if (value == null) {
			value = _("firmware.FirmwareCombobox.nonAvailable");
		}
    	Color bg = isSelected ? list.getSelectionBackground() : (index%2==0||index==-1?list.getBackground().brighter():list.getBackground());
    	Color fg = Color.black;
    	if (value instanceof Firmware) setRequirementsAndFeatures((Firmware)value);
    	panel.setBackground(bg);
  		if (!System.getProperty("os.name").toLowerCase().contains("mac")) {
  			fg = isSelected ? list.getSelectionForeground() : list.getForeground();
  			panel.setForeground(fg);
  		}
  		
  		panel.setFont(list.getFont());
    	if (value instanceof Firmware) {
    		Firmware f = ((Firmware)value);
    		JLabel line1 = new JLabel(f.getName() +  " V" + f.getVersion() + " " + f.getVersionName() + " by " + f.getAuthor());
    		line1.setForeground(fg);
    		JLabel line2 = new JLabel(f.getServer());
    		line2.setForeground(fg);
    		panel.add(line1, cc.xy(2, 2));
    		panel.add(line2, cc.xy(2, 4));
    		if (f.getTarget() == Firmware.TARGET_KK) {
    			JPanel line3 = getLine(f, fg, bg);
    			panel.add(line3, cc.xy(2, 6));
    		}
    	} else {
    		JLabel warning = new JLabel((String) value);
    		warning.setForeground(fg);
    		panel.add(warning, cc.xy(2, 2));
    	}
    	
    	return panel;
    }
	
	
	
	private JPanel getLine(Firmware firmware, Color fg, Color bg) {
		int target = firmware.getTarget();
		JPanel panel = new JPanel();
		// create the CellContraints
		CellConstraints cc = new CellConstraints();

		// create the Layout for Panel this
		
		String panelRows = "pref";
		
		if  (target == Firmware.TARGET_KK) {
			String panelColumns = "pref,3dlu,pref,3dlu,pref,3dlu,pref,3dlu,pref,3dlu,pref";
			FormLayout panelLayout = new FormLayout(panelColumns, panelRows);
			panel.setLayout(panelLayout);
			panel.setForeground(fg);
			panel.setBackground(bg);
			panel.add(new ToggledLable("pot", hasPot, fg, bg), cc.xy(1, 1));
			panel.add(new ToggledLable("LCD", hasLCD, fg, bg), cc.xy(3, 1));
			panel.add(new ToggledLable("piezo", hasPIEZO, fg, bg), cc.xy(5, 1));
			panel.add(new ToggledLable("MEMS", hasMEMS, fg, bg), cc.xy(7, 1));
			panel.add(new ToggledLable("acc", hasACC, fg, bg), cc.xy(9, 1));
			panel.add(new ToggledLable("CPPM", hasCPPM, fg, bg), cc.xy(11, 1));
		} else {
			
		}
		panel.setOpaque(true);
		return panel;
	}

	private void setRequirementsAndFeatures(Firmware firmware) {
		int features = firmware.getFeatures();
		int target = firmware.getTarget();
		
		if (target == Firmware.TARGET_KK) {
			if (features != -1) {
				hasPot = (features & Firmware.POT) != 0;
				hasLCD = (features & Firmware.LCD) != 0;
				hasPIEZO = (features & Firmware.PIEZO) != 0;
				hasMEMS = (features & Firmware.MEMS) != 0;
				hasACC = (features & Firmware.ACC) != 0;
				hasCPPM = (features & Firmware.CPPM) != 0;
			} else {
				hasPot = false;
				hasLCD = false;
				hasPIEZO = false;
				hasMEMS = false;
				hasACC = false;
				hasCPPM = false;
			}
		} else if (target == Firmware.TARGET_ESC) {
			
		}
	}
	
	
}
