package graphics.scene;

import java.awt.Color;
import java.awt.event.KeyEvent;

import graphics.userInterface.Menu;
import graphics.userInterface.Window;
import system.managers.SceneManager;

public class ConnectionScene extends Scene {
	private static final long serialVersionUID = 1093416442337197974L;

	private Menu menu;
	private SceneManager sceneManager;
	
	public ConnectionScene(int width, int height, SceneManager sceneManager) {
		super(width, height);
		setLayout(null);
		setName("ConnectionScene");
		setBackground(Color.BLACK);
		setFocusable(true);
		
		this.sceneManager = sceneManager;
		
		Window hideBackground = createWindowWithText(0, 0, 768, 480, null);
		Window info = createWindowWithText(230, 120, 250, 30, "Conecte-se para poder iniciar uma partida");
		Window tooltip = createWindowWithText(0, 0, 768, 30, "Ajuda: W, A, S, D = Direcionais, K = Confirma, L = Cancela");

		String[] optionName = new String[] {
				"Conectar no Netgames", "Iniciar Partida"
		};
		menu = new Menu(260, 160, optionName);
		
		add(menu.getCursor());
		for (int i = 0; i < menu.getOptions().length; i++) {
			add(menu.getOptions()[i]);
		}
		
		Runnable methodOption1 = () -> connect();
		Runnable methodOption2 = () -> startMatch();
		menu.getOptions()[0].setExecuteMethod(methodOption1);
		menu.getOptions()[1].setExecuteMethod(methodOption2);
		
		Runnable cancelMethod = () -> commandStandby();
		menu.setCancelMethod(cancelMethod);

		add(info);
		add(tooltip);
		add(hideBackground);
		
	}
	
	private void commandStandby() {
		System.out.println("Mode Selection Scene: Command Standby");
	}
	
	private void connect() {
		sceneManager.conectar("localhost", "Teste");
	}
	
	private void startMatch() {
		boolean successful = sceneManager.iniciarPartidaRede();
		
		if (successful) {
			menu.disable();			
		}
		
		System.out.println("Start Match");
//		sceneManager.goToScene("BattleScene");//TODO Enviar isso
		
		// if bemsucedido = desabilita essa porra de cena
	}
	
	public void inputManager(KeyEvent e) throws InterruptedException {
		menu.inputManager(e);
	}

	public Menu getMenu() {
		return menu;
	}
}
