package xyz.thastertyn.Types;

import java.util.ArrayList;

/**
 * Subject for marks. Holds a name, all its marks and the final mark
 */
public class Subject {
	
	private ArrayList<Mark> marks = new ArrayList<>();
	private String subjectName = "";
	private boolean isFinal = false;

	private FinalMark finalMark = FinalMark.UNKNOWN;

	public Subject(String subjectName)
	{
		this.subjectName = subjectName;
	}

	public Subject(String subjectName, FinalMark finalMark)
	{
		this.subjectName = subjectName;
		this.finalMark = finalMark;
		isFinal = true;
	}

	public void addMark(Mark newMark)
	{
		marks.add(newMark);
	}

	private void calculateFinalMark()
	{
		if(marks.isEmpty())
		{
			this.finalMark = FinalMark.NOTHING;
		}else{
			int total = marks
			.stream()
			.mapToInt((z) -> z.getMark() * z.getWeight())
			.sum();
	
			int weight = marks
				.stream()
				.mapToInt((z) -> z.getWeight())
				.sum();
	
			double finalMark = ((double) total / weight);
	
			this.finalMark = new FinalMark(finalMark);
		}
	}

	public FinalMark getFinalMark()
	{
		if(!isFinal)
		{
			calculateFinalMark();
		}

		return finalMark;
	}

	public boolean isFinal()
	{
		return isFinal;
	}

	public String getSubjectName()
	{
		return subjectName;
	}

	public ArrayList<Mark> getMarks()
	{
		return marks;
	}
}

