package kr.co.vividnext.sodalive.mypage.block

import android.annotation.SuppressLint
import android.graphics.Rect
import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import kr.co.vividnext.sodalive.base.BaseActivity
import kr.co.vividnext.sodalive.common.LoadingDialog
import kr.co.vividnext.sodalive.databinding.ActivityBlockMemberBinding
import kr.co.vividnext.sodalive.extensions.dpToPx
import org.koin.android.ext.android.inject

class BlockMemberActivity : BaseActivity<ActivityBlockMemberBinding>(
    ActivityBlockMemberBinding::inflate
) {
    private val viewModel: BlockMemberViewModel by inject()

    private lateinit var loadingDialog: LoadingDialog
    private lateinit var adapter: BlockMemberAdapter

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

        bindData()
        viewModel.getBlockedMemberList()
    }

    override fun setupView() {
        loadingDialog = LoadingDialog(this, layoutInflater)
        binding.toolbar.tvBack.text = "차단 리스트"
        binding.toolbar.tvBack.setOnClickListener { finish() }

        adapter = BlockMemberAdapter(
            blockMember = { viewModel.blockMember(it) },
            unBlockMember = { viewModel.unBlockMember(it) }
        )

        binding.rvBlockMember.layoutManager = LinearLayoutManager(
            applicationContext,
            LinearLayoutManager.VERTICAL,
            false
        )

        binding.rvBlockMember.addOnScrollListener(object : RecyclerView.OnScrollListener() {
            override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                super.onScrolled(recyclerView, dx, dy)

                val layoutManager = recyclerView.layoutManager as? LinearLayoutManager
                if (
                    layoutManager != null &&
                    layoutManager.findLastCompletelyVisibleItemPosition() == adapter.itemCount - 1
                ) {
                    viewModel.getBlockedMemberList()
                }
            }
        })

        binding.rvBlockMember.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()
            }
        })

        binding.rvBlockMember.adapter = adapter
    }

    @SuppressLint("SetTextI18n")
    private fun bindData() {
        viewModel.toastLiveData.observe(this) {
            it?.let { Toast.makeText(applicationContext, it, Toast.LENGTH_LONG).show() }
        }

        viewModel.isLoading.observe(this) {
            if (it) {
                loadingDialog.show(screenWidth, "")
            } else {
                loadingDialog.dismiss()
            }
        }

        viewModel.blockedMemberListLiveData.observe(this) {
            adapter.addAll(it)
        }

        viewModel.blockedMemberTotalCountLiveData.observe(this) {
            binding.tvTotalCount.text = "  $it "

            if (it > 0) {
                binding.tvNone.visibility = View.GONE
                binding.rvBlockMember.visibility = View.VISIBLE
            } else {
                binding.tvNone.visibility = View.VISIBLE
                binding.rvBlockMember.visibility = View.GONE
            }
        }
    }
}
