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.HomeListEntity
import com.zxhl.cms.net.model.chat.DistanceEntity
import com.zxhl.cms.net.model.chat.RecommendEntity
import com.zxhl.main.page.contract.HomeContract

class HomePresenter : HomeContract.Presenter {
    private val mView: HomeContract.View


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

    private var page = 1
    private val size = 20
    private var isMore = false
    override fun getHomeList(ifRefresh: Boolean, free: Int,category:String) {
//        if (ifRefresh) {
//            page = 1
//            isMore = false
//        } else if (isMore) {
//            return
//        }
//        isMore = true
        if (ifRefresh) {
            clearPage()
        }
        ApiClient.homeApi.getHomelist(page, size, free,category)
            .compose(RxSchedulers.observableIO2Main())
            .subscribe(object : BaseObserver<List<HomeListEntity>>() {
                override fun onSuccess(result: List<HomeListEntity>?) {
                    if (result != null) {
//                        if (result.size <= size) {
//                            page++
//                            isMore = false
//                            mView.setLoadMore(false)
//                        } else {
//                            isMore = true
//                            mView.setLoadMore(true)
//                        }
                        if ((result.size) < size) {
                            mView.setLoadMore(false)
                        } else {
                            mView.setLoadMore(true)
                            page++
                        }
                        mView.setHomeData(ifRefresh, result)
                    }
                }

                override fun onFailure(e: Throwable?, code: String?, errorMsg: String?) {
                    mView.getHomeDataFail()
                }
            })

    }

    override fun getNewUserReceive() {
        ApiClient.homeApi.receiveNewUser()
            .compose(RxSchedulers.observableIO2Main())
            .subscribe(object : BaseObserver<Any>() {
                override fun onSuccess(result: Any?) {
                    mView?.newUserReceiveSuc()
                }

                override fun onFailure(e: Throwable?, code: String?, errorMsg: String?) {
                    mView?.newUserReceivefail()
                }
            })
    }

    override fun likeUser(uid: String?, free: String) {
        ApiClient.homeApi.likeUser(uid, free).compose(RxSchedulers.observableIO2Main())
            .subscribe(object : BaseObserver<Any>() {
                override fun onSuccess(result: Any?) {

                    mView.likeSuc()

                }

                override fun onFailure(e: Throwable?, code: String?, errorMsg: String?) {
                    Log.e("MXL", "ERROR" + code + errorMsg)
                }
            })
    }

    override fun clearPage() {
        page = 1
    }

    override fun getRecommend() {
        ApiClient.homeApi.getRecommendChat().compose(RxSchedulers.observableIO2Main())
            .subscribe(object : BaseObserver<RecommendEntity>() {
                override fun onSuccess(result: RecommendEntity?) {
                    mView.setRecommend(result)

                }

                override fun onFailure(e: Throwable?, code: String?, errorMsg: String?) {
                    Log.e("MXL", "ERROR" + code + errorMsg)
                }
            })
    }

    override fun getDistance(free: Int, uid: String?) {
        ApiClient.homeApi.getDistance(free, uid).compose(RxSchedulers.observableIO2Main())
            .subscribe(object : BaseObserver<DistanceEntity>() {
                override fun onSuccess(result: DistanceEntity?) {
                    if (result != null) {
                        mView.distanceSuc(result)
                    }

                }

                override fun onFailure(e: Throwable?, code: String?, errorMsg: String?) {
                    Log.e("MXL", "ERROR" + code + errorMsg)
                    mView.distanceFail()
                }
            })
    }


}