package com.lht.dot.logic.impl;

import android.content.Context;
import android.os.Handler;

import com.lht.common.base.LogicBase;
import com.lht.dot.datasource.UnsplashApiDataSource;
import com.lht.dot.datasource.callback.GetCollectionsCallback;
import com.lht.dot.logic.ICollectionListLogic;
import com.lht.dot.module.models.model.Collection;

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

/**
 * Created by lht-Mac on 2017/7/9.
 */

public class CollectionListLogicImpl extends LogicBase
        implements ICollectionListLogic {

    private int mPage = 0;
    private List<Collection> mCollectionList = new LinkedList<>();
    private UnsplashApiDataSource mDataSource;

    public CollectionListLogicImpl(Context context, Handler handler) {
        super(context, handler);
        mDataSource = new UnsplashApiDataSource();
    }

    @Override
    public void loadCollectionList(int page) {
        mDataSource.listCollectionsFromHttp(page, PAGE_LIMIT, new GetCollectionsCallback() {
            @Override
            public void onSucceed(Collection[] collections) {
                Collections.addAll(mCollectionList, collections);
                notifyUi(MSG_COLLECTION_LIST_LOAD_SUCCESS);
            }

            @Override
            public void onFailed(IOException e) {
                notifyUi(MSG_COLLECTION_LIST_LOAD_FAILED);
            }
        });
    }

    @Override
    public List<Collection> getCollectionList() {
        return mCollectionList;
    }

    @Override
    public void page(int step) {
        mPage += step;
        loadCollectionList(mPage);
    }

    @Override
    public int getPage() {
        return mPage;
    }
}
