package kr.co.vividnext.sodalive.following

import android.annotation.SuppressLint
import android.content.Intent
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.Constants
import kr.co.vividnext.sodalive.common.LoadingDialog
import kr.co.vividnext.sodalive.databinding.ActivityFollowingCreatorBinding
import kr.co.vividnext.sodalive.explorer.profile.CreatorFollowNotifyFragment
import kr.co.vividnext.sodalive.explorer.profile.UserProfileActivity
import kr.co.vividnext.sodalive.extensions.dpToPx
import org.koin.android.ext.android.inject

class FollowingCreatorActivity : BaseActivity<ActivityFollowingCreatorBinding>(
    ActivityFollowingCreatorBinding::inflate
) {
    private val viewModel: FollowingCreatorViewModel by inject()

    private lateinit var loadingDialog: LoadingDialog
    private lateinit var adapter: FollowingCreatorAdapter

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

        bindData()
        viewModel.getFollowedCreatorAllList()
    }

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

        adapter = FollowingCreatorAdapter(
            onClickItem = { creatorId ->
                startActivity(
                    Intent(applicationContext, UserProfileActivity::class.java).apply {
                        putExtra(Constants.EXTRA_USER_ID, creatorId)
                    }
                )
            },
            onClickFollow = { creatorId, isFollow ->
                if (isFollow) {
                    val notifyFragment = CreatorFollowNotifyFragment(
                        onClickNotifyAll = {
                            viewModel.follow(creatorId, follow = true, notify = true)
                        },
                        onClickNotifyNone = {
                            viewModel.follow(creatorId, follow = true, notify = false)
                        },
                        onClickUnFollow = {
                            viewModel.follow(creatorId, follow = false, notify = false)
                        }
                    )

                    if (notifyFragment.isAdded) return@FollowingCreatorAdapter
                    notifyFragment.show(supportFragmentManager, notifyFragment.tag)
                } else {
                    viewModel.follow(creatorId)
                }
            },
        )

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

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

        binding.rvFollowingCreator.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.rvFollowingCreator.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.creatorListLiveData.observe(this) {
            if (viewModel.page == 2) adapter.clear()
            adapter.addAll(it)
        }

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

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