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.bytedance.pangle.transform.ZeusTransformUtils.getResources
import com.zxhl.cms.AppContext
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.*
import com.zxhl.main.page.`interface`.PopupWidowCloseCallBack
import java.text.DecimalFormat


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 listener2: PopupWidowCloseCallBack? = null
    private var mPresenter: RecoverPopupPresenter? = null

    //listener2 为了和使用listener的区分开  逻辑不一样
    fun showRecycleDialog(
        context: Activity?,
        ids: String,
        type: String?,
        recoverprice: String,
        recove_type: String,
        listener: PopupWidowCloseCallBack?,
        listener2: PopupWidowCloseCallBack?
    ) {
        if (context == null) {
            return
        }
        this.context = context
        this.ids = ids
        this.type = type
        this.recoverPrice = recoverprice
        this.recovetype = recove_type
        this.listener = listener
        this.listener2 = listener2
        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)
        var id_recycle_desc = mDialogView?.findViewById<TextView>(R.id.id_recycle_desc)
        if (recove_type == "1") {
            var icon = AppContext.get().resources.getDrawable(R.drawable.icon_mobi, null)
            tv_recycle_price?.setCompoundDrawablesRelativeWithIntrinsicBounds(
                icon,
                null,
                null,
                null
            )
            id_recycle_desc.visibility = View.VISIBLE
        } else {
            var icon = AppContext.get().resources.getDrawable(R.drawable.icon_jinbi, null)
            tv_recycle_price?.setCompoundDrawablesRelativeWithIntrinsicBounds(
                icon,
                null,
                null,
                null
            )
            id_recycle_desc.visibility = View.GONE
        }


        mDialogView?.findViewById<ImageView>(R.id.id_img_dialog_close).setOnClickListener {
            mDialog?.dismiss()
        }
        val df = DecimalFormat("#.00")

        if (type.equals(Constant.Key.ONE_LOTTERY)) {
            tv_recycle_price?.text = "${df.format(recoverPrice.toDoubleOrNull())}"
        } else if (type.equals(Constant.Key.FIVE_LOTTERY)) {
            tv_recycle_price?.text = "${
                recoverPrice.toDoubleOrNull()?.times(
                    5
                )?.toString()
            }"
        } else {
            tv_recycle_price?.text = "${df.format(recoverPrice.toDoubleOrNull())}"
        }
        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?.getString(R.string.recycle_suc))
            if (listener2 != null) {
                listener2?.onClosePopupWindow()
            } else {
                context?.finish()
            }
        } else {
            listener?.onClosePopupWindow()
        }
    }
}