package com.swago.room.dialog

import android.os.Bundle
import android.view.Gravity
import com.swago.baseswago.baseroom.RoomTimer
import com.swago.baseswago.baseroom.SwagoRoomManager
import com.swago.baseswago.dialog.BaseXDFragment
import com.swago.baseswago.util.AppContext
import com.swago.baseswago.util.NoDoubleClickListener
import com.swago.room.R
import com.swago.room.databinding.DialogForceCloseRoomBinding

class ForceCloseRoomDialog : BaseXDFragment<DialogForceCloseRoomBinding>(),
    RoomTimer.TimeTickListener {

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

    var time = 5

    companion object{
        fun newInstance(content:String): ForceCloseRoomDialog {
            val args = Bundle()
            args.putString("msg",content)
            val fragment = ForceCloseRoomDialog()
            fragment.arguments = args
            return fragment
        }
    }

    override fun initOther() {
        RoomTimer.timeTickList.add(this)
    }

    override fun initLiveData() {
        arguments?.let {
            val msg = it.getString("msg","")
            binding.tvMsg.text = msg
            binding.tvClose.text = AppContext.getContext().resources.getString(R.string.got_it_ss).format(time)
        }

        binding.tvClose.setOnClickListener(object:NoDoubleClickListener(){
            override fun onClick() {
                SwagoRoomManager.closeRoom()
                dismissAllowingStateLoss()
            }
        })
    }


    override fun onDestroyView() {
        RoomTimer.timeTickList.remove(this)
        super.onDestroyView()
    }

    override fun onTimeCount(i: Int) {
        time--
        if (time==0){
            SwagoRoomManager.closeRoom()
            dismissAllowingStateLoss()
        }else{
            binding.tvClose.text = AppContext.getContext().resources.getString(R.string.got_it_ss).format(time)
        }
    }


}