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.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.ym.ddhy.R
import com.ym.game.adapter.RecordAdapter
import com.ym.game.adapter.WareHouseAdapter
import com.ym.game.contract.WareHouseContract
import com.ym.game.presenter.RecordPresenter
import com.ym.game.presenter.WareHousePresenter
import com.ym.library.module.FlyBoxEntity
import com.ym.library.module.WateringTaskEntity
import com.ym.library.rxbus.RxBusUtil
import com.ym.library.utils.EventUtils
import com.ym.library.utils.LogUtils

object WareHouseDialog : WareHouseContract.View {

    private var context: Context? = null
    private var mDialog: Dialog? = null
    private var mPresenter: WareHousePresenter? = null
    private var adapter: WareHouseAdapter? = null

    //展示兑换记录窗口
    fun showWareHouse(context: Context?) {
        if (context == null) {
            return
        }
        this.context = context
        EventUtils.onEvent("show_cangku_dialog", "仓库弹窗曝光")
        mPresenter = WareHousePresenter(this)
        mPresenter?.getwarehouseData()
        RxBusUtil.getDefault().register(this)
        mDialog = Dialog(context, R.style.UpdateVersionCompatDialogTheme75)
        val mDialogView =
            MainDialog.showBottomDialog(
                R.layout.dialog_layout_warehouse,
                context,
                mDialog!!,
                0,
                Gravity.CENTER
            )
        val rl = mDialog?.findViewById<RecyclerView>(R.id.rl_record)
        rl?.layoutManager =
            GridLayoutManager(context,4)
        adapter = WareHouseAdapter(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 warehouseDataResult(result: List<FlyBoxEntity>) {
        adapter?.clear()
        adapter?.appendToList(result)
        adapter?.notifyDataSetChanged()
    }

}