package xyz.thastertyn.ContentDisplay;

import java.util.concurrent.atomic.AtomicBoolean;

import com.googlecode.lanterna.TerminalPosition;
import com.googlecode.lanterna.TerminalSize;
import com.googlecode.lanterna.gui2.Borders;
import com.googlecode.lanterna.gui2.Direction;
import com.googlecode.lanterna.gui2.Label;
import com.googlecode.lanterna.gui2.LinearLayout;
import com.googlecode.lanterna.gui2.Panel;
import com.googlecode.lanterna.gui2.Window;
import com.googlecode.lanterna.gui2.WindowListener;
import com.googlecode.lanterna.input.KeyStroke;
import com.googlecode.lanterna.input.KeyType;

import xyz.thastertyn.ContentDisplay.Content.JecnaContent;
import xyz.thastertyn.ContentDisplay.Content.OmluvnyList;
import xyz.thastertyn.ContentDisplay.Content.Rozvrh;
import xyz.thastertyn.ContentDisplay.Content.Sdeleni;
import xyz.thastertyn.ContentDisplay.Content.Znamky;

public class WindowSwitchListener implements WindowListener {

	private JecnaContent[] contents = {new Rozvrh(), new Znamky(), new Sdeleni(), new OmluvnyList()};
	private Label[] tabs = new Label[contents.length];

	private Panel tabsPanel = new Panel().setLayoutManager(new LinearLayout(Direction.HORIZONTAL));
	private Panel content = new Panel();

	private Panel errorPanel = new Panel().addComponent(new Label("Something went wrong"));

	int current = contents.length - 1;

	public WindowSwitchListener(Panel holder, String title)
	{
		holder.addComponent(tabsPanel);
		holder.addComponent(content.withBorder(Borders.singleLine()));
		
		for(int i = 0; i < contents.length; i++)
		{
			this.tabs[i] = contents[i].getLabel();
			tabsPanel.addComponent(this.tabs[i]);
		}

		next();
	}

	private void next()
	{
		if(current + 1 == contents.length)
		{
			current = 0;
		}else{
			current++;
		}

		if(!contents[current].hasStarted())
		{
			contents[current].download();
		}

		content.removeAllComponents();
		tabsPanel.removeAllComponents();

		if(contents[current].hasStarted())
		{
			int tabColumns = tabsPanel.getSize().getColumns();
			
			if(contents[current].getPanel().getSize().getColumns() < tabColumns)
			{
				content.addComponent(
					contents[current].getPanel().setPreferredSize(new TerminalSize(tabColumns, 
					contents[current].getPanel().getPreferredSize().getRows())));
				
			}else{
				content.addComponent(contents[current].getPanel());
			}
		}else{
			content.addComponent(errorPanel);
		}

		for(int i = 0; i < tabs.length; i++)
		{
			if(i == current)
			{
				tabsPanel.addComponent(tabs[i].withBorder(Borders.doubleLine()));
			}else{
				tabsPanel.addComponent(tabs[i].withBorder(Borders.singleLine()));
			}
		}
	}
	
	@Override
	public void onInput(Window basePane, KeyStroke keyStroke, AtomicBoolean deliverEvent) {
		KeyType type = keyStroke.getKeyType();

		switch(type)
		{
			case Tab:
				next();
				break;
			default:
				break;
		}
	}

	@Override
	public void onUnhandledInput(Window basePane, KeyStroke keyStroke, AtomicBoolean hasBeenHandled) {
		// TODO Auto-generated method stub
	}

	@Override
	public void onResized(Window window, TerminalSize oldSize, TerminalSize newSize) {
		window.invalidate();
	}

	@Override
	public void onMoved(Window window, TerminalPosition oldPosition, TerminalPosition newPosition) {
		// TODO Auto-generated method stub
	}
	
}
