package zero1hd.polyjet.ui.builders;

import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.badlogic.gdx.utils.Array;

import zero1hd.polyjet.controls.KeyMap;

public class SetControls extends Table {
	public SetControls(Skin skin, KeyMap keyMap) {
		super(skin);
		
		//first
		Label forwardKeyLabel = new Label("Forward: ",skin);
		forwardKeyLabel.setName(KeyMap.UP);
		add(forwardKeyLabel).left();
		KeySetter forwardKeySetter = new KeySetter(keyMap, KeyMap.UP);
		add(forwardKeySetter).spaceRight(45f);
		Label shootKeyLabel = new Label("Shoot: ", skin);
		shootKeyLabel.setName(KeyMap.SHOOT);
		add(shootKeyLabel).left();
		KeySetter shootKeySetter = new KeySetter(keyMap, KeyMap.SHOOT);
		add(shootKeySetter);
		
		row();

		//second
		Label backwardKeyLabel = new Label("Backward: ", skin);
		backwardKeyLabel.setName(KeyMap.DOWN);
		add(backwardKeyLabel).left();
		KeySetter backwardKeySetter = new KeySetter(keyMap, KeyMap.DOWN);
		add(backwardKeySetter).spaceRight(45f);
		Label sector1TPKeyLabel = new Label("Left Teleport", skin);
		sector1TPKeyLabel.setName(KeyMap.FIRSTTHIRDTELEPORT);
		add(sector1TPKeyLabel).left();
		KeySetter Sector1TPKeySetter = new KeySetter(keyMap, KeyMap.FIRSTTHIRDTELEPORT);
		add(Sector1TPKeySetter);
		
		row();
		
		Label leftKeyLabel = new Label("Left: ", skin);
		leftKeyLabel.setName(KeyMap.LEFT);
		add(leftKeyLabel).left();
		KeySetter leftKeySetter = new KeySetter(keyMap, KeyMap.LEFT);
		add(leftKeySetter).spaceRight(45f);
		Label sector2TPKeyLabel = new Label("Middle Teleport: ", skin);
		sector2TPKeyLabel.setName(KeyMap.SECONDTHIRDTELEPORT);
		add(sector2TPKeyLabel).left();
		KeySetter sector2TPKeySetter = new KeySetter(keyMap, KeyMap.SECONDTHIRDTELEPORT);
		add(sector2TPKeySetter);
		
		row();
		
		Label rightKeyLabel = new Label("Right: ", skin);
		rightKeyLabel.setName(KeyMap.RIGHT);
		add(rightKeyLabel).left();
		KeySetter rightKeySetter = new KeySetter(keyMap, KeyMap.RIGHT);
		add(rightKeySetter).spaceRight(45f);
		Label sector3TPKeyLabel = new Label("Right Teleport: ", skin);
		sector3TPKeyLabel.setName(KeyMap.THIRDTHIRDTELEPORT);
		add(sector3TPKeyLabel).left();
		KeySetter sector3TPKeySetter = new KeySetter(keyMap, KeyMap.THIRDTHIRDTELEPORT);
		add(sector3TPKeySetter);
		
		addListener(new ClickListener() {
			@Override
			public void clicked(InputEvent event, float x, float y) {
				unselect();
				getStage().setKeyboardFocus(event.getTarget());
				event.getTarget().setColor(Color.ORANGE);
				super.clicked(event, x, y);
			}
		});
	}
	
	public void unselect() {
		Array<Actor> keys = getChildren();
		for (int i = 0; i < keys.size; i++) {
			keys.get(i).setColor(Color.WHITE);;
		}
	}
	
	@Override
	public void act(float delta) {
		super.act(delta);
	}
}
