package com.zxhl.main.page.presenter

import android.util.Log
import com.zxhl.cms.net.ApiClient
import com.zxhl.cms.net.RxSchedulers
import com.zxhl.cms.net.callback.BaseObserver
import com.zxhl.cms.net.model.box.MagicCoinLogEntity
import com.zxhl.main.page.contract.MagicCoinContract

class MagicCoinPresenter : MagicCoinContract.Presenter {
    private val mView: MagicCoinContract.View


    constructor(mView: MagicCoinContract.View) {
        this.mView = mView
    }

    private var page: Int = 1
    private val size: Int = 10
    private var isMore = false
    override fun getMagicCoinLog(ifRefresh: Boolean) {
        if (ifRefresh) {
            page = 1
            isMore = false
        } else if (isMore) {
            return
        }
        isMore = true
        ApiClient.homeApi.getMagicCoinLog(page, size)
            .compose(RxSchedulers.observableIO2Main())
            .subscribe(object : BaseObserver<List<MagicCoinLogEntity>>() {
                override fun onSuccess(result: List<MagicCoinLogEntity>?) {
                    if (result != null) {
                        if(!result.isNullOrEmpty()){
                            if (result.size <= size) {
                                page++
                                isMore = false
                            } else {
                                isMore = true
                            }
                            mView.setResult(ifRefresh, result)
                        }else{
                            if(page==1){

                            }

                        }

                    }else{

                    }
                }

                override fun onFailure(e: Throwable?, code: String?, errorMsg: String?) {

                    Log.e("MXL", "ERROR" + code + errorMsg + e)
                }
            })
    }
}