package com.swago.home

import android.Manifest
import android.content.Intent
import android.util.Log
import android.view.View
import android.widget.Toast
import androidx.activity.viewModels
import androidx.fragment.app.activityViewModels
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.UserVm
import com.swago.baseswago.constant.ARouteConstant
import com.swago.baseswago.fragment.BaseXFragment
import com.swago.baseswago.util.AppContext
import com.swago.baseswago.util.NoDoubleClickListener
import com.swago.baseswago.util.UserInfo
import com.swago.home.databinding.FragmentMessageBinding
import com.swago.home.official.OfficialMessageActivity
import com.swago.home.official.OfficialVm
import com.tencent.imsdk.v2.V2TIMConversation
import com.tencent.qcloud.tim.uikit.modules.chat.base.ChatInfo

/**
 *@date 2021/8/21 12:43
 *description:
 */
class MessageFragment : BaseXFragment<FragmentMessageBinding>() {

    private val officialVm by activityViewModels<OfficialVm>()

    override fun loadData() {
        officialVm.getMsgSum()
    }

    override fun initOther() {
        binding.conversation.initDefault()
        binding.conversation.conversationList.setOnItemClickListener { view, position, messageInfo ->
            PermissionX.init(this)
                .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)
                            .withInt("type", V2TIMConversation.V2TIM_C2C)
                            .withString("account", messageInfo.id)
                            .withString("chatName", messageInfo.title).navigation()
                    } else {
                        Toast.makeText(
                            AppContext.getContext(),
                            "These permissions are denied: $deniedList",
                            Toast.LENGTH_LONG
                        ).show()
                    }
                }
        }

        binding.conversation.conversationList.setOnItemLongClickListener { view, position, messageInfo ->

        }


        binding.ivSetRead.setOnClickListener(object : NoDoubleClickListener() {
            override fun onClick() {

            }
        })

        binding.clOfficialMsg.setOnClickListener(object : NoDoubleClickListener() {
            override fun onClick() {
                binding.tvCount.visibility = View.GONE
                startActivity(Intent(activity, OfficialMessageActivity::class.java))
            }
        })

    }

    override fun initLiveData() {
        officialVm.officialSumLiveData.observe(this) {
            binding.tvContent.text = it.message_content
            binding.tvTime.text = it.message_time
            if (it.unread_total.toInt() == 0) {
                binding.tvCount.visibility = View.GONE
            } else {
                binding.tvCount.text = it.unread_total
                binding.tvCount.visibility = View.VISIBLE
            }
        }
    }
}