package kr.co.vividnext.sodalive.mypage.can.charge

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.Constants
import kr.co.vividnext.sodalive.common.LoadingDialog
import kr.co.vividnext.sodalive.databinding.ActivityCanChargeBinding
import kr.co.vividnext.sodalive.extensions.dpToPx
import kr.co.vividnext.sodalive.mypage.can.payment.CanPaymentActivity
import org.koin.android.ext.android.inject

class CanChargeActivity : BaseActivity<ActivityCanChargeBinding>(
    ActivityCanChargeBinding::inflate
) {

    private val viewModel: CanChargeViewModel by inject()
    private var gotoPrevPage: Boolean = false

    private lateinit var adapter: CanChargeAdapter
    private lateinit var loadingDialog: LoadingDialog
    private lateinit var activityResultLauncher: ActivityResultLauncher<Intent>

    override fun onCreate(savedInstanceState: Bundle?) {
        activityResultLauncher = registerForActivityResult(
            ActivityResultContracts.StartActivityForResult()
        ) {
            if (it.resultCode == Activity.RESULT_OK) {
                finish()
            }
        }

        super.onCreate(savedInstanceState)
        bindData()

        viewModel.getCanCharges()
    }

    override fun setupView() {
        loadingDialog = LoadingDialog(this, layoutInflater)

        binding.toolbar.tvBack.text = "충전하기"
        binding.toolbar.tvBack.setOnClickListener { finish() }

        gotoPrevPage = intent.getBooleanExtra(
            Constants.EXTRA_GO_TO_PREV_PAGE,
            false
        )

        val recyclerView = binding.rvChargeCan
        adapter = CanChargeAdapter {
            val intent = Intent(applicationContext, CanPaymentActivity::class.java)
            intent.putExtra(Constants.EXTRA_CAN, it)
            intent.putExtra(Constants.EXTRA_GO_TO_PREV_PAGE, gotoPrevPage)
            activityResultLauncher.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 = 26.7f.dpToPx().toInt()
                    }

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

        recyclerView.adapter = adapter
    }

    @SuppressLint("NotifyDataSetChanged")
    private fun bindData() {
        viewModel.canChargeLiveData.observe(this) {
            adapter.items.addAll(it)
            adapter.notifyDataSetChanged()
        }

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