package com.kaixed.kchat.ui.activity

import android.content.Intent
import android.os.Build
import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.annotation.RequiresApi
import com.bumptech.glide.Glide
import com.kaixed.kchat.databinding.ActivityApplyFriendsDetailBinding
import com.kaixed.kchat.model.friend.FriendRequestItem
import com.kaixed.kchat.model.search.User
import com.kaixed.kchat.ui.base.BaseActivity

class ApplyFriendsDetailActivity : BaseActivity<ActivityApplyFriendsDetailBinding>() {

    override fun inflateBinding(): ActivityApplyFriendsDetailBinding {
        return ActivityApplyFriendsDetailBinding.inflate(layoutInflater)
    }

    private var request: FriendRequestItem? = null

    private var user: User? = null

    @RequiresApi(Build.VERSION_CODES.TIRAMISU)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()

        user = intent.getParcelableExtra("user", User::class.java)
        request = intent.getParcelableExtra("request", FriendRequestItem::class.java)

        setContent()

        setOnClick()
    }

    private fun setContent() {
        // 使用 Elvis 操作符 ?:，如果 request 不为空则使用 request，否则使用 user
        val nickname = request?.nickname ?: user?.nickname
        val signature = request?.signature ?: user?.signature
        val avatarUrl = request?.avatarUrl ?: user?.avatarUrl

        // 设置到 UI 上
        binding.tvContactName.text = nickname
        binding.tvContactSignature.text = signature
        binding.tvSignatureContent.text = signature
        Glide.with(this).load(avatarUrl).into(binding.ifvAvatar)
    }

    private fun setOnClick() {
        binding.rlSendMessage.setOnClickListener {
            val contactId = request?.username ?: user?.username
            contactId?.let {
                val intent = Intent(this, ApplyAddFriendActivity::class.java)
                intent.putExtra("contactId", it)
                startActivity(intent)
            }
        }

        binding.ctl.setOnSettingClickListener {
            val intent = Intent(this, DataSettingActivity::class.java)
            startActivity(intent)
        }

        binding.rlSetRemark.setOnClickListener {
            val intent = Intent(this, SetRemarkActivity::class.java)
            startActivity(intent)
        }
    }
}
