package com.bc_chat.account.task

import com.bc_chat.bc_base.entity.SplashAndAdImage
import com.bc_chat.bc_base.entity.SplashImage
import com.bc_chat.bc_base.http.Encryption
import com.bc_chat.bc_base.http.RetrofitHelper
import com.zhaohaoting.framework.abs.entity.ResultEntity
import com.zhaohaoting.framework.abs.task.RxDataAsyncTask
import com.zhaohaoting.framework.requset.rxjava_retrofit.RxResultSubscriber
import io.reactivex.Flowable
import io.reactivex.Flowable.zip
import io.reactivex.functions.BiFunction


/**
 * author：Created by zhaohaoting on 2019/3/5
 * email：526309416@qq.com
 * desc：
 */
class SplashTask : RxDataAsyncTask<SplashAndAdImage, ResultEntity<SplashAndAdImage>>() {
    override fun loadDataAsyncTask(): Flowable<ResultEntity<SplashAndAdImage>>? {
        val guidParams: MutableMap<String, Any?> = mutableMapOf("type" to 2)
        val adParams: MutableMap<String, Any?> = mutableMapOf("type" to 1)

        return zip(RetrofitHelper.api.splashPic(Encryption.encryption(guidParams)), RetrofitHelper.api.splashPic(Encryption.encryption(adParams)),
                BiFunction<ResultEntity<ArrayList<SplashImage>>, ResultEntity<ArrayList<SplashImage>>, ResultEntity<SplashAndAdImage>> { t1, t2 ->
                    val resultEntity = ResultEntity<SplashAndAdImage>()
                    resultEntity.data = SplashAndAdImage()
                    if (RxResultSubscriber.REQUEST_SUCCESS == t1.error || RxResultSubscriber.REQUEST_SUCCESS == t2.error) {
                        resultEntity.error = RxResultSubscriber.REQUEST_SUCCESS
                        if (RxResultSubscriber.REQUEST_SUCCESS == t1.error) {
                            resultEntity.message = t1.message
                            resultEntity.data.guidList = t1.data
                        }
                        if (RxResultSubscriber.REQUEST_SUCCESS == t2.error){
                            resultEntity.message = t2.message
                            resultEntity.data.adList = t2.data
                        }
                    } else {
                        resultEntity.error = RxResultSubscriber.FAILURE
                        resultEntity.message = t1.message
                    }
                    return@BiFunction resultEntity
                })
    }
}
