package com.ym.game.presenter

import android.app.Activity
import com.ym.game.contract.ClockInWithdrawContract
import com.ym.game.listener.IDialogViewCloseCallback
import com.ym.game.view.MainDialog
import com.ym.library.module.ClockInWithdrawEntity
import com.ym.library.module.ExchangeEntity
import com.ym.library.module.FlyBoxEntity
import com.ym.library.net.BaseObserver
import com.ym.library.net.GameApiClient
import com.ym.library.net.RxSchedulers
import com.ym.library.utils.EventUtils

class ClockInWithdrawPresenter : ClockInWithdrawContract.Presenter {

    var mView: ClockInWithdrawContract.View

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


    override fun getClockInWithdrawList() {
        GameApiClient.gameApi.getClockList().compose(
            RxSchedulers.observableIO2Main()
        ).subscribe(object : BaseObserver<ClockInWithdrawEntity>() {
            override fun onSuccess(result: ClockInWithdrawEntity?) {
                mView?.clockInWithdrawListResult(result)
            }

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

    override fun getDoWd(id:Int) {
        GameApiClient.gameApi.getDoWd(id).compose(
            RxSchedulers.observableIO2Main()
        ).subscribe(object : BaseObserver<ExchangeEntity>() {
            override fun onSuccess(result: ExchangeEntity?) {
                mView?.dowdResult(result)
            }

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


    fun getFlyBoxReceive(activity: Activity) {
        GameApiClient.gameApi.geClockBoxReceive()
            .compose(RxSchedulers.observableIO2Main())
            .subscribe(object : BaseObserver<FlyBoxEntity>() {
                override fun onSuccess(result: FlyBoxEntity?) {
                    getClockInWithdrawList()
                    if (result != null) {
                        var type = result.type
                        if (type != null) {
                            EventUtils.onEvent("attendance_task_box_receive_dialog_show")
                            MainDialog.showopenBox(
                                activity, type,
                                "宝箱奖励",
                                result.awardNum ?: 0,
                                result,
                                object : IDialogViewCloseCallback {
                                    override fun onCloseViewSuccess(isSuccess: Boolean) {

                                    }
                                }
                            )
                        }
                    }
                }

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

}