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.ActivityApproveDetailBinding
import com.kaixed.kchat.model.friend.FriendRequestItem
import com.kaixed.kchat.ui.base.BaseActivity

class ApproveDetailActivity : BaseActivity<ActivityApproveDetailBinding>() {

    private var request: FriendRequestItem? = null

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

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

        handleIntent(intent)

        setContent()

        setOnClick()
    }

    private fun handleIntent(intent: Intent) {
        request = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            intent.getParcelableExtra("request", FriendRequestItem::class.java)
        } else {
            @Suppress("DEPRECATION")
            intent.getParcelableExtra("request") as? FriendRequestItem
        }
    }

    private fun setContent() {
        if (request == null) {
            return
        }
        val nickname = request?.nickname
        val signature = request?.signature
        val avatarUrl = request?.avatarUrl

        binding.tvContactName.text = nickname
        binding.tvContactSignature.text = signature
        binding.tvSignatureContent.text = signature
        Glide.with(this).load(avatarUrl).into(binding.ifvAvatar)
    }

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

        binding.rlGotoVerify.setOnClickListener {
            startActivity(
                Intent(this, ApproveContactRequestActivity::class.java).apply {
                    putExtra("nickname", request?.nickname)
                    putExtra("message", request?.message)
                    putExtra("contactId", request?.username)
                }
            )
        }
    }
}
