package com.zxbw.modulemain.activity

import android.app.Activity
import android.util.Log
import android.view.View
import com.zx.ad.AdManager
import com.zx.ad.callback.RewardVideoAdListener
import com.zxbw.modulemain.R
import com.zxbw.modulemain.view.DialogUtils
import com.zxhl.cms.AppContext
import com.zxhl.cms.common.base.BaseActivity
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.NoticeEntity
import com.zxhl.cms.net.model.video.VideoInfoEntity
import com.zxhl.cms.utils.JumpUtils
import kotlinx.android.synthetic.main.layout_pay_suc.*

class PaySuccessActivity : BaseActivity() {
    var type = ""
    override fun onClick(v: View?) {

    }

    override fun before() {
        super.before()
        setStatusBarBackground(AppContext.get().resources.getColor(R.color.transparent))
    }

    override fun layoutID(): Int {
        return R.layout.layout_pay_suc
    }

    override fun init() {

        type = intent?.data?.getQueryParameter("pay_type") ?: ""
        if (type.equals("2")) {
            id_tv_desc_tips.text = "支付成功\n话费充值将在72小时内到账！"
        } else {
            id_tv_desc_tips.text = "恭喜您\n本次交易成功！"
        }
        id_img_back2?.setOnClickListener {
            finish()
        }
        id_tv_btn_order?.setOnClickListener {
            JumpUtils.MyOrderJump("")
            finish()
        }
        getVideoInfo()
    }

    fun getVideoInfo() {
        ApiClient.homeApi.getVideoInfo()
            .compose(RxSchedulers.observableIO2Main())
            .subscribe(object : BaseObserver<VideoInfoEntity>() {
                override fun onSuccess(result: VideoInfoEntity?) {
                    if (result != null) {
                        if (result.leftCount > 0) {
                            DialogUtils.showViewInfo(
                                mActivity as Activity,
                                result.content,
                                View.OnClickListener {
                                    lookVideo()
                                })
                        }
                    }


                }

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

                }
            })
    }

    fun getReward() {

        ApiClient.homeApi.getVideoReward()
            .compose(RxSchedulers.observableIO2Main())
            .subscribe(object : BaseObserver<VideoInfoEntity>() {
                override fun onSuccess(result: VideoInfoEntity?) {
                    if (result != null) {

                        DialogUtils.showGetReward(
                            mActivity as Activity,
                            result?.leftCount,
                            result.content,
                            View.OnClickListener {
                                JumpUtils.PhoneVoucherJump()
                            },
                            View.OnClickListener {
                                lookVideo()
                            })

                    }


                }

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

                }
            })
    }

    fun lookVideo() {
        AdManager.loadRewardAd(this, object : RewardVideoAdListener {
            override fun onRewardVideoAdClose() {
                Log.e("MXL", "onRewardVideoAdClose")
                getReward()
            }

            override fun onRewardVideoAdReward() {

            }

            override fun onRewardVideoAdShow() {
                Log.e("MXL", "onRewardVideoAdShow")
            }

            override fun onRewardVideoAdError(errorMsg: String?) {
                Log.e("MXL", "errorMsg:" + errorMsg)
            }
        })
    }
}