package com.zxhl.main.page.view

import android.app.Dialog
import android.content.Context
import android.util.Log
import android.view.Gravity
import android.view.animation.AnimationUtils
import android.widget.ImageView
import android.widget.TextView
import com.zxhl.cms.common.Constant
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.utils.EventUtils
import com.zxhl.cms.utils.JumpUtils
import com.zxhl.main.R
import kotlinx.android.synthetic.main.activity_layout_goods_detail.*

//9.9秒杀弹窗
object DiscountDialog {
    private var context: Context? = null
    private var mDialog: Dialog? = null
    private var tv_miao_sha: TextView? = null
    fun showDiscountDialog(context: Context?) {
        if (context == null) {
            return
        }
        this.context = context
        mDialog = Dialog(context, R.style.CenterCompatDialogTheme)
        val mDialogView =
            DialogUtils.showBottomDialog(
                R.layout.dialog_layout_discount,
                context,
                mDialog!!,
                0,
                Gravity.CENTER
            )
        tv_miao_sha = mDialogView?.findViewById<TextView>(R.id.id_tv_miaosha)
        mDialogView?.findViewById<ImageView>(R.id.id_img_dialog_close).setOnClickListener {
            EventUtils.onEvent("9.9_dialog_close","9.9弹窗关闭")
            mDialog?.dismiss()
            ApiClient.homeApi.canceChoose("-3")
                .compose(RxSchedulers.observableIO2Main())
                .subscribe(object : BaseObserver<Any>() {
                    override fun onSuccess(result: Any?) {

                    }

                    override fun onFailure(e: Throwable?, code: String?, errorMsg: String?) {
                        Log.e("MXL", "error" + errorMsg + "code" + code + e)
                    }
                })
        }
        tv_miao_sha?.setOnClickListener {
            EventUtils.onEvent("9.9_dialog_btn_click","9.9弹窗按钮点击")
            mDialog?.dismiss()
            var ownCoin = SettingPreference.getUserCoinNum().toDoubleOrNull()
            if (ownCoin?.compareTo(9.9)!! >= 0) {
                JumpUtils.lotteryBoxJump("-3", Constant.Key.ONE_LOTTERY, "9.9")
                //  JumpUtils.lotteryBoxJump(boxid, Constant.Key.ONE_LOTTERY)
            } else {
                if (ownCoin != null) {
                    val chajia = 9.9.minus(ownCoin!!)
                    JumpUtils.payJump("-3", chajia.toString(), Constant.Key.ONE_LOTTERY)
                }

            }
        }
        tv_miao_sha?.animation =
            AnimationUtils.loadAnimation(context, R.anim.button_scale_anim)
        mDialog?.setCanceledOnTouchOutside(false)
        mDialog?.setCancelable(false)
        mDialog?.show()
        EventUtils.onEvent("9.9_dialog_show","9.9弹窗曝光")
        mDialog?.setOnDismissListener {
        }
    }

}