package com.ym.game.presenter


import android.app.Activity
import android.util.Log
import android.view.View
import com.ym.game.contract.PuzzleContract
import com.ym.library.module.PuzzleEntity
import com.ym.library.net.BaseObserver
import com.ym.library.net.GameApiClient
import com.ym.library.net.RxSchedulers


class PuzzlePresenter : PuzzleContract.Presenter {

    var mView: PuzzleContract.View

    constructor(mView: PuzzleContract.View) {
        this.mView = mView
    }
    // 获取卡片列表
    override fun getPuzzleData() {
          GameApiClient.gameApi.getCollectCard().compose(RxSchedulers.observableIO2Main()).subscribe(object : BaseObserver<PuzzleEntity?>() {
            override fun onSuccess(result: PuzzleEntity?) {
                if (result != null) {
                  mView?.PutPuzzDataResult(result)
                }
            }

            override fun onFailure(e: Throwable?, code: String?, errorMsg: String?) {}
        })
    }
        //已有宝箱卡片拼图
    override fun putPuzzleData(index:Int) {
          GameApiClient.gameApi.getCollectCardPut(index).compose(RxSchedulers.observableIO2Main()).subscribe(object : BaseObserver<PuzzleEntity?>() {
            override fun onSuccess(result: PuzzleEntity?) {
                if (result != null) {
                    mView.cardsFlyUp(result,index)

                }
            }

            override fun onFailure(e: Throwable?, code: String?, errorMsg: String?) {}
        })
    }
    //看广告单个图片获取或者拼图
    override fun noputPuzzleData(index: Int) {
          GameApiClient.gameApi.getCollectCardPutorAdd(index).compose(RxSchedulers.observableIO2Main()).subscribe(object : BaseObserver<PuzzleEntity?>() {
            override fun onSuccess(result: PuzzleEntity?) {
                if (result != null) {
                    mView?.PutPuzzDataResult(result)
                }
            }

            override fun onFailure(e: Throwable?, code: String?, errorMsg: String?) {}
        })
    }
    //看广告一键拼图
    override fun onecleanPuzzle() {
          GameApiClient.gameApi.getCollectCardAdv().compose(RxSchedulers.observableIO2Main()).subscribe(object : BaseObserver<PuzzleEntity?>() {
            override fun onSuccess(result: PuzzleEntity?) {
                if (result != null) {
                    mView?.PutPuzzDataResult(result)
                       if(result.advert==1){
                        //去看广告
                        mView?.getPuzzlelookAd(result.diffCardsNum)
                    }
                }
            }

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

    //一键拼图
    override fun automatic() {
          GameApiClient.gameApi.getCollectCardAutoMatic().compose(RxSchedulers.observableIO2Main()).subscribe(object : BaseObserver<PuzzleEntity?>() {
            override fun onSuccess(result: PuzzleEntity?) {
                if (result != null) {
                    mView?.PutPuzzDataResult(result)
                    Log.e("MXL",""+result.isWin)
                    if(result.advert==1){
                        //去看广告
                     mView?.getPuzzlelookAd(result.diffCardsNum)
                    }

                }
            }

            override fun onFailure(e: Throwable?, code: String?, errorMsg: String?) {}
        })
    }
    //开宝箱
    override fun openBox() {
          GameApiClient.gameApi.openCard().compose(RxSchedulers.observableIO2Main()).subscribe(object : BaseObserver<PuzzleEntity?>() {
            override fun onSuccess(result: PuzzleEntity?) {
                if (result != null) {
                    mView?.PutPuzzDataResult(result)

                }
            }

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


}