package com.swago.room.audio

import android.Manifest
import android.os.Bundle
import android.view.Gravity
import android.widget.Toast
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.launcher.ARouter
import com.permissionx.guolindev.PermissionX
import com.swago.baseswago.constant.ARouteConstant
import com.swago.baseswago.dialog.BaseXDFragment
import com.swago.baseswago.model.live.audio.IMAudioModel
import com.swago.baseswago.util.AppContext
import com.swago.baseswago.util.NoDoubleClickListener
import com.swago.room.R
import com.swago.room.databinding.DialogInvitedByAnchorMicBinding
import com.swago.room.vm.RoomVm
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

class UserByAnchorInviteDialog  : BaseXDFragment<DialogInvitedByAnchorMicBinding>()  {

    private val roomVm by activityViewModels<RoomVm>()


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

    companion object {
        fun newInstance(name:String,avatar:String,maiIndex:Int,applyId:String): UserByAnchorInviteDialog {
            val args = Bundle()
            args.putString("name",name)
            args.putString("avatar",avatar)
            args.putString("applyId",applyId)
            args.putInt("maiIndex",maiIndex)
            val fragment = UserByAnchorInviteDialog()
            fragment.arguments = args
            return fragment
        }
    }

    override fun initOther() {
        arguments?.let {
            val name = it.getString("name","")
            val avatar = it.getString("avatar","")
            val maiIndex = it.getInt("maiIndex",-1)
            val applyId = it.getString("applyId","")
            binding.ivAvatar.loadImage(avatar)
            binding.tvName.text = name
            val time = 20
            binding.tvReject.text = AppContext.getContext().resources.getString(R.string.reject_time).format(time)
            lifecycleScope.launch {
                for (time in 20 downTo 0){
                    delay(1000)
                    if (time==0){
                        roomVm.userAgreeAnchorInvite(applyId,2)
                        dismissAllowingStateLoss()
                    }else{
                        binding.tvReject.text = AppContext.getContext().resources.getString(R.string.reject_time).format(time)
                    }
                }
            }
            binding.tvReject.setOnClickListener(object:NoDoubleClickListener(){
                override fun onClick() {
                    //拒绝
                    binding.tvReject.isEnabled = false
                    roomVm.userAgreeAnchorInvite(applyId,2)
                    dismissAllowingStateLoss()
                }
            })

            binding.llAgree.setOnClickListener(object:NoDoubleClickListener(){
                override fun onClick() {
                    //同意
                    activity?.let {
                        PermissionX.init(it)
                            .permissions(
                                Manifest.permission.RECORD_AUDIO,
                                Manifest.permission.CAMERA,
                                Manifest.permission.READ_EXTERNAL_STORAGE)
                            .request { allGranted, grantedList, deniedList ->
                                if (allGranted) {
                                    roomVm.userAgreeAnchorInvite(applyId,1)
                                    dismissAllowingStateLoss()
                                } else {
                                    Toast.makeText(
                                        AppContext.getContext(),
                                        "These permissions are denied: $deniedList",
                                        Toast.LENGTH_LONG
                                    ).show()
                                }
                            }
                    }
                }
            })
        }
    }

    override fun initLiveData() {
    }
}