package com.swago.room.hongbao

import android.os.Bundle
import android.view.Gravity
import androidx.lifecycle.lifecycleScope
import com.swago.baseswago.dialog.BaseXDFragment
import com.swago.baseswago.util.AppContext
import com.swago.baseswago.util.NoDoubleClickListener
import com.swago.loadUrl
import com.swago.room.R
import com.swago.room.databinding.DialogRedEnvelopeBroadcastBinding
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

class RedEnvelopBroadcastDialog : BaseXDFragment<DialogRedEnvelopeBroadcastBinding>()  {

    init {
        setGravity(Gravity.CENTER)
        setCanCancel(false)
        setDimAmount(0f)
    }

    var gotoTargetRoom:((anchorId:String)->Unit)?=null

    companion object{
        fun newInstance(senderName:String,senderAvatar:String,anchorId:String,anchorName:String): RedEnvelopBroadcastDialog {
            val args = Bundle()
            args.putString("senderName",senderName)
            args.putString("senderAvatar",senderAvatar)
            args.putString("anchorId",anchorId)
            args.putString("anchorName",anchorName)
            val fragment = RedEnvelopBroadcastDialog()
            fragment.arguments = args
            return fragment
        }
    }

    override fun initOther() {
        arguments?.let {
            val senderName=  it.getString("senderName","")
            val senderAvatar=  it.getString("senderAvatar","")
            val anchorId=  it.getString("anchorId","")
            val anchorName=  it.getString("anchorName","")
            context?.let { context ->
                binding.ivAvatar.loadUrl(context,senderAvatar)
                binding.tvSenderName.text = senderName
                binding.tvDescribe.text = AppContext.getContext().resources.getString(R.string.who_send_red_envelope_broadcast).format(anchorName)
            }

            binding.tvSure.setOnClickListener(object:NoDoubleClickListener(){
                override fun onClick() {
                    gotoTargetRoom?.invoke(anchorId)
                    dismissAllowingStateLoss()
                }
            })
        }

        binding.tvCancel.setOnClickListener(object:NoDoubleClickListener(){
            override fun onClick() {
                dismissAllowingStateLoss()
            }
        })
    }

    override fun initLiveData() {}

    override fun onDestroyView() {
        super.onDestroyView()
    }
}