package com.zxbw.modulemain.presenter

import android.content.Context
import android.text.TextUtils
import android.util.Log
import com.zxbw.modulemain.R
import com.zxbw.modulemain.contract.HomeContract
import com.zxbw.modulemain.contract.HomeRecommendContract
import com.zxbw.modulemain.contract.LoginContract
import com.zxbw.modulemain.contract.ZeroBuyListContract
import com.zxhl.cms.net.ApiClient
import com.zxhl.cms.net.RxSchedulers
import com.zxhl.cms.net.SettingPreference
import com.zxhl.cms.net.callback.BaseObserver
import com.zxhl.cms.net.model.box.AppInEntity
import com.zxhl.cms.net.model.qy.CouponsEntity
import com.zxhl.cms.net.model.qy.HomeBannerEntity
import com.zxhl.cms.net.model.qy.JgqQyEntity
import com.zxhl.cms.net.model.qy.TbGoodsEntity
import com.zxhl.cms.net.model.uc.UserInfo
import com.zxhl.cms.net.model.video.HomeAdEntity
import com.zxhl.cms.utils.EventUtils
import com.zxhl.cms.utils.UserDataUtils
import com.zxhl.cms.utils.Utils
import kotlinx.android.synthetic.main.activity_layout_open_member.*

/**
 * 主页
 */
class HomePresenter : HomeContract.Presenter {

    private val mView: HomeContract.View

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

    override fun getBannerList() {
        ApiClient.homeApi.getHomeBanner()
            .compose(RxSchedulers.observableIO2Main())
            .subscribe(object : BaseObserver<List<HomeBannerEntity>>() {
                override fun onSuccess(result: List<HomeBannerEntity>?) {
                    if (TextUtils.equals(SettingPreference.getSource(), "sh")) {
                        val toMutableList = result?.toMutableList()
                        var entity = HomeBannerEntity()
                        entity.bannerType = "hua_fei"
                        toMutableList?.add(0, entity)
                        toMutableList?.removeAt(toMutableList.size - 1)
                        mView.setBannerList(toMutableList?.toList())
                    } else {
                        mView.setBannerList(result)
                    }
                }

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

    override fun getTabList() {
        ApiClient.homeApi.getAppInfo()
            .compose(RxSchedulers.observableIO2Main())
            .subscribe(object : BaseObserver<AppInEntity>() {
                override fun onSuccess(result: AppInEntity?) {
                    SettingPreference.saveConfig(result)
                    mView.setTabList(result?.tabs)
                }

                override fun onFailure(e: Throwable?, code: String?, errorMsg: String?) {
                    val config = SettingPreference.getConfig();
                    if (config != null && !config.tabs.isNullOrEmpty()) {
                        mView.setTabList(config.tabs)
                    } else {
                        mView.setTabList(null)
                    }
                }
            })
    }

    override fun getFetchCard() {
        ApiClient.homeApi.getFetchallCard().compose(RxSchedulers.observableIO2Main())
            .subscribe(object : BaseObserver<List<CouponsEntity>>() {
                override fun onSuccess(result: List<CouponsEntity>?) {
                    mView.setCouponsList(result)

                }

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

                }
            })
    }

    override fun getAdInfo() {
        ApiClient.homeApi.getHomeAdInfo().compose(RxSchedulers.observableIO2Main())
            .subscribe(object : BaseObserver<List<HomeAdEntity>>() {
                override fun onSuccess(result: List<HomeAdEntity>?) {
                    mView.setAdBanner(result)
                }

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

                }
            })
    }
}