package com.zxhl.main.page.view


import android.app.Dialog
import android.content.Context
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.net.model.chat.SignEntity
import com.zxhl.cms.net.model.chat.SignReceiveEntity
import com.zxhl.cms.utils.Utils
import com.zxhl.cms.widget.RecyclerViewSpacesItemDecoration
import com.zxhl.main.R
import java.util.HashMap

//签到弹窗
object SignDialog : SignContract.View {
    private var context: Context? = null
    private var mDialog: Dialog? = null
    private var id_tv_sign: TextView? = null
    private var id_tv_current_day: TextView? = null
    private var rl_sign: RecyclerView? = null
    private var mPresenter: SignPresenter? = null
    private var mAdpater: SignAdapter? = null
    fun showSignDialog(context: Context?,listener:View.OnClickListener) {
        if (context == null) {
            return
        }
        this.context = context
        mPresenter = SignPresenter(this)
        mDialog = Dialog(context, R.style.CenterCompatDialogTheme)
        val mDialogView =
            DialogUtils.showBottomDialog(
                R.layout.dialog_layout_sign,
                context,
                mDialog!!,
                0,
                Gravity.CENTER
            )

        rl_sign = mDialogView?.findViewById<RecyclerView>(R.id.id_rl_sign)

        id_tv_sign = mDialogView?.findViewById<TextView>(R.id.id_tv_sign)
        id_tv_current_day = mDialogView?.findViewById<TextView>(R.id.id_tv_current_day)
        mDialogView?.findViewById<ImageView>(R.id.id_img_close).setOnClickListener {
            mDialog?.dismiss()
            listener.onClick(it)
        }
        id_tv_sign?.setOnClickListener {
            mPresenter?.signReceive()
        }

        mPresenter?.getSingList()
        mAdpater = SignAdapter(context)
        val gridLayoutManager: GridLayoutManager = object : GridLayoutManager(context, 4) {
            override fun canScrollVertically(): Boolean {
                return false
            }
        }
        rl_sign?.layoutManager = gridLayoutManager
        val stringIntegerHashMap: HashMap<String, Int> = HashMap()
        stringIntegerHashMap[RecyclerViewSpacesItemDecoration.BOTTOM_DECORATION] =
            Utils.dip2px(10) //
        rl_sign?.addItemDecoration(
            RecyclerViewSpacesItemDecoration(
                stringIntegerHashMap
            )
        )
        rl_sign?.adapter = mAdpater
        mDialog?.setCanceledOnTouchOutside(false)
        mDialog?.setCancelable(false)
        mDialog?.show()

    }

    override fun setSignList(result: SignEntity?) {
        if (result != null) {
            mAdpater?.clear()
            mAdpater?.appendToList(result.list)
            mAdpater?.notifyDataSetChanged()
            id_tv_current_day?.text = result.userProgress.toString()
            if(result.todaySign==false){
                id_tv_sign?.text="签到"
                id_tv_sign?.setBackgroundResource(R.drawable.button_qiandao)
            }else{
               // mDialog?.dismiss()
                id_tv_sign?.text="已签到"
                id_tv_sign?.setBackgroundResource(R.drawable.shape_cccccc_r25)
            }
        }

    }

    override fun getReceiveSuc(result: SignReceiveEntity?) {
        Utils.showToast(context,"签到成功,获得${result?.desc}")
        mPresenter?.getSingList()
        id_tv_sign?.setBackgroundResource(R.drawable.button_qiandao)
      //  id_tv_current_day?.text = (id_tv_current_day?.text.toString().toInt()+1).toString()
    }

    override fun getReceiveFail() {

    }


}