package org.freecode.irc.votebot;

import org.freecode.irc.votebot.entity.Poll;
import org.freecode.irc.votebot.entity.Vote;

import java.sql.SQLException;
import java.util.concurrent.ScheduledFuture;

/**
 * Created by shivam on 22/04/14.
 */
public class PollExpiryAnnouncer implements Runnable {

    private final long expiry;
    private final int id;
    private boolean hasAnnounced, hasFinished;
    private FreeVoteBot fvb;
    private ScheduledFuture<?> future;

    public PollExpiryAnnouncer(final long expiry, final int id, final FreeVoteBot fvb) {
        this.expiry = expiry;
        this.id = id;
        this.fvb = fvb;
    }

    public void run() {
        long ttl = expiry - System.currentTimeMillis();
        if (!hasAnnounced && ttl <= 300000 && ttl >= 0) { //5 minutes
            hasAnnounced = true;
            fvb.sendMsg(String.format("Poll #%d has less than 5 minutes remaining!", id));
        } else if (ttl <= 0 && !hasFinished) {
            fvb.sendMsg(String.format("Voting for poll #%d has now closed!", id));
            hasFinished = true;
            try {
                Poll poll = fvb.getPollDAO().getPoll(id);
                Vote[] votes = fvb.getVoteDAO().getVotesOnPoll(id);
                int yes = 0, no = 0, abstain = 0;
                for (Vote v : votes) {
                    switch (v.getAnswerIndex()) {
                        case Vote.YES:
                            yes++;
                            break;
                        case Vote.NO:
                            no++;
                            break;
                        case Vote.ABSTAIN:
                            abstain++;
                            break;
                        default:
                            break;
                    }
                }
                fvb.sendMsg(String.format("Question: \"%s\", Yes: %d, No: %d, Abstain: %d", poll.getQuestion(), yes, no, abstain));
            } catch (SQLException e) {
                e.printStackTrace();
            }
            getFuture().cancel(true);
        }
    }

    public void setFuture(ScheduledFuture<?> future) {
        this.future = future;
    }

    public ScheduledFuture<?> getFuture() {
        return future;
    }
}

@@ -366,15 +366,11 @@
 ate 
-boolean
+int
  has
@@ -382,21 +382,12 @@
 nced
-, hasFinished
+ = 0
 ;%0A  
@@ -451,16 +451,70 @@
  future;
+%0A    private static long MILLIS_IN_AN_HOUR = 3600000L;
 %0A%0A    pu
@@ -771,17 +771,17 @@
     if (
-!
+(
 hasAnnou
@@ -786,16 +786,26 @@
 ounced &
+ 1) == 0 &
 & ttl %3C=
@@ -830,124 +830,526 @@
 0) %7B
- //5 minutes%0A            hasAnnounced = true;%0A            fvb.sendMsg(String.format(%22Poll #%25d has less than 5 minute
+%0A            hasAnnounced %7C= 1;%0A            fvb.sendMsg(String.format(%22Poll #%25d has less than an hour remaining!%22, id));%0A        %7D else if ((hasAnnounced & 2) == 0 && ttl %3C= MILLIS_IN_AN_HOUR && ttl %3E= 0) %7B%0A            hasAnnounced %7C= 2;%0A            fvb.sendMsg(String.format(%22Poll #%25d has less than an hour remaining!%22, id));%0A        %7D else if ((hasAnnounced & 4) == 0 && ttl %3C= 6 * MILLIS_IN_AN_HOUR && ttl %3E= 0) %7B%0A            hasAnnounced %7C= 4;%0A            fvb.sendMsg(String.format(%22Poll #%25d has less than six hour
 s re
@@ -1400,20 +1400,65 @@
  && 
-!hasFinished
+((hasAnnounced & Integer.MAX_VALUE) != Integer.MAX_VALUE)
 ) %7B%0A
@@ -1559,23 +1559,37 @@
  has
-Finished = true
+Announced = Integer.MAX_VALUE
 ;%0A  

adding 1 hour and 6 hours