package com.hi.music.player.ui.paging;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.paging.PagingSource;
import androidx.paging.PagingState;

import com.hi.music.player.api.RequestListener;
import com.hi.music.player.helper.CommonUtils;
import com.hi.music.player.javabean.response.ResponseHome;
import com.hi.music.player.network.JsonHelper;
import com.hi.music.player.network.RetrofitManager;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Single;
import kotlin.coroutines.Continuation;
import okhttp3.ResponseBody;

public class MyPagingSource extends PagingSource<Integer, ResponseHome> {



    @Nullable
    @Override
    public Integer getRefreshKey(@NonNull PagingState<Integer, ResponseHome> pagingState) {
        return null;
    }

    @Nullable
    @Override
    public Object load(@NonNull LoadParams<Integer> loadParams, @NonNull Continuation<? super PagingSource.LoadResult<Integer, ResponseHome>> continuation) {
        int nextPage = 1;
        Integer key = loadParams.getKey();
        if (key != null) {
            nextPage = key + 1;
        }

//        try {
//            // 使用 Single 来处理异步请求
//
//            List<ResponseHome> result = Single.<List<ResponseHome>>create(emitter -> {
//                        RetrofitManager.getInstance().getHomeData();
//                    })
//                    .blockingGet(); // 阻塞获取结果
//
//            return new LoadResult.Page<>(
//                    result,
//                    page == 1 ? null : page - 1,
//                    result.isEmpty() ? null : page + 1
//            );
//        } catch (Exception e) {
//            return new LoadResult.Error<>(e);
//        }
//
        return null;
    }
}
