package com.pengrad.telegrambot.impl;

import com.pengrad.telegrambot.Callback;
import com.pengrad.telegrambot.TelegramBot;
import com.pengrad.telegrambot.UpdatesListener;
import com.pengrad.telegrambot.model.Update;
import com.pengrad.telegrambot.request.GetUpdates;
import com.pengrad.telegrambot.response.GetUpdatesResponse;

import java.io.IOException;
import java.util.List;

import static com.pengrad.telegrambot.UpdatesListener.CONFIRMED_UPDATES_ALL;
import static com.pengrad.telegrambot.UpdatesListener.CONFIRMED_UPDATES_NONE;

/**
 * Stas Parshin
 * 29 September 2016
 */
public class UpdatesHandler {

    private TelegramBot bot;
    private UpdatesListener listener;

    public void start(TelegramBot bot, UpdatesListener listener, GetUpdates request) {
        this.bot = bot;
        this.listener = listener;
        getUpdates(request);
    }

    public void stop() {
        bot = null;
        listener = null;
    }

    private void getUpdates(GetUpdates request) {
        if (bot == null || listener == null) return;

        bot.execute(request, new Callback<GetUpdates, GetUpdatesResponse>() {
            @Override
            public void onResponse(GetUpdates request, GetUpdatesResponse response) {
                if (listener == null) return;

                if (!response.isOk() || response.updates() == null || response.updates().size() <= 0) {
                    getUpdates(request);
                    return;
                }

                List<Update> updates = response.updates();
                int lastConfirmedUpdate = listener.process(updates);

                GetUpdates newRequest;
                if (lastConfirmedUpdate == CONFIRMED_UPDATES_NONE) {
                    newRequest = request;
                } else {
                    int offset = lastConfirmedUpdate == CONFIRMED_UPDATES_ALL
                            ? lastUpdateId(updates) + 1
                            : lastConfirmedUpdate + 1;

                    newRequest = new GetUpdates()
                            .limit(request.getLimit())
                            .timeout(request.getTimeoutSeconds())
                            .offset(offset);
                }
                getUpdates(newRequest);
            }

            @Override
            public void onFailure(GetUpdates request, IOException e) {
                getUpdates(request);
            }
        });
    }

    private int lastUpdateId(List<Update> updates) {
        return updates.get(updates.size() - 1).updateId();
    }

}

@@ -1594,47 +1594,8 @@
 );%0A%0A
-                GetUpdates newRequest;%0A
     
@@ -1622,33 +1622,33 @@
 ConfirmedUpdate 
-=
+!
 = CONFIRMED_UPDA
@@ -1663,75 +1663,8 @@
 ) %7B%0A
-                    newRequest = request;%0A                %7D else %7B%0A
     
@@ -1848,17 +1848,16 @@
 te + 1;%0A
-%0A
         
@@ -1872,187 +1872,25 @@
     
-newRequest = new GetUpdates()%0A                            .limit(request.getLimit())%0A                            .timeout(request.getTimeoutSeconds())%0A                            
+request = request
 .off
@@ -1951,12 +1951,9 @@
 tes(
-newR
+r
 eque

Handle all GetUpdate fields UpdatesHandler #105