/*
 * Copyright (C) 2015 Actor LLC. <https://actor.im>
 */

package im.actor.core.modules.internal.messages;

import im.actor.core.api.rpc.RequestLoadArchived;
import im.actor.core.api.rpc.ResponseLoadArchived;
import im.actor.core.modules.ModuleContext;
import im.actor.core.modules.updates.internal.ArchivedDialogLoaded;
import im.actor.core.network.RpcCallback;
import im.actor.core.network.RpcException;
import im.actor.core.util.ModuleActor;
import im.actor.runtime.Log;

public class ArchivedDialogsActor extends ModuleActor {

    private static final String TAG = "ArchivedDialogsActor";

    private static final int LIMIT = 20;

    private static final String KEY_LOADED_OFFSET = "archived_dialogs_offset";

    private byte[] nextOffset;

    private boolean isLoading = false;

    RpcCallback<ResponseLoadArchived> lastCallback;

    public ArchivedDialogsActor(ModuleContext context) {
        super(context);
    }

    private void onLoadMore(boolean init, RpcCallback<ResponseLoadArchived> callback) {

        if (isLoading) {

            //
            // replace old callback
            //
            lastCallback.onError(new RpcException(TAG, 0, "callback replaced", false, null));
            lastCallback = callback;
            return;
        }else{
            lastCallback = callback;
        }

        if(init){
            nextOffset = null;
        }

        isLoading = true;

        Log.d(TAG, "Loading archived dialogs");

        request(new RequestLoadArchived(nextOffset, LIMIT),
                new RpcCallback<ResponseLoadArchived>() {
                    @Override
                    public void onResult(ResponseLoadArchived response) {
                        updates().onUpdateReceived(new ArchivedDialogLoaded(response));
                    }

                    @Override
                    public void onError(RpcException e) {
                        lastCallback.onError(e);
                    }
                });
    }

    private void onLoadedMore(ResponseLoadArchived responseLoadArchiveds) {
        isLoading = false;

        this.nextOffset = responseLoadArchiveds.getNextOffset();
        lastCallback.onResult(responseLoadArchiveds);
        Log.d(TAG, "Archived dialogs loaded");
    }

    // Messages

    @Override
    public void onReceive(Object message) {
        if (message instanceof LoadMore) {
            onLoadMore(((LoadMore) message).isInit(), ((LoadMore) message).getCallback());
        } else if (message instanceof LoadedMore) {
            onLoadedMore(((LoadedMore) message).getResponseLoadArchived());
        } else {
            drop(message);
        }
    }

    public static class LoadMore {
        RpcCallback<ResponseLoadArchived> callback;
        boolean init;
        public LoadMore(boolean init, RpcCallback<ResponseLoadArchived> callback) {
            this.callback = callback;
            this.init = init;
        }

        public boolean isInit() {
            return init;
        }

        public RpcCallback<ResponseLoadArchived> getCallback() {
            return callback;
        }
    }

    public static class LoadedMore {
        ResponseLoadArchived responseLoadArchived;

        public LoadedMore(ResponseLoadArchived responseLoadArchived) {
            this.responseLoadArchived = responseLoadArchived;
        }

        public ResponseLoadArchived getResponseLoadArchived() {
            return responseLoadArchived;
        }
    }
}

@@ -636,87 +636,8 @@
 0;%0A%0A
-    private static final String KEY_LOADED_OFFSET = %22archived_dialogs_offset%22;%0A
 %0A   
@@ -756,16 +756,51 @@
 allback;
+%0A    private long lastRequest = -1;
 %0A%0A    pu
@@ -982,16 +982,24 @@
     if (
+init %7C%7C 
 isLoadin
@@ -1034,23 +1034,22 @@
      // 
-replace
+notify
  old cal
@@ -1053,16 +1053,25 @@
 callback
+ replaced
 %0A       
@@ -1168,32 +1168,38 @@
 se, null));%0A    
+    %7D%0A
         lastCall
@@ -1207,35 +1207,65 @@
 ack = callback;%0A
+%0A
    
+     if(isLoading && !init)%7B%0A
          return;
@@ -1253,24 +1253,27 @@
 )%7B%0A         
+   
 return;%0A    
@@ -1277,20 +1277,34 @@
        %7D
-else
+%0A%0A        if(init)
 %7B%0A      
@@ -1313,61 +1313,86 @@
     
+if(
 last
-Callback = callback;%0A        %7D%0A%0A        if(init)%7B
+Request!=-1)%7B%0A                cancelRequest(lastRequest);%0A            %7D
 %0A   
@@ -1505,24 +1505,37 @@
 logs%22);%0A
-%0A
        
+ lastRequest =
  request

load archive dialogs - cancel last request if init