package com.swago.baseswago

import android.Manifest
import android.os.Bundle
import android.text.TextUtils
import android.view.Gravity
import android.view.View
import android.widget.Toast
import androidx.fragment.app.viewModels
import com.alibaba.android.arouter.launcher.ARouter
import com.bumptech.glide.Glide
import com.permissionx.guolindev.PermissionX
import com.swago.baseswago.baseroom.SwagoRoomManager
import com.swago.baseswago.constant.ARouteConstant
import com.swago.baseswago.databinding.DialogPersonDataBinding
import com.swago.baseswago.dialog.BaseXDFragment
import com.swago.baseswago.dialog.HandleDialogFragment
import com.swago.baseswago.dialog.ReportDialog
import com.swago.baseswago.util.AppContext
import com.swago.baseswago.util.IMUtil
import com.swago.baseswago.util.NoDoubleClickListener
import com.swago.baseswago.util.UserInfo

/**
 *@date 2021/8/27 17:14
 *description:用户资料
 */
class PersonDataDFragment : BaseXDFragment<DialogPersonDataBinding>() {

    private val userVm by viewModels<UserVm>()
    private val handleVm by viewModels<HandleVm>()
    var isFollow = 0
    var isBlock = 0
    var uid = ""
    var nickName = ""
    var account = ""
    var isAnchor = false //在直播间是不是打开资料卡
    private var inRoom = false
    private var isForbid = 0 //0没有被禁言  1已被禁言
    var openGiftDialog:((nickName:String,userId:String)->Unit)? = null

    companion object{
        fun newInstance(uid:String,isAnchor:Boolean=false,inRoom:Boolean=false): PersonDataDFragment {
            val dialog = PersonDataDFragment()
            val bundle = Bundle()
            bundle.putString("uid",uid)
            bundle.putBoolean("inRoom",inRoom)
            bundle.putBoolean("isAnchor",isAnchor)
            dialog.arguments = bundle
            return dialog
        }
    }

    override fun initOther() {
        setDimAmount(0f)
        arguments?.let {
            uid = it.getString("uid","")
            inRoom = it.getBoolean("inRoom")
            isAnchor = it.getBoolean("isAnchor")
            if (!TextUtils.isEmpty(uid)){
                userVm.getOtherUserInfo(uid)
            }

            if (inRoom){
                setGravity(Gravity.BOTTOM)
            }else{
                setGravity(Gravity.CENTER)
            }


            UserInfo.getUserInfo()?.let {
                if (it.id == uid){
                    binding.ivGift.visibility = View.GONE
                    binding.llChat.visibility = View.GONE
                    binding.llToFollow.visibility = View.GONE
                    binding.ivMore.visibility = View.GONE
                    binding.ivReport.visibility = View.GONE
                }else{
                    if (inRoom){
                        SwagoRoomManager.iRoomInfo?.let {
                            handleVm.getUserRoomAuth(uid,it.getRoomId())
                        }
                        binding.ivGift.visibility = View.VISIBLE
                    }else{
                        binding.ivGift.visibility = View.GONE
                    }
                    binding.llChat.visibility = View.VISIBLE
                    binding.ivMore.visibility = View.VISIBLE

                    binding.ivReport.visibility = View.VISIBLE
                }
            }

            binding.llToFollow.setOnClickListener(object:NoDoubleClickListener(){
                override fun onClick() {
                    if (isFollow == 0){
                        userVm.followUser(uid,0)
                    }else if (isFollow == 1){
                        userVm.followUser(uid,1)
                    }
                }
            })

            binding.llChat.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) {
                                    ARouter.getInstance().build(ARouteConstant.Home.chatDetail)
                                        .withString("account",account)
                                        .withString("chatName",binding.tvName.text.toString()).navigation()
                                } else {
                                    Toast.makeText(
                                        AppContext.getContext(),
                                        "These permissions are denied: $deniedList",
                                        Toast.LENGTH_LONG
                                    ).show()
                                }
                            }
                    }
                }
            })

            binding.ivGift.setOnClickListener(object:NoDoubleClickListener(){
                override fun onClick() {
                    openGiftDialog?.invoke(nickName,uid)
                    dismissAllowingStateLoss()
                }
            })

            binding.ivReport.setOnClickListener(object:NoDoubleClickListener(){
                override fun onClick() {
                    ReportDialog.newInstance(account).show(parentFragmentManager,"ReportDialog")
                }
            })

            binding.ivMore.setOnClickListener(object:NoDoubleClickListener(){
                override fun onClick() {
                    HandleDialogFragment.newInstance(uid,account,isForbid,isAnchor,inRoom).show(parentFragmentManager,"HandleDialogFragment")
                }
            })

        }
    }

    override fun initLiveData() {
        userVm.otherUserInfoLiveData.observe(this) {
            isFollow = it.is_follow
            isBlock = it.is_black
            nickName = it.user_name
            account = it.user_account
            Glide.with(this).load(it.user_head_img_url)
                .placeholder(R.mipmap.default_avatar)
                .error(R.mipmap.default_avatar).into(binding.ivAvatar)
            binding.tvName.text = it.user_name
            binding.tvFansNum.text = it.user_fans_num
            binding.tvFollowNum.text = it.user_follow_num
            binding.tvAccount.text =
                AppContext.getContext().resources.getString(R.string.id_x).format(it.user_account)
            binding.tvLevelView.setUserLevel(it.user_wealth_level.toInt())
            if (isFollow == 1) {
                binding.ivFollow.setImageResource(R.mipmap.followed)
            } else {
                binding.ivFollow.setImageResource(R.mipmap.icon_info_follow)
            }

            if (it.user_sex == 1) {
                binding.ivSex.setImageResource(R.mipmap.info_female)
            } else if (it.user_sex == 0) {
                binding.ivSex.setImageResource(R.mipmap.info_male)
            }

            if (it.user_broadcast_status == 1) {
                binding.ivOnLive.visibility = View.VISIBLE
            } else {
                binding.ivOnLive.visibility = View.GONE
            }
        }


        userVm.followStateLiveData.observe(this) {
            if (isFollow == 0) {
                isFollow = 1
                binding.ivFollow.setImageResource(R.mipmap.followed)
                Toast.makeText(AppContext.getContext(), "Follow success", Toast.LENGTH_SHORT).show()
            } else {
                isFollow = 0
                binding.ivFollow.setImageResource(R.mipmap.icon_info_follow)
                Toast.makeText(AppContext.getContext(), "Follow canceled", Toast.LENGTH_SHORT)
                    .show()

            }
        }

        userVm.hasBeenBlackLiveData.observe(this) {
            isBlock = 1
        }

        handleVm.roomAuthLiveData.observe(this){
            isForbid = it.is_forbid
        }

    }
}