package com.kaixed.kchat.ui.activity

import android.annotation.SuppressLint
import android.app.TaskStackBuilder
import android.content.Intent
import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import com.kaixed.kchat.databinding.ActivityApproveContactRequestBinding
import com.kaixed.kchat.utils.ConstantsUtil.getUsername
import com.kaixed.kchat.viewmodel.ContactViewModel

class ApproveContactRequestActivity : BaseActivity() {

    private lateinit var binding: ActivityApproveContactRequestBinding
    private val contactViewModel: ContactViewModel by viewModels()

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

        setContent()

        setOnClickListener()

    }

    @SuppressLint("SetTextI18n")
    private fun setContent() {
        val nickname = intent.getStringExtra("nickname")
        val message = intent.getStringExtra("message")
        binding.etRemark.setText(nickname)
        binding.tvMessage.text = "$message"
    }

    private fun setOnClickListener() {
        binding.tvFinish.setOnClickListener {
            val contactId = intent.getStringExtra("contactId")
            contactViewModel.acceptContactRequest(getUsername(), contactId!!)
                .observe(this) { value ->
                    value?.let {
                        runOnUiThread {
                            toast(it.data)
                        }
                        val stackBuilder: TaskStackBuilder = TaskStackBuilder.create(this)
                        stackBuilder.addNextIntentWithParentStack(
                            Intent(
                                this,
                                MainActivity::class.java
                            )
                        )
                        stackBuilder.addNextIntent(
                            Intent(
                                this,
                                ChatActivity::class.java
                            ).putExtra("friendId", contactId)
                        )
                        stackBuilder.startActivities()
                    }

                }
        }
    }
}

