package xyz.thastertyn.UserInterface.Content;

import java.util.ArrayList;
import java.util.HashMap;

import com.googlecode.lanterna.TextColor;
import com.googlecode.lanterna.TextColor.ANSI;
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.LayoutData;
import com.googlecode.lanterna.gui2.LinearLayout;
import com.googlecode.lanterna.gui2.Panel;
import com.googlecode.lanterna.gui2.Separator;

import xyz.thastertyn.Types.FinalMark;
import xyz.thastertyn.Types.Subject;
import xyz.thastertyn.Types.Mark;
import xyz.thastertyn.UserInterface.Listeners.UpdateListener;

public class Marks extends JecnaContent {

	private final TextColor.RGB VYBORNY = new TextColor.RGB(85,212,0);
	private final TextColor.RGB CHVALITEBNY = new TextColor.RGB(196,224,80);
	private final TextColor.RGB DOBRY = new TextColor.RGB(255,213,42);
	private final TextColor.RGB DOSTATECNY = new TextColor.RGB(255,102,0);
	private final TextColor.RGB NEDOSTATECNY = new TextColor.RGB(255,48,48);
	private final TextColor.RGB NEHODNOCEN = new TextColor.RGB(0,0,0);
	private final TextColor.RGB UVOLNEN = new TextColor.RGB(28, 107, 255);

	private final LayoutData ALIGN_LEFT = GridLayout.createLayoutData(
		GridLayout.Alignment.BEGINNING,
		GridLayout.Alignment.BEGINNING,
		false,
		false);

	private final LayoutData ALIGN_RIGHT = GridLayout.createLayoutData(
		GridLayout.Alignment.END,
		GridLayout.Alignment.BEGINNING,
		true,
		false);
	
	private xyz.thastertyn.Scrape.Marks marks = new xyz.thastertyn.Scrape.Marks();

	public Marks(UpdateListener listener)
	{
		super(listener);

		this.mainPanel = new Panel(new GridLayout(5));
		this.borderLabel = new Label("Znamky");
		super.scraper = this.marks;
	}

	@Override
	protected void setGUI()
	{
		mainPanel.removeAllComponents();
	
		ArrayList<Subject> subjects = marks.getSubjects();

		// Colors are used a lot and instead of referencing them in lengthy lines, this seems shorter
		HashMap<Integer, SimpleTheme> colors = new HashMap<>();
		colors.put(1, new SimpleTheme(ANSI.BLACK, VYBORNY));
		colors.put(2, new SimpleTheme(ANSI.BLACK, CHVALITEBNY));
		colors.put(3, new SimpleTheme(ANSI.BLACK, DOBRY));
		colors.put(4, new SimpleTheme(ANSI.BLACK, DOSTATECNY));
		colors.put(5, new SimpleTheme(ANSI.BLACK, NEDOSTATECNY));
		colors.put(0, new SimpleTheme(ANSI.BLACK, ANSI.WHITE));
		colors.put(-1, new SimpleTheme(ANSI.WHITE, NEHODNOCEN));
		colors.put(-2, new SimpleTheme(ANSI.WHITE, NEHODNOCEN));
		colors.put(-3, new SimpleTheme(ANSI.WHITE, UVOLNEN));
		colors.put(-4, new SimpleTheme(ANSI.BLACK, NEDOSTATECNY));
		colors.put(-5, new SimpleTheme(ANSI.BLACK, ANSI.WHITE));

		// Column for subject names
		Panel subjectNames = new Panel(new LinearLayout(Direction.VERTICAL))
		.setLayoutData(ALIGN_LEFT)
		.addTo(mainPanel);

		Panel emptySpaceLeft = new Panel(new LinearLayout(Direction.VERTICAL))
		.addTo(mainPanel);

		// Column for marks
		Panel marks = new Panel(new LinearLayout(Direction.VERTICAL))
		.setLayoutData(ALIGN_LEFT)
		.addTo(mainPanel);

		Panel emptySpaceRight = new Panel(new LinearLayout(Direction.VERTICAL))
		.setLayoutData(ALIGN_RIGHT)
		.addTo(mainPanel);

		// Column for final mark
		Panel finalMarks = new Panel(new LinearLayout(Direction.VERTICAL))
		.setLayoutData(ALIGN_RIGHT)
		.addTo(mainPanel);

		for(Subject subject : subjects)
		{
			emptySpaceLeft.addComponent(new Separator(Direction.VERTICAL));
			emptySpaceRight.addComponent(new Separator(Direction.VERTICAL));
			Panel individualMarks = new Panel(new LinearLayout(Direction.HORIZONTAL))
			.addTo(marks);

			if(subject.getMarks().isEmpty())
			{
				individualMarks.addComponent(new Label(""));
			}

			for(Mark mark : subject.getMarks())
			{
				Label markLabel = new Label(mark.toString());
				markLabel.setTheme(colors.get(mark.getMark()));
				individualMarks.addComponent(markLabel);
			}
			
			FinalMark finalMark = subject.getFinalMark();

			Label vysl = new Label(finalMark.toString());
			
			vysl.setTheme(colors.get((int) Math.round(finalMark.getValue())));
			
			subjectNames.addComponent(new Label(subject.getSubjectName()));
			finalMarks.addComponent(vysl);
		}
	}
}