package pt.utl.ist.scripts.runOnce;

import java.util.Comparator;
import java.util.TreeSet;

import net.sourceforge.fenixedu.domain.ExecutionDegree;
import net.sourceforge.fenixedu.domain.ExecutionYear;
import net.sourceforge.fenixedu.domain.degree.DegreeType;
import net.sourceforge.fenixedu.domain.finalDegreeWork.FinalDegreeWorkGroup;
import net.sourceforge.fenixedu.domain.finalDegreeWork.GroupStudent;
import net.sourceforge.fenixedu.domain.student.Registration;
import net.sourceforge.fenixedu.domain.student.Student;
import pt.utl.ist.scripts.commons.AtomicScript;

public class FixRegistrationsForStudentGroups extends AtomicScript {

    @Override
    protected void run() throws Exception {
        final ExecutionYear executionYear = ExecutionYear.readCurrentExecutionYear();
        for (final ExecutionDegree executionDegree : executionYear.getExecutionDegreesSet()) {
            for (final FinalDegreeWorkGroup finalDegreeWorkGroup : executionDegree.getAssociatedFinalDegreeWorkGroupsSet()) {
                for (final GroupStudent groupStudent : finalDegreeWorkGroup.getGroupStudentsSet()) {
                    final Registration currentRegistration = groupStudent.getRegistration();
                    final Registration newRegistration = findSomeRegistration(currentRegistration.getStudent());
                    if (newRegistration != null && newRegistration != currentRegistration) {
                        System.out.println(currentRegistration.getDegree().getSigla() + " ---> "
                                + newRegistration.getDegree().getSigla());
                        groupStudent.setRegistration(newRegistration);
                    }
                }
            }
        }
    }

    private Registration findSomeRegistration(final Student student) {
        final TreeSet<Registration> registrations = new TreeSet<Registration>(new Comparator<Registration>() {

            @Override
            public int compare(final Registration r1, final Registration r2) {
                final DegreeType dt1 = r1.getDegreeType();
                final DegreeType dt2 = r2.getDegreeType();
                return 0 - dt1.compareTo(dt2);
            }

        }) {

            @Override
            public boolean add(final Registration r) {
                final DegreeType degreeType = r.getDegreeType();
                return isValidDegreeType(degreeType) && super.add(r);
            }

            private boolean isValidDegreeType(DegreeType degreeType) {
                return degreeType == DegreeType.BOLONHA_MASTER_DEGREE
                        || degreeType == DegreeType.BOLONHA_INTEGRATED_MASTER_DEGREE || degreeType == DegreeType.BOLONHA_DEGREE;
            }

        };
        registrations.addAll(student.getRegistrationsSet());
        return registrations.isEmpty() ? null : registrations.first();
    }

    public static void main(String[] args) {
        processWriteTransaction(new FixRegistrationsForStudentGroups());
        System.exit(0);
    }

}
