package com.swago.room.dialog

import android.os.Bundle
import android.view.Gravity
import androidx.fragment.app.viewModels
import com.swago.baseswago.baseroom.SwagoRoomManager
import com.swago.baseswago.dialog.BaseXDFragment
import com.swago.baseswago.util.NoDoubleClickListener
import com.swago.baseswago.util.SwagoLoading
import com.swago.room.bean.UserRoomModel
import com.swago.room.databinding.DialogAudioNoticeBinding
import com.swago.room.vm.RoomOtherVm

class AudioNoticeDialog : BaseXDFragment<DialogAudioNoticeBinding>() {

    private val roomOtherVm by viewModels<RoomOtherVm>()

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

    private var content = ""

    companion object{
        fun newInstance(): AudioNoticeDialog {
            val args = Bundle()
            val fragment = AudioNoticeDialog()
            fragment.arguments = args
            return fragment
        }
    }

    override fun initOther() {
        SwagoRoomManager.iRoomInfo?.let {
            binding.etNotice.setText(it.getAudioNotice())
            binding.etNotice.setSelection(it.getAudioNotice().length)
        }
        binding.tvCancel.setOnClickListener(object:NoDoubleClickListener(){
            override fun onClick() {
                dismissAllowingStateLoss()
            }
        })

        binding.tvSure.setOnClickListener(object:NoDoubleClickListener(){
            override fun onClick() {
                content = binding.etNotice.text.toString()
                activity?.let {
                    SwagoLoading.showLoadingDialog(it)
                    roomOtherVm.updateAudioNotice(content)
                }
            }
        })
    }

    override fun initLiveData() {
        roomOtherVm.noticeUpdateLiveData.observe(this){
            SwagoRoomManager.iRoomInfo?.let {
                (it as UserRoomModel).roomModel?.yuying_notice_content = content
            }
            SwagoLoading.cancelLoadingDialog()
            dismissAllowingStateLoss()
        }
    }
}