package com.kaixed.kchat.ui.activity

import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import com.kaixed.kchat.databinding.ActivityApplyAddFriendBinding
import com.kaixed.kchat.ui.base.BaseActivity
import com.kaixed.kchat.viewmodel.ContactViewModel

class ApplyAddFriendActivity : BaseActivity<ActivityApplyAddFriendBinding>() {

    private val contactViewModel: ContactViewModel by viewModels()

    override fun inflateBinding(): ActivityApplyAddFriendBinding =
        ActivityApplyAddFriendBinding.inflate(layoutInflater)


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()

        val contactId = intent.getStringExtra("contactId")

        binding.tvSendApply.setOnClickListener {
            sendContactRequest(contactId)
        }
    }

    private fun sendContactRequest(contactId: String?) {
        contactId?.let {
            contactViewModel.addContactResult
                .observe(this) { result ->
                    result.onSuccess {
                        toast(result.getOrNull().toString())
                        finish()
                    }
                    result.onFailure { toast(it.message.toString()) }
                }
            contactViewModel.addContact(contactId, binding.etMessage.text.toString())
        }
    }
}
