package com.zxhl.main.page.view

import android.app.Activity
import android.app.Dialog
import android.content.Context
import android.content.Entity
import android.text.TextUtils
import android.util.Log
import android.view.Gravity
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.zxhl.cms.common.Constant
import com.zxhl.cms.net.SettingPreference
import com.zxhl.cms.net.model.box.HomeListEntity
import com.zxhl.cms.net.model.chat.ChatMessageChargeOrNotEntity
import com.zxhl.cms.net.model.chat.OneChatUpEntity
import com.zxhl.cms.utils.JumpUtils
import com.zxhl.cms.utils.OnRecycleItemClickListener
import com.zxhl.cms.utils.RxBus
import com.zxhl.cms.utils.Utils
import com.zxhl.cms.widget.ChatDialogUtils
import com.zxhl.cms.widget.LoadingDialog
import com.zxhl.cms.widget.MemberDialogUtils
import com.zxhl.cms.widget.RecyclerViewSpacesItemDecoration
import com.zxhl.main.R
import java.util.HashMap

object AccostDialog : AccostContract.View, OnRecycleItemClickListener<Boolean> {
    private var context: Context? = null
    private var mDialog: Dialog? = null
    private var mPresenter: AccostPresenter? = null
    private var rl_accost: RecyclerView? = null
    private var id_tv_on_click_chat_up: TextView? = null
    private var mAdpater: AccostAdapter? = null
    private var mLoading: LoadingDialog? = null
    fun showAccostDialog(context: Context?, list: List<HomeListEntity>?) {
        if (context == null) {
            return
        }
        this.context = context
        mLoading = LoadingDialog.getLoadingDialog(
            context,
           "请稍后",
            false,
            false
        )
        mPresenter = AccostPresenter(this)
        mDialog = Dialog(context, R.style.CenterCompatDialogTheme)
        val mDialogView =
            DialogUtils.showBottomDialog(
                R.layout.dialog_layout_accost,
                context,
                mDialog!!,
                0,
                Gravity.CENTER
            )

        rl_accost = mDialogView?.findViewById<RecyclerView>(R.id.id_rl_accost)
        id_tv_on_click_chat_up = mDialogView?.findViewById<TextView>(R.id.id_tv_on_click_chat_up)
        mDialogView?.findViewById<ImageView>(R.id.id_img_close).setOnClickListener {
            mDialog?.dismiss()
        }
        id_tv_on_click_chat_up?.setOnClickListener {
            if (mChoosList.isNullOrEmpty()) {
                return@setOnClickListener
            }
            val sb = StringBuilder()
            for (dataBean in mChoosList) {
                if (sb.length > 0) {
                    sb.append(",")
                }
                sb.append(dataBean)
            }
            mLoading?.setLoading("操作中")
            mLoading?.show()
            mPresenter?.onClickChatUp(sb.toString())
        }

        mAdpater = AccostAdapter(context, this)
        val gridLayoutManager: GridLayoutManager = object : GridLayoutManager(context, 3) {
            override fun canScrollVertically(): Boolean {
                return false
            }
        }
        rl_accost?.layoutManager = gridLayoutManager
        val stringIntegerHashMap: HashMap<String, Int> = HashMap()
        stringIntegerHashMap[RecyclerViewSpacesItemDecoration.BOTTOM_DECORATION] =
            Utils.dip2px(10) //
        stringIntegerHashMap[RecyclerViewSpacesItemDecoration.LEFT_DECORATION] =
            Utils.dip2px(15) //左间距
        stringIntegerHashMap[RecyclerViewSpacesItemDecoration.RIGHT_DECORATION] =
            Utils.dip2px(15) //右间距
        rl_accost?.addItemDecoration(
            RecyclerViewSpacesItemDecoration(
                stringIntegerHashMap
            )
        )
        rl_accost?.adapter = mAdpater
        setData(list)
        if (!list.isNullOrEmpty()) {
            for (i in list) {
                mChoosList.add(i.uid.toString())
            }
        }

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

    }

    fun setData(result: List<HomeListEntity>?) {
        if (result != null) {
            mAdpater?.clear()
            mAdpater?.appendToList(result)
            mAdpater?.notifyDataSetChanged()
        }

    }


    override fun chatUpfinish(result: OneChatUpEntity?) {
        if (mLoading != null) {
            mLoading?.dismiss()
        }
        mDialog?.dismiss()
        RxBus.get().post(Constant.SWITCH_PAGE, 2)
        if (result != null) {
            if (result.badMoney >0) {

                ChatDialogUtils.showMemberTrip(
                    context as Activity,
                    object : ChatDialogUtils.ChatDialogCallBack {
                        override fun confirm() {
                            JumpUtils.starCoinpayJump()
                        }

                        override fun cancel() {}
                    })
            }
        }
    }

    override fun chatupFail() {
        if (mLoading != null) {
            mLoading?.dismiss()
        }
    }


    var mChoosList: MutableList<String> = ArrayList()
    override fun onItemClick(view: View?, position: Int, data: Boolean?) {
        val entity = mAdpater?.getItem(position)
        if (data == true) {
            if (!mChoosList.contains(entity?.uid)) {
                mChoosList.add(entity?.uid!!)
            }
        } else {
            mChoosList.remove(entity?.uid)
        }

        Log.e("MXL", mChoosList.toString())
    }
}
