package com.swago.room.pk

import android.os.Bundle
import android.view.Gravity
import android.view.View
import androidx.fragment.app.viewModels
import com.swago.baseswago.baseroom.RoomTimer
import com.swago.baseswago.dialog.BaseXDFragment
import com.swago.baseswago.model.live.pk.*
import com.swago.baseswago.util.AppContext
import com.swago.baseswago.util.NoDoubleClickListener
import com.swago.baseswago.util.UserInfo
import com.swago.loadUrl
import com.swago.room.R
import com.swago.room.databinding.DialogPkInviteBinding

class PKInviteDialog : BaseXDFragment<DialogPkInviteBinding>(), RoomTimer.TimeTickListener,IPKListener {

    private val pkVm by viewModels<PkVm>()
    private var timeCount = 30

    init {
        setGravity(Gravity.BOTTOM)
        setCanCancel(true)
        setDimAmount(0f)
    }

    companion object{
        fun newInstance(peerPKLiverModel:PKLiverModel): PKInviteDialog {
            val args = Bundle()
            args.putParcelable("pkLiverModel",peerPKLiverModel)
            val fragment = PKInviteDialog()
            fragment.arguments = args
            return fragment
        }
    }


    override fun initOther() {
        RoomTimer.timeTickList.add(this)
        PKStateManager.addPKListener(this)
        arguments?.let {
           val peerPKLiverModel : PKLiverModel? =  it.getParcelable("pkLiverModel")
            peerPKLiverModel?.apply {
                binding.tvPeerName.text = this.user_name
                context?.let { context ->
                    binding.ivPeer.loadUrl(context,this.user_head_img_url)

                    UserInfo.getUserInfo()?.let { userInfoModel ->
                        binding.tvSelfName.text = userInfoModel.user_name
                        binding.ivSelf.loadUrl(context,userInfoModel.user_head_img_url)
                    }
                }
            }

            binding.tvInvite.setOnClickListener(object:NoDoubleClickListener(){
                override fun onClick() {
                    peerPKLiverModel?.let {
                        pkVm.invitePeerPK(it.id)
                        binding.tvInvite.visibility = View.GONE
                        binding.tvCancel.visibility = View.VISIBLE
                    }
                }
            })

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

    override fun initLiveData() {
        pkVm.inviteSuccessLiveData.observe(this){
            timeCount = 30
            binding.tvTimeCount.visibility = View.VISIBLE
            binding.tvTimeCount.text = AppContext.getContext().resources.getString(R.string.inviting_time).format(timeCount)
        }

        pkVm.cancelPKSuccessLiveData.observe(this){
            binding.tvInvite.visibility = View.VISIBLE
            binding.tvCancel.visibility = View.GONE
            binding.tvTimeCount.visibility = View.GONE
        }
    }

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

    override fun onTimeCount(time: Int) {
        timeCount--
        if (timeCount>0){
            binding.tvTimeCount.text = AppContext.getContext().resources.getString(R.string.inviting_time).format(timeCount)
        }
    }

    override fun startPK(startPKModel: StartPKModel, isGetDataFromIM: Boolean) {
        dismissAllowingStateLoss()
    }

    override fun stopPK(stopPKModel: StopPKModel) {
    }

    override fun punishPK(punishPKModel: PunishPKModel, isGetDataFromIM: Boolean) {
    }

    override fun processChangePK(processPKModel: ProcessPKModel,isGetDataFromIM: Boolean) {
    }
}