package com.kaixed.kchat.ui.activity

import android.content.Intent
import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import androidx.localbroadcastmanager.content.LocalBroadcastManager
import com.kaixed.kchat.data.objectbox.ObjectBox.get
import com.kaixed.kchat.data.objectbox.entity.Contact
import com.kaixed.kchat.databinding.ActivityDataSettingBinding
import com.kaixed.kchat.ui.base.BaseActivity
import com.kaixed.kchat.ui.fragment.DeleteContactDialogFragment
import com.kaixed.kchat.ui.fragment.LoadingDialogFragment
import com.kaixed.kchat.utils.ConstantsUtil.getUsername
import com.kaixed.kchat.viewmodel.ContactViewModel
import io.objectbox.Box

class DataSettingActivity : BaseActivity<ActivityDataSettingBinding>(),
    DeleteContactDialogFragment.OnContactDeletedListener {

    private val contactViewModel by viewModels<ContactViewModel>()

    private val contactBox: Box<Contact> by lazy { get().boxFor(Contact::class.java) }

    private var contactId = ""

    private val loadingDialogFragment by lazy { LoadingDialogFragment.newInstance("加载中...") }

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

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

        setOnClick()
        handleIntent(intent)
    }

    private fun handleIntent(intent: Intent) {
        contactId = intent.getStringExtra("contactId") ?: ""
    }

    private fun setOnClick() {
        binding.ciSetRemarkAndLabel.setOnClickListener {
            val intent = Intent(this, SetRemarkActivity::class.java)
            startActivity(intent)
        }

        binding.tvDelete.setOnClickListener {
            val deleteDialogFragment = DeleteContactDialogFragment.newInstance(contactId)
            deleteDialogFragment.show(supportFragmentManager, "DeleteContactDialogFragment")
        }
    }

    override fun onContactDeleted() {
        deleteContact(getUsername(), contactId)
    }

    private fun deleteContact(username: String, contactId: String) {
        loadingDialogFragment.show(supportFragmentManager, "LoadingDialogFragment")

        contactViewModel.deleteContactResult.observe(this) { result ->
            result.onSuccess {
                loadingDialogFragment.dismiss()

                toast("删除成功")

                val deleteIntent = Intent("com.kaixed.kchat.broadcast.UPDATE_CONTACT_LIST")
                deleteIntent.putExtra("update", true)
                LocalBroadcastManager.getInstance(this).sendBroadcast(deleteIntent)

                val intent = Intent(this, MainActivity::class.java).apply {
                    setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP)
                }
                startActivity(intent)
            }

            result.onFailure { toast(it.message.toString()) }
        }
        contactViewModel.deleteContact(username, contactId)
    }
}
