package org.manzerbredes.open_klm.client;

import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;

import org.manzerbredes.open_klm.drivers.DriverTypeA.*;
import org.manzerbredes.open_klm.drivers.*;

public class MainWindow extends JFrame {
	
	
	private JComboBox<Color> left;
	private JComboBox<Color> middle;
	private JComboBox<Color> right;
	private JButton apply=new JButton("Apply");
	
	private DriverTypeA keyboardTypeA;
	private Class<?> driverType;
	private DriverManager drvMan=new DriverManager();
	
	public MainWindow() throws InstantiationException{
		this.initUI();
		Driver drv=drvMan.getDevice();
		if(drv==null){
			System.err.println("No driver avalaible (try as root)");
			System.exit(1);
		}
		else{
			this.driverType=drv.getType();
			this.keyboardTypeA=(DriverTypeA) drv;
		}
		
		this.left=new JComboBox<>(Color.values());
		this.middle=new JComboBox<>(Color.values());
		this.right=new JComboBox<>(Color.values());
		
		this.apply.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				Color leftRegion=(Color) left.getSelectedItem();
				Color middleRegion=(Color) middle.getSelectedItem();
				Color rightRegion=(Color) right.getSelectedItem();

				keyboardTypeA.setRegionColor(Region.LEFT, leftRegion, Intensity.HIGH);
				keyboardTypeA.setRegionColor(Region.MIDDLE, middleRegion, Intensity.HIGH);
				keyboardTypeA.setRegionColor(Region.RIGHT, rightRegion, Intensity.HIGH);

				
			}
		});
		
		
		JPanel container = new JPanel();
		BoxLayout gridLayout=new BoxLayout(container, BoxLayout.Y_AXIS);
		
		container.add(new Label("Test interface"));
	
		container.add(left);
		container.add(middle);
		container.add(right);
		
		container.add(this.apply);

		this.add(container);
		

		
		this.setVisible(true);
	}
	
	private void initUI(){
		this.setTitle("Open KLM");
		this.setSize(700, 500);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
	}
	
}