package pt.utl.ist.scripts.runOnce.delegates;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

import net.sourceforge.fenixedu.domain.Person;
import net.sourceforge.fenixedu.domain.elections.DelegateElection;
import net.sourceforge.fenixedu.domain.student.Student;
import net.sourceforge.fenixedu.domain.util.Email;
import pt.utl.ist.scripts.commons.AtomicScript;

public class SendVoteConfirmations extends AtomicScript {

    private static final String fromName = "Comissão Eleitoral de Delegados de Ano";

    private static final String from = "ce-delegados-ano@mlists.ist.utl.pt";

    private static final String subject = "Eleição de delegado de ano - Confirmação de Voto";

    private static final String msg =
            "Caro(a) Aluno(a), \n\n"
                    + "Esta é a confirmação final de registo do seu voto para as Eleições dos Delegados de Ano. \n\nObrigado pela sua participação.";

    @Override
    protected void run() throws Exception {
        final Set<String> emails = new HashSet<String>();

        for (final DelegateElection delegateElection : rootDomainObject.getDelegateElectionsSet()) {
            for (final Student student : delegateElection.getLastVotingPeriod().getVotingStudentsSet()) {
                final Person person = student.getPerson();
                emails.add(person.getEmail());
            }
        }

        new Email(fromName, from, null, Collections.EMPTY_LIST, Collections.EMPTY_LIST, emails, subject, msg);
        final Set<String> toMe = new HashSet<String>();
        toMe.add("luis.cruz@ist.utl.pt");
        new Email(fromName, from, null, Collections.EMPTY_LIST, Collections.EMPTY_LIST, toMe, subject, msg);

        System.out.println("Sent a total of: " + emails.size() + " emails.");
    }

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

}
