package kr.co.vividnext.sodalive.settings.notice

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.ActivityNoticeBinding
import kr.co.vividnext.sodalive.extensions.dpToPx
import org.koin.android.ext.android.inject

class NoticeActivity : BaseActivity<ActivityNoticeBinding>(ActivityNoticeBinding::inflate) {

    private val viewModel: NoticeViewModel by inject()

    private lateinit var loadingDialog: LoadingDialog

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

        viewModel.isLoading.observe(this) {
            if (it) {
                loadingDialog.show(screenWidth, "공지사항을 불러오고 있습니다.")
            } else {
                loadingDialog.dismiss()
            }
        }

        viewModel.toastLiveData.observe(this) {
            it?.let { Toast.makeText(applicationContext, it, Toast.LENGTH_LONG).show() }
        }

        viewModel.getNotices()
    }

    @SuppressLint("NotifyDataSetChanged")
    override fun setupView() {
        binding.toolbar.tvBack.text = "공지사항"
        binding.toolbar.tvBack.setOnClickListener { finish() }

        loadingDialog = LoadingDialog(this, layoutInflater)

        val adapter = NoticeAdapter {
            val intent = Intent(applicationContext, NoticeDetailActivity::class.java)
            intent.putExtra(Constants.EXTRA_NOTICE, it)
            startActivity(intent)
        }
        binding.rvNotice.layoutManager = LinearLayoutManager(this)
        binding.rvNotice.addItemDecoration(object : RecyclerView.ItemDecoration() {
            override fun getItemOffsets(
                outRect: Rect,
                view: View,
                parent: RecyclerView,
                state: RecyclerView.State
            ) {
                super.getItemOffsets(outRect, view, parent, state)

                when (parent.getChildAdapterPosition(view)) {
                    0 -> {
                        outRect.top = 13.3f.dpToPx().toInt()
                        outRect.bottom = 0
                    }

                    adapter.itemCount - 1 -> {
                        outRect.top = 0
                        outRect.bottom = 13.3f.dpToPx().toInt()
                    }

                    else -> {
                        outRect.top = 0
                        outRect.bottom = 0
                    }
                }

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

        binding.rvNotice.adapter = adapter

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

                val lastVisiblePosition = (recyclerView.layoutManager as LinearLayoutManager)
                    .findLastVisibleItemPosition()
                val itemTotalCount = adapter.itemCount - 1

                if (lastVisiblePosition == itemTotalCount) {
                    viewModel.getNotices()
                }
            }
        })

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