package com.ym.game.view

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.xynt.R
import com.ym.game.adapter.RecordAdapter
import com.ym.game.contract.RecordContract
import com.ym.game.presenter.RecordPresenter
import com.ym.library.module.WdHistoryEntity
import com.ym.library.rxbus.RxBusUtil

//兑换记录弹窗
object ExchangeRecordDialog : RecordContract.View {

    private var context: Context? = null
    private var mDialog: Dialog? = null
    private var mPresenter: RecordPresenter? = null
    private var adapter: RecordAdapter? = null

    //展示兑换记录窗口
    fun showRecord(context: Context?) {
        if (context == null) {
            return
        }
        this.context = context
        //  EventUtils.onEvent("show_lucky_turntable", "幸运转盘")
        mPresenter = RecordPresenter(this)
        mPresenter?.getWdHistoryList()
        RxBusUtil.getDefault().register(this)
        mDialog = Dialog(context, R.style.UpdateVersionCompatDialogTheme75)
        val mDialogView =
            MainDialog.showBottomDialog(
                R.layout.dialog_layout_record,
                context,
                mDialog!!,
                0,
                Gravity.CENTER
            )
        val rl = mDialog?.findViewById<RecyclerView>(R.id.rl_record)
        rl?.layoutManager = LinearLayoutManager(context)
        adapter = RecordAdapter(context)
        rl?.adapter = adapter
        mDialogView?.findViewById<ImageView>(R.id.iv_dialog_close).setOnClickListener {
            mDialog?.dismiss()
            RxBusUtil.getDefault().unregister(this)
        }

        mDialog?.setOnCancelListener {
            RxBusUtil.getDefault().unregister(this)
        }

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


    override fun WdHistoryResult(result: List<WdHistoryEntity>) {
        adapter?.clear()
        adapter?.appendToList(result)
        adapter?.notifyDataSetChanged()
    }
}