package com.zxhl.cms.pay

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.chat.RecordEntity


class RecordPresenter:RecordContract.Presenter {
    private val mView: RecordContract.View


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

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

                        }
                    }else{
                        mView.setEmptyView()
                    }
                }

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

                }
            })
    }

    override fun getCoinRecord(ifRefresh: Boolean) {
        if (ifRefresh) {
            page = 1
            isMore = false
        } else if (isMore) {
            return
        }
        isMore = true
        ApiClient.homeApi.getCoinLog(page, size)
            .compose(RxSchedulers.observableIO2Main())
            .subscribe(object : BaseObserver<List<RecordEntity>>() {
                override fun onSuccess(result: List<RecordEntity>?) {
                    if (result != null) {
                        if(!result.isNullOrEmpty()){
                            if (result.size <= size) {
                                page++
                                isMore = false
                            } else {
                                isMore = true
                            }
                            mView.setData(ifRefresh, result)
                        }else{
                            if(page==1){
                                mView.setEmptyView()
                            }

                        }
                    }else{
                        mView.setEmptyView()
                    }
                }

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

                }
            })
    }

}