package com.renrenliao.im.ui.activity

import android.content.Intent
import androidx.activity.result.contract.ActivityResultContracts
import com.renrenliao.im.R
import com.renrenliao.im.base.BaseVMActivity
import com.renrenliao.im.bean.UserInfo
import com.renrenliao.im.ext.*
import com.renrenliao.im.utils.DataCacheUtils
import com.renrenliao.im.utils.GlideUtils
import com.renrenliao.im.viewmodel.FriendInfoViewModel
import com.renrenliao.im.widget.dialog.ExplainDialog
import kotlinx.android.synthetic.main.activity_friendinfo.*
import org.koin.androidx.viewmodel.ext.android.getViewModel

/**
 **@ClassName FriendInfoActivity
 **@Description
 **@Author gan
 **@Date 2022/6/13 18:03
 **/
class FriendInfoActivity : BaseVMActivity<FriendInfoViewModel>() {

    var friendInfo: UserInfo? = null

    override fun initVM(): FriendInfoViewModel = getViewModel()

    override fun getLayoutResId(): Int = R.layout.activity_friendinfo

    override fun initView() {

        tool_bar.setOnBackListener { onBackPressed() }

        friendInfo = intent.getParcelableExtra<UserInfo>("userinfo")

        if (friendInfo != null) {
            showInfo(friendInfo!!)
        }


        //删除好友
        rl_delete.click {
            ExplainDialog.Builder()
                .setTitle("删除联系人")
                .setDesc("同时删除与该联系人的聊天记录")
                .setNo("删除")
                .setNoListener {
                    mViewModel.deleteFriend(friendInfo?.userUid.toString())
                }
                .create()
                .show()
        }
        //添加好友
        rl_add.click {
            if (friendInfo?.userUid == DataCacheUtils.getUid()) {
                toast("不能添加自己哦")
                return@click
            }
            //RouteUtils.gotoFriendAdd(this,userInfo)
            val intent = Intent(this, FriendAddActivity::class.java)
            intent.putExtra("userinfo", friendInfo)
            requestDataLauncher.launch(intent)
        }
        //修改备注
        rl_remark.click {
            val intent = Intent(this, FriendRemarkActivity::class.java)
            intent.putExtra("friendId", friendInfo?.userUid)
            intent.putExtra("friendRemark", friendInfo?.buddyRemark)
            requestDataLauncher.launch(intent)
        }
    }

    override fun initData() {
        //判断是否是好友
        mViewModel.isFriend(friendInfo?.userUid.toString())
    }

//    override fun onResume() {
//        super.onResume()
//    }

    override fun startObserve() {
        mViewModel.run {
            //判断是否是好友展示不一样的视图
            isFriendLiveData.observe(this@FriendInfoActivity) {
                if (it) {
                    rl_delete.visible()
                    rl_add.gone()
                    tv_flag.gone()
                } else {
                    rl_delete.gone()
                    rl_add.visible()
                    tv_flag.visible()
                }
            }
            //删除成功退出activity
            deleteLiveData.observe(this@FriendInfoActivity) {
                if (it) {
                    finish()
                }
            }
        }
    }

    private val requestDataLauncher =
        registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
            if (result.resultCode == RESULT_OK) {
                val data = result.data?.getStringExtra("data")
                // Handle data from FriendAddActivity
                if (data == "add") {
                    finish()
                }
                if (data == "remark") {
                    val buddyRemark = result.data?.getStringExtra("buddyRemark")
                    tv_mark.text = buddyRemark
                }
            }
        }

    private fun showInfo(userInfo: UserInfo) {
        tv_name.text = userInfo.nickname
        tv_whatsup.text = userInfo.whatsUp
        if (userInfo.liveStatus == 1) {
            tv_status.text = "在线"
        } else {
            tv_status.text = "离线"
        }
        tv_mark.text = userInfo.buddyRemark
        tv_ID.text = userInfo.chatid
        tv_regtime.text = userInfo.registerTime
        tv_lastlogin.text = userInfo.latestLoginTime
        switch_button.isChecked = DataCacheUtils.getMsgSwitch(userInfo.userUid)
        //显示头像
        GlideUtils.loadUserImage(iv_avatar, userInfo.userAvatarFileName.toString())
        if (isMan(userInfo)) {
            iv_sex.setImageResource(R.drawable.sns_friend_list_form_item_male_img)
        } else {
            iv_sex.setImageResource(R.drawable.sns_friend_list_form_item_female_img)
        }
        //管理账号 1
        if (userInfo.userType == 1) {
            iv_cert.visible()
        } else {
            iv_cert.gone()
        }
    }
}