package com.nutrition.express.main;

import com.nutrition.express.model.rest.ApiService.UserService;
import com.nutrition.express.model.rest.ResponseListener;
import com.nutrition.express.model.rest.RestCallback;
import com.nutrition.express.model.rest.RestClient;
import com.nutrition.express.model.rest.bean.BaseBean;
import com.nutrition.express.model.rest.bean.BlogPosts;

import java.util.HashMap;

import retrofit2.Call;

/**
 * Created by huang on 11/2/16.
 */

public class DashboardPresenter implements DashboardContract.Presenter, ResponseListener {
    private DashboardContract.View view;
    private UserService userService;
    private Call<BaseBean<BlogPosts>> call;
    private int defaultLimit = 20;
    private int offset = 0;
    private boolean hasNext = true, reset = false;
    private String type;

    public DashboardPresenter(DashboardContract.View view, String type) {
        this.view = view;
        this.type = type;
        userService = RestClient.getInstance().getUserService();
    }

    private void getDashboardPosts() {
        if (call == null) {
            HashMap<String, String> options = new HashMap<>(3);
            options.put("limit", "" + defaultLimit);
            options.put("offset", "" + offset);
            options.put("type", type);
            call = userService.getDashboard(options);
            call.enqueue(new RestCallback<BlogPosts>(this, "dashboard"));
        }
    }

    @Override
    public void refresh() {
        offset = 0;
        hasNext = true;
        reset = true;
        getDashboard();
    }

    @Override
    public void getDashboard() {
        getDashboardPosts();
    }

    @Override
    public void getNextDashboard() {
        getDashboardPosts();
    }

    @Override
    public void onAttach() {

    }

    @Override
    public void onDetach() {
        view = null;
    }

    @Override
    public void onResponse(BaseBean baseBean, String tag) {
        if (view == null) {
            return;
        }
        call = null;
        BlogPosts posts = (BlogPosts) baseBean.getResponse();
        offset += posts.getList().size();
        if (posts.getList().size() < defaultLimit) {
            hasNext = false;
        }
        if (reset) {
            reset = false;
            view.resetData(posts.getList(), hasNext);
        } else {
            view.showDashboard(posts.getList(), hasNext);
        }
    }

    @Override
    public void onError(int code, String error, String tag) {
        if (view == null) {
            return;
        }
        call = null;
        view.onError(code, error);
    }

    @Override
    public void onFailure(Throwable t, String tag) {
        if (view == null) {
            return;
        }
        call = null;
        view.onFailure(t);
    }

}

@@ -368,16 +368,72 @@
 ogPosts;
+%0Aimport com.nutrition.express.model.rest.bean.PostsItem;
 %0A%0Aimport
@@ -451,16 +451,39 @@
 HashMap;
+%0Aimport java.util.List;
 %0A%0Aimport
@@ -813,24 +813,85 @@
 offset = 0;%0A
+    private long lastTimestamp = System.currentTimeMillis();%0A
     private 
@@ -2165,26 +2165,38 @@
     
-BlogPosts
+List%3CPostsItem%3E
  posts
+Items
  = 
+(
 (Blo
@@ -2225,16 +2225,27 @@
 sponse()
+).getList()
 ;%0A      
@@ -2257,34 +2257,29 @@
 set += posts
-.getList()
+Items
 .size();%0A   
@@ -2292,26 +2292,21 @@
 f (posts
-.getList()
+Items
 .size() 
@@ -2438,34 +2438,29 @@
 etData(posts
-.getList()
+Items
 , hasNext);%0A
@@ -2476,16 +2476,115 @@
  else %7B%0A
+            postsItems = removeDuplicate(postsItems);%0A            if (postsItems.size() %3E 0) %7B%0A    
         
@@ -2611,26 +2611,21 @@
 rd(posts
-.getList()
+Items
 , hasNex
@@ -2628,33 +2628,206 @@
 sNext);%0A        
-%7D
+    %7D else %7B%0A                getNextDashboard();%0A                return;%0A            %7D%0A        %7D%0A        lastTimestamp = postsItems.get(postsItems.size() - 1).getTimestamp();
 %0A    %7D%0A%0A    @Ove
@@ -3195,10 +3195,420 @@
 %0A    %7D%0A%0A
+    private List%3CPostsItem%3E removeDuplicate(List%3CPostsItem%3E postsItems) %7B%0A        int i = 0;%0A        for (; i %3C postsItems.size(); i++) %7B%0A            if (lastTimestamp %3E postsItems.get(i).getTimestamp()) %7B%0A                break;%0A            %7D%0A        %7D%0A        if (i %3E 0 && i %3C postsItems.size()) %7B%0A            postsItems = postsItems.subList(i, postsItems.size());%0A        %7D%0A        return postsItems;%0A    %7D%0A%0A
 %7D%0A

remove duplicate posts when loading next page