package kr.co.vividnext.sodalive.mypage.service_center

import android.annotation.SuppressLint
import android.content.Intent
import android.graphics.Rect
import android.net.Uri
import android.os.Bundle
import android.view.View
import android.widget.LinearLayout
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.databinding.ActivityServiceCenterBinding
import kr.co.vividnext.sodalive.extensions.dpToPx
import org.koin.android.ext.android.inject
import kotlin.math.ceil

class ServiceCenterActivity : BaseActivity<ActivityServiceCenterBinding>(
    ActivityServiceCenterBinding::inflate
) {

    private val viewModel: ServiceCenterViewModel by inject()

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

        viewModel.getFaqCategories()
    }

    @SuppressLint("NotifyDataSetChanged")
    override fun setupView() {
        binding.toolbar.tvBack.text = "고객센터"
        binding.toolbar.tvBack.setOnClickListener { finish() }

        val categoryAdapter = FaqCategoryAdapter(this)
        binding.gvFaqCategory.adapter = categoryAdapter
        binding.gvFaqCategory.setOnItemClickListener { _, _, position, _ ->
            viewModel.getFaqs(categoryAdapter.selectCategory(position))
        }

        binding.llQuestionKakao.setOnClickListener {
            startActivity(
                Intent(
                    Intent.ACTION_VIEW,
                    Uri.parse("http://pf.kakao.com/_lkxgxhG/chat")
                )
            )
        }

        val recyclerView = binding.rvFaq
        val faqAdapter = FaqAdapter()

        recyclerView.layoutManager = LinearLayoutManager(
            this,
            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)) {
                    faqAdapter.itemCount - 1 -> {
                        outRect.top = 0.dpToPx().toInt()
                        outRect.bottom = 40.dpToPx().toInt()
                    }

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

        recyclerView.adapter = faqAdapter

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

        viewModel.categoriesLiveData.observe(this) {
            categoryAdapter.items.addAll(it)

            val lp = binding.gvFaqCategory.layoutParams as LinearLayout.LayoutParams
            lp.height = (ceil(it.count().toFloat() / 4f) * 46.7f + 10f).dpToPx().toInt()

            viewModel.getFaqs(categoryAdapter.selectCategory(0))
        }

        viewModel.faqLiveData.observe(this) {
            faqAdapter.items.clear()
            faqAdapter.items.addAll(it)
            faqAdapter.notifyDataSetChanged()
        }
    }
}
