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 int hasAnnounced = 0;
    private FreeVoteBot fvb;
    private ScheduledFuture<?> future;
    private static long MILLIS_IN_AN_HOUR = 3600000L;

    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();
        String time_announcement = "Poll #%d has less than %s remaining!";

        try {
            Poll poll = fvb.getPollDAO().getPoll(id);
            String question = poll.getQuestion();
            Vote[] votes = fvb.getVoteDAO().getVotesOnPoll(id);
            int total = votes.length;
            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;
                }
            }

            if ((hasAnnounced & 1) == 0 && ttl <= 2 * 300000 && ttl >= 0) {
                hasAnnounced |= 1;
                fvb.sendMsg(String.format(time_announcement, id, question, "10 minutes"));
            } else if ((hasAnnounced & 3) == 0 && ttl <= MILLIS_IN_AN_HOUR * 2 && ttl >= 0) {
                hasAnnounced |= 2;
                fvb.sendMsg(String.format(time_announcement, id, question, "two hours"));
            } else if ((hasAnnounced & 7) == 0 && ttl <= 12 * MILLIS_IN_AN_HOUR && ttl >= 0) {
                hasAnnounced |= 4;
                fvb.sendMsg(String.format(time_announcement, id, question, "twelve hours"));
            } else if (ttl <= 0 && ((hasAnnounced & Integer.MAX_VALUE) != Integer.MAX_VALUE)) {
                hasAnnounced = Integer.MAX_VALUE;
                String result = (total >= 5 && yes > no && yes > abstain) ? "passed" : "did not pass";
                fvb.sendMsg(String.format("Poll #%d %s!", id, result));
                getFuture().cancel(true);
            }
            fvb.sendMsg(String.format("Question: \"%s\", Yes: %d, No: %d, Abstain: %d", question, yes, no, abstain));
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

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

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

@@ -1770,24 +1770,146 @@
 minutes%22));%0A
+                fvb.sendMsg(String.format(%22Question: %5C%22%25s%5C%22, Yes: %25d, No: %25d, Abstain: %25d%22, question, yes, no, abstain));%0A
             
@@ -2107,32 +2107,154 @@
  %22two hours%22));%0A
+                fvb.sendMsg(String.format(%22Question: %5C%22%25s%5C%22, Yes: %25d, No: %25d, Abstain: %25d%22, question, yes, no, abstain));%0A
             %7D el
@@ -2452,32 +2452,154 @@
 welve hours%22));%0A
+                fvb.sendMsg(String.format(%22Question: %5C%22%25s%5C%22, Yes: %25d, No: %25d, Abstain: %25d%22, question, yes, no, abstain));%0A
             %7D el
@@ -2911,60 +2911,8 @@
     
-            getFuture().cancel(true);%0A            %7D%0A
     
@@ -3017,32 +3017,88 @@
  no, abstain));%0A
+                getFuture().cancel(true);%0A            %7D%0A
         %7D catch 

Stop spam...