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.ext.click
import com.renrenliao.im.im.AlarmEntity
import com.renrenliao.im.utils.GlideUtils
import com.renrenliao.im.viewmodel.ProcessAddFriendViewModel
import kotlinx.android.synthetic.main.activity_friendinfo.*
import kotlinx.android.synthetic.main.activity_process_friendaddreq.*
import kotlinx.android.synthetic.main.activity_process_friendaddreq.iv_avatar
import kotlinx.android.synthetic.main.activity_process_friendaddreq.tool_bar
import org.koin.androidx.viewmodel.ext.android.getViewModel

/**
 **@ClassName ProcessAddFriendActivity
 **@Description
 **@Author gan
 **@Date 2022/6/20 15:42
 **/
class ProcessAddFriendActivity:BaseVMActivity<ProcessAddFriendViewModel>() {

    override fun initVM(): ProcessAddFriendViewModel = getViewModel()

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

    override fun initView() {
        val alarm = intent.getParcelableExtra<AlarmEntity>("alarm")

        tool_bar.setOnBackListener { onBackPressed() }

        tv_nickname.text = alarm.name
        tv_uid.text = "UID: ${alarm.tid}"
        tv_msg.text = alarm.msg
        GlideUtils.loadUserImage(iv_avatar,alarm.getAvatarUrl())

        //拒绝
        tv_reject.click {
            mViewModel.reject(alarm.gId.toString(),alarm.tid.toString(),alarm.fid.toString())
        }
        //同意
        tv_agree.click {
            val intent = Intent(this, AddFriendSureActivity::class.java)
            intent.putExtra("alarm", alarm)
            requestDataLauncher.launch(intent)
        }

    }

    private val requestDataLauncher =
        registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
            if (result.resultCode == RESULT_OK) {
                val data = result.data?.getStringExtra("data")
                if (data == "agree") {
                    finish()
                }
            }
        }

    override fun initData() {

    }

    override fun startObserve() {
        mViewModel.rejectLiveData.observe(this){
            if (it){
                finish()
            }
        }
    }
}