package kr.co.vividnext.sodalive.message

import android.annotation.SuppressLint
import android.content.Intent
import android.graphics.Rect
import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.jakewharton.rxbinding4.widget.textChanges
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
import io.reactivex.rxjava3.schedulers.Schedulers
import kr.co.vividnext.sodalive.base.BaseActivity
import kr.co.vividnext.sodalive.common.Constants
import kr.co.vividnext.sodalive.databinding.ActivitySelectMessageRecipientBinding
import kr.co.vividnext.sodalive.extensions.dpToPx
import org.koin.android.ext.android.inject
import java.util.concurrent.TimeUnit

class SelectMessageRecipientActivity : BaseActivity<ActivitySelectMessageRecipientBinding>(
    ActivitySelectMessageRecipientBinding::inflate
) {

    private val viewModel: SelectMessageRecipientViewModel by inject()

    private lateinit var adapter: SelectMessageRecipientAdapter

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

        bindData()
        viewModel.searchUser("")
    }

    override fun setupView() {
        binding.toolbar.tvBack.text = "받는 사람 검색"
        binding.toolbar.tvBack.setOnClickListener { finish() }

        val recyclerView = binding.rvRecipient

        adapter = SelectMessageRecipientAdapter {
            val intent = Intent()
            intent.putExtra(Constants.EXTRA_SELECT_RECIPIENT, it)
            setResult(RESULT_OK, intent)
            finish()
        }

        recyclerView.layoutManager = LinearLayoutManager(
            applicationContext,
            LinearLayoutManager.VERTICAL,
            false
        )

        recyclerView.addItemDecoration(object : RecyclerView.ItemDecoration() {
            override fun getItemOffsets(
                outRect: Rect,
                view: View,
                parent: RecyclerView,
                state: RecyclerView.State
            ) {
                super.getItemOffsets(outRect, view, parent, state)

                outRect.left = 13.3f.dpToPx().toInt()
                outRect.right = 13.3f.dpToPx().toInt()
                outRect.top = 13.3f.dpToPx().toInt()
                outRect.bottom = 13.3f.dpToPx().toInt()
            }
        })

        recyclerView.adapter = adapter
    }

    @SuppressLint("NotifyDataSetChanged")
    private fun bindData() {
        compositeDisposable.add(
            binding.etSearchNickname.textChanges().skip(1)
                .debounce(500, TimeUnit.MILLISECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe {
                    viewModel.searchUser(it.toString())
                }
        )

        viewModel.searchUserLiveData.observe(this) {
            adapter.items.clear()
            adapter.items.addAll(it)
            adapter.notifyDataSetChanged()
        }
    }
}
