package com.kaixed.kchat.ui.activity

import android.os.Bundle
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import com.kaixed.kchat.databinding.ActivityApplyAddFriendBinding
import com.kaixed.kchat.viewmodel.ApplyFriendViewModel

class ApplyAddFriendActivity : BaseActivity() {
    private lateinit var binding: ActivityApplyAddFriendBinding

    private val applyAddFriendViewModel: ApplyFriendViewModel by viewModels()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        binding = ActivityApplyAddFriendBinding.inflate(layoutInflater)
        setContentView(binding.root)

        val contactId = intent.getStringExtra("contactId")

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

    private fun sendContactRequest(contactId: String?) {
        contactId?.let {
            applyAddFriendViewModel.addContact(contactId, binding.etMessage.text.toString())
                .observe(this) { value ->
                    runOnUiThread {
                        if (value?.code == "200") {
                            Toast.makeText(this, value.msg, Toast.LENGTH_SHORT).show()
                            finish()
                        }
                    }
                }
        }
    }
}
