package com.zxhl.main.page.view

import android.app.Activity
import android.app.Dialog
import android.text.TextUtils
import android.view.Gravity
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import com.zxhl.cms.common.Constant
import com.zxhl.cms.utils.NoDoubleClickListener
import com.zxhl.cms.utils.Utils
import com.zxhl.main.R
import com.zxhl.main.page.`interface`.PopupWidowCloseCallBack

object RecycleDialog : RecoverPopupContract.View {


    private var context: Activity? = null
    private var mDialog: Dialog? = null
    private var tv_recycle_price: TextView? = null
    private var ids: String = ""
    private var type: String? = ""
    private var recoverPrice: String = ""
    private var recovetype: String = ""
    private var listener: PopupWidowCloseCallBack? = null
    private var mPresenter: RecoverPopupPresenter? = null
    fun showRecycleDialog(
        context: Activity?, ids: String, type: String?,
        recoverprice: String, recove_type: String, listener: PopupWidowCloseCallBack
    ) {
        if (context == null) {
            return
        }
        this.context = context
        this.ids = ids
        this.type = type
        this.recoverPrice = recoverprice
        this.recovetype = recove_type
        this.listener=listener
        mPresenter = RecoverPopupPresenter(this)
        mDialog = Dialog(context, R.style.CenterCompatDialogTheme)
        val mDialogView =
            DialogUtils.showBottomDialog(
                R.layout.dialog_layout_recycle,
                context,
                mDialog!!,
                0,
                Gravity.CENTER
            )
        tv_recycle_price = mDialogView?.findViewById<TextView>(R.id.id_tv_recycle_price)
        mDialogView?.findViewById<ImageView>(R.id.id_img_dialog_close).setOnClickListener {
            mDialog?.dismiss()
        }
        if (type.equals(Constant.Key.ONE_LOTTERY)) {
            tv_recycle_price?.text = recoverPrice
        } else if (type.equals(Constant.Key.FIVE_LOTTERY)) {
            tv_recycle_price?.text = recoverPrice.toDoubleOrNull()?.times(5)?.toInt().toString()
        } else {
            tv_recycle_price?.text = recoverPrice.toDoubleOrNull()?.toInt().toString()
        }
        mDialogView.findViewById<TextView>(R.id.id_tv_sure_recycle).setOnClickListener(object :NoDoubleClickListener(){
            override fun onNoDoubleClick(v: View?) {
                if (!TextUtils.isEmpty(ids)) {
                    mPresenter?.recoverGoods(ids,recovetype)
                }

            }

        })
        mDialog?.setCanceledOnTouchOutside(false)
        mDialog?.setCancelable(false)
        mDialog?.show()
        mDialog?.setOnDismissListener {

        }
    }

    override fun recoverSuccess() {
       mDialog?.dismiss()
        if (type == Constant.Key.ONE_LOTTERY || type == Constant.Key.FIVE_LOTTERY|| type == Constant.Key.RE_PUMPING_ONE_LOTTERY || type == Constant.Key.RE_PUMPING_FIVE_LOTTERY) {
            Utils.showToast(context, "回收成功")
            context?.finish()
        } else {
            listener?.onClosePopupWindow()
        }
    }
}