package xyz.thastertyn.ContentDisplay.Content;

import java.io.IOException;
import java.util.ArrayList;

import com.googlecode.lanterna.TextColor;
import com.googlecode.lanterna.graphics.SimpleTheme;
import com.googlecode.lanterna.gui2.Direction;
import com.googlecode.lanterna.gui2.GridLayout;
import com.googlecode.lanterna.gui2.Label;
import com.googlecode.lanterna.gui2.LinearLayout;
import com.googlecode.lanterna.gui2.Panel;
import com.googlecode.lanterna.gui2.WindowBasedTextGUI;

import xyz.thastertyn.Tuples.Pair;

public class Znamky extends JecnaContent {
	
	private Panel mainPanel = new Panel()
	.setLayoutManager(new GridLayout(3));

	private Label borderLabel = new Label("Znamky");
	private xyz.thastertyn.JecnaParse.Znamky znamky = new xyz.thastertyn.JecnaParse.Znamky();
	
	private boolean hasStarted = false;

	@Override
	public void download()
	{
		try{
			znamky.downloadZnamky();

			Panel predmetyPanel = new Panel()
			.setLayoutManager(new LinearLayout(Direction.VERTICAL))
			.setLayoutData(GridLayout.createLayoutData(
				GridLayout.Alignment.BEGINNING,
				GridLayout.Alignment.BEGINNING,
				false,
				false));

			Panel znamkyPanel = new Panel()
			.setLayoutManager(new LinearLayout(Direction.VERTICAL))
			.setLayoutData(GridLayout.createLayoutData(
				GridLayout.Alignment.BEGINNING,
				GridLayout.Alignment.BEGINNING,
				false,
				false));

			Panel vyslednaZnamkaPanel = new Panel().
			setLayoutManager(new LinearLayout(Direction.VERTICAL))
			.setLayoutData(GridLayout.createLayoutData(
				GridLayout.Alignment.END,
				GridLayout.Alignment.BEGINNING,
				true,
				false));

			ArrayList<Pair<String, Pair<ArrayList<Pair<Integer, Double>>,Double>>> grades = znamky.getGrades();

			TextColor.RGB vyborny = new TextColor.RGB(85,212,0);
			TextColor.RGB chvalitebny = new TextColor.RGB(196,224,80);
			TextColor.RGB dobry = new TextColor.RGB(255,213,42);
			TextColor.RGB dostatecny = new TextColor.RGB(255,102,0);
			TextColor.RGB nedostatecny = new TextColor.RGB(255,48,48);

			for(Pair<String, Pair<ArrayList<Pair<Integer, Double>>,Double>> predmet : grades)
			{

				Panel jednotliveZnamky = new Panel().setLayoutManager(new LinearLayout(Direction.HORIZONTAL));

				if(predmet.getValue1().getValue0().isEmpty())
				{
					jednotliveZnamky.addComponent(new Label(""));
				}

				for(Pair<Integer, Double> znamka : predmet.getValue1().getValue0())
				{
					Label znamkaLabel = new Label(znamka.getValue0() + "");
					switch(znamka.getValue0())
					{
						case -1:
							znamkaLabel.setText("N");
							znamkaLabel.setTheme(new SimpleTheme(TextColor.ANSI.WHITE, TextColor.ANSI.BLACK));
							break;
						case 1:
							znamkaLabel.setTheme(new SimpleTheme(TextColor.ANSI.BLACK, vyborny));
							break;
						case 2:
							znamkaLabel.setTheme(new SimpleTheme(TextColor.ANSI.BLACK, chvalitebny));
							break;
						case 3:
							znamkaLabel.setTheme(new SimpleTheme(TextColor.ANSI.BLACK, dobry));
							break;
						case 4:
							znamkaLabel.setTheme(new SimpleTheme(TextColor.ANSI.BLACK, dostatecny));
							break;
						case 5:
							znamkaLabel.setTheme(new SimpleTheme(TextColor.ANSI.BLACK, nedostatecny));
							break;
					}

					jednotliveZnamky.addComponent(znamkaLabel);
				}
				
				double prumer = predmet.getValue1().getValue1();

				Label vysl = new Label(String.format("%.2f", prumer));

				if(prumer >= 1 && prumer < 1.5)
				{
					vysl.setTheme(new SimpleTheme(TextColor.ANSI.BLACK, vyborny));
				}else if(prumer >= 1.5 && prumer < 2.5)
				{
					vysl.setTheme(new SimpleTheme(TextColor.ANSI.BLACK, chvalitebny));

				}else if(prumer >= 2.5 && prumer < 3.5)
				{
					vysl.setTheme(new SimpleTheme(TextColor.ANSI.BLACK, dobry));

				}else if(prumer >= 3.5 && prumer < 4.5)
				{
					vysl.setTheme(new SimpleTheme(TextColor.ANSI.BLACK, dostatecny));
				}else if(prumer >= 4.5)
				{
					vysl.setTheme(new SimpleTheme(TextColor.ANSI.BLACK, nedostatecny));
				}

				znamkyPanel.addComponent(jednotliveZnamky);
				predmetyPanel.addComponent(new Label(predmet.getValue0()));
				vyslednaZnamkaPanel.addComponent(vysl);
			}
			
			mainPanel.addComponent(predmetyPanel);
			mainPanel.addComponent(znamkyPanel);
			mainPanel.addComponent(vyslednaZnamkaPanel);

			hasStarted = true;
		}catch(IOException e)
		{
			mainPanel.addComponent(new Label("An error has occured"));
		}
	}

	@Override
	public Panel getPanel()
	{
		return mainPanel;
	}

	@Override
	public boolean hasStarted()
	{
		return hasStarted;
	}

	@Override
	public Label getLabel()
	{
		return borderLabel;
	}

	@Override
	public void specify(int y, int hy) {
		// TODO Auto-generated method stub
		throw new UnsupportedOperationException("Unimplemented method 'specify'");
	}

	@Override
	public void getOptions(WindowBasedTextGUI textGUI) {
		// TODO Auto-generated method stub
		throw new UnsupportedOperationException("Unimplemented method 'getOptions'");
	}
}
