package at.ac.tuwien.inso.dto;

import java.util.Objects;

import at.ac.tuwien.inso.entity.Semester;
import at.ac.tuwien.inso.entity.SemesterType;

//finished transformation on 8.1.
public class SemesterDto {

	private Long id;

	private int year;

	private SemesterType type;

	public SemesterDto() {
	}

	public SemesterDto(int year, SemesterType type) {
		this.year = year;
		this.type = type;
	}

	public Long getId() {
		return id;
	}
	
	public void setId(Long id) {
		this.id = id;
	}

	public String getLabel() {
		return getYear() + " " + getType();
	}

	public int getYear() {
		return year;
	}

	public void setYear(int year) {
		this.year = year;
	}

	public SemesterType getType() {
		return type;
	}

	public void setType(SemesterType type) {
		this.type = type;
	}

	/**
	 * The start date of the semester
	 *
	 * Calculated from automatic start of the semester type.
	 */
	public Calendar getStart() {
		Calendar calendar = new GregorianCalendar(getYear(), getType().getStartMonth(), getType().getStartDay());
		return calendar;
	}

    /**
     * If the semester started in the past
     * @param now date to compare with (needed for testing)
     */
    public boolean isStartInPast(Calendar now) {
        return getStart().before(now);
    }

    /**
     * The semester following after this
     */
    public SemesterDto nextSemester() {
        int currentYear = this.getYear();

        List<SemesterDto> allSemesters = new LinkedList<>();

        int[] possibleYears = {currentYear, currentYear + 1};

        // Create a list of all possible semesters in those 3 years
        for (int year : possibleYears) {
            for (SemesterType type : SemesterType.values()) {
                allSemesters.add(new SemesterDto(year, type));
            }
        }

        // Add a second, so that the same semester does not get propsed again
        Calendar startLimit = getStart();
        startLimit.add(Calendar.SECOND, 1);

        SemesterDto next = allSemesters
                .stream()
                .filter(s -> ! s.isStartInPast(startLimit))
                .sorted(Comparator.comparing(SemesterDto::getStart))
                .findFirst()
                .get();

        return next;
    }

	@Override
	public boolean equals(Object o) {
		if (o == null || getClass() != o.getClass()) {
			return false;
		}

		SemesterDto that = (SemesterDto) o;

		return getYear() == that.getYear() && Objects.equals(getId(), that.getId()) && getType() == that.getType();
	}

	@Override
	public int hashCode() {
		return Objects.hash(getId(), getYear(), getType());
	}

	/**
	 * sets the label to a new Semester instance. The Id will not be persisted.
	 * @return
	 */
	public Semester toEntity(){
		Semester semesterEntity = new Semester(year, type);
		semesterEntity.setId(id);
		return semesterEntity;
	}

    @Override
    public String toString() {
        return "SemesterDto{" + "id=" + id + ", year=" + year + ", type=" + type + '}';
    }
}

@@ -1037,24 +1037,1446 @@
 lendar;%0A%09%7D%0A%0A
+    /**%0A     * SemesterDto that could be the current one%0A     *%0A     * Does not come from the DB, but only is calculated. Needed to find out, if the current%0A     * semester is outdated.%0A     */%0A%09public static SemesterDto calculateCurrentSemester(Calendar now) %7B%0A        int currentYear = now.get(Calendar.YEAR);%0A%0A        List%3CSemesterDto%3E allSemesters = new LinkedList%3C%3E();%0A%0A        int%5B%5D possibleYears = %7BcurrentYear - 1, currentYear, currentYear + 1%7D;%0A%0A        // Create a list of all possible semesters in those 3 years%0A        for (int year : possibleYears) %7B%0A            for (SemesterType type : SemesterType.values()) %7B%0A                allSemesters.add(new SemesterDto(year, type));%0A            %7D%0A        %7D%0A%0A        SemesterDto possibleCurrent = allSemesters%0A                .stream()%0A                .filter(s -%3E s.isStartInPast(now)) // Current semesters that start in the future are not possible%0A                .sorted(Comparator.comparing(SemesterDto::getStart).reversed())%0A                .findFirst()%0A                .get();%0A%0A        return possibleCurrent;%0A    %7D%0A%0A    /**%0A     * If the given Semester is also the current Semester%0A     *%0A     * @param now date to compare with (needed for testing)%0A     */%0A    public boolean isCurrent(Calendar now) %7B%0A        SemesterDto calculated = calculateCurrentSemester(now);%0A        return getYear() == calculated.getYear() && getType() == calculated.getType();%0A    %7D%0A%0A
     /**%0A    

Add SemesterDto.isCurrent