package kr.co.vividnext.sodalive.live.reservation_status

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Intent
import android.graphics.Rect
import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
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.ActivityLiveReservationStatusBinding
import kr.co.vividnext.sodalive.extensions.dpToPx
import org.koin.android.ext.android.inject

class LiveReservationStatusActivity : BaseActivity<ActivityLiveReservationStatusBinding>(
    ActivityLiveReservationStatusBinding::inflate
) {
    private val viewModel: LiveReservationStatusViewModel by inject()

    private lateinit var loadingDialog: LoadingDialog
    private lateinit var adapter: LiveReservationStatusAdapter
    private lateinit var cancelActivityResultLauncher: ActivityResultLauncher<Intent>

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

        cancelActivityResultLauncher = registerForActivityResult(
            ActivityResultContracts.StartActivityForResult()
        ) {
            if (it.resultCode == Activity.RESULT_OK) {
                adapter.items.clear()
                viewModel.getLiveReservationStatus(isActive = true)
            }
        }

        bindData()
        viewModel.getLiveReservationStatus(isActive = true)
    }

    override fun setupView() {
        binding.toolbar.tvBack.text = "라이브 예약 현황"
        binding.toolbar.tvBack.setOnClickListener { finish() }
        loadingDialog = LoadingDialog(this, layoutInflater)

        val recyclerView = binding.recyclerView
        adapter = LiveReservationStatusAdapter {
            val intent = Intent(applicationContext, LiveReservationCancelActivity::class.java)
            intent.putExtra("reservation_id", it.reservationId)
            cancelActivityResultLauncher.launch(intent)
        }

        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()

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

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

                    else -> {
                        outRect.top = 6.7f.dpToPx().toInt()
                        outRect.bottom = 6.7f.dpToPx().toInt()
                    }
                }
            }
        })

        recyclerView.adapter = adapter
    }

    @SuppressLint("NotifyDataSetChanged")
    private fun bindData() {
        viewModel.liveReservationStatusLiveData.observe(this) {
            if (adapter.items.size > 0 || it.isNotEmpty()) {
                adapter.items.addAll(it)
                adapter.notifyDataSetChanged()

                binding.recyclerView.visibility = View.VISIBLE
                binding.tvNoItems.visibility = View.GONE
            } else {
                binding.recyclerView.visibility = View.GONE
                binding.tvNoItems.visibility = View.VISIBLE
            }
        }

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

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