package xyz.thastertyn.ContentDisplay;

import java.io.IOException;
import java.util.Arrays;

import com.googlecode.lanterna.TextColor;
import com.googlecode.lanterna.gui2.BasicWindow;
import com.googlecode.lanterna.gui2.Borders;
import com.googlecode.lanterna.gui2.DefaultWindowManager;
import com.googlecode.lanterna.gui2.Direction;
import com.googlecode.lanterna.gui2.EmptySpace;
import com.googlecode.lanterna.gui2.LinearLayout;
import com.googlecode.lanterna.gui2.MultiWindowTextGUI;
import com.googlecode.lanterna.gui2.Panel;
import com.googlecode.lanterna.gui2.Window;
import com.googlecode.lanterna.screen.Screen;
import com.googlecode.lanterna.screen.TerminalScreen;
import com.googlecode.lanterna.terminal.DefaultTerminalFactory;
import com.googlecode.lanterna.terminal.Terminal;

public class MainWindow {

	private Terminal terminal;
	private Screen screen;
	private Window window;
	private MultiWindowTextGUI textGUI;

	
	public void run()
	{
		try {
			//#region init
			// Setup terminal and screen layers
			terminal = new DefaultTerminalFactory().createTerminal();

			screen = new TerminalScreen(terminal);
			screen.startScreen();
			
			// Create window to hold the panel
			window = new BasicWindow();
			window.setHints(Arrays.asList(Window.Hint.CENTERED));

			// Create gui and start gui
			textGUI = new MultiWindowTextGUI(screen, new DefaultWindowManager(), new EmptySpace(TextColor.ANSI.BLACK_BRIGHT));
			//#endregion

			// Create panel to hold components
			final Panel mainPanel = new Panel();
			mainPanel.setLayoutManager(new LinearLayout(Direction.VERTICAL));
			
			window.setComponent(mainPanel);

			String title = "Jecnak"; 
			
			Panel content = new Panel();
			mainPanel.addComponent(content.withBorder(Borders.singleLine(title)));

			LoginController controller = new LoginController(textGUI);
			controller.login();

			window.addWindowListener(new WindowSwitchListener(content, title));

			textGUI.addWindowAndWait(window);

			
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
