package com.ym.game.view

import android.annotation.SuppressLint
import android.app.Activity
import android.app.Dialog
import android.content.Context
import android.view.Gravity
import android.widget.ImageView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.ym.game.adapter.CashExchangeAdapter
import com.ym.game.contract.CashExchangeContract
import com.ym.game.listener.IDialogViewCloseCallback
import com.ym.library.module.WithDrawEntity2
import com.ym.game.presenter.CashExchangePresenter
import com.ym.library.rxbus.RxBusConstant
import com.ym.library.rxbus.RxBusUtil
import com.ym.library.rxbus.Subscribe
import com.ym.library.rxbus.ThreadMode
import com.ym.ddhy.R
import com.ym.library.utils.EventUtils

object CashExchangeDialog : CashExchangeContract.View {

    private var context: Context? = null
    private var mDialog: Dialog? = null
    private var mPresenter: CashExchangePresenter? = null
    private var cashExchangeAdapter: CashExchangeAdapter? = null

    //展示兑换窗口
    fun showCashExchage(context: Activity?, mListener: IDialogViewCloseCallback) {
        if (context == null) {
            return
        }
        this.context = context
        EventUtils.onEvent("show_duihuan_cash_dialog", "兑换现金")
        mPresenter = CashExchangePresenter(this)
        mPresenter?.getWithDrawList()
        RxBusUtil.getDefault().register(this)
        mDialog = Dialog(context, R.style.UpdateVersionCompatDialogTheme75)
        val mDialogView =
            MainDialog.showBottomDialog(
                R.layout.dialog_layout_cash_exchage,
                context,
                mDialog!!,
                0,
                Gravity.CENTER
            )
        val recyclerexchange = mDialog?.findViewById<RecyclerView>(R.id.rl_cash_exchage)

        recyclerexchange?.layoutManager = LinearLayoutManager(context)
        cashExchangeAdapter = CashExchangeAdapter(context, mDialog!!)
        recyclerexchange?.adapter = cashExchangeAdapter
        mDialogView?.findViewById<ImageView>(R.id.iv_dialog_close).setOnClickListener {
            mDialog?.dismiss()
            mListener.onCloseViewSuccess(true);
            RxBusUtil.getDefault().unregister(this)
        }
        mDialogView?.findViewById<ImageView>(R.id.id_mg_cash_recode).setOnClickListener {
            mDialog?.dismiss()
            mListener.onCloseViewSuccess(true);
            RxBusUtil.getDefault().unregister(this)
            ExchangeRecordDialog.showRecord(context)
        }
        mDialog?.setOnCancelListener {
            RxBusUtil.getDefault().unregister(this)
        }

        mDialog?.setCanceledOnTouchOutside(false)
        mDialog?.setCancelable(false)
        if (context.isFinishing){
            return
        }
        mDialog?.show()
    }


    /**
     * 刷新兑换列表
     */
    @SuppressLint()
    @Subscribe(code = RxBusConstant.RX_WITH_DRAW_LIST, threadMode = ThreadMode.MAIN)
    fun updateWithDrawList() {
        mPresenter?.getWithDrawList()
    }


    override fun WithDrawListResult(result: WithDrawEntity2) {
        cashExchangeAdapter?.clear()
        cashExchangeAdapter?.appendToList(result.wdList)
        cashExchangeAdapter?.notifyDataSetChanged()

    }

    override fun dowdResult() {
    }

}