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

import android.annotation.SuppressLint
import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.webkit.CookieManager
import android.webkit.WebSettings
import androidx.core.view.isVisible
import androidx.recyclerview.widget.RecyclerView
import androidx.webkit.WebSettingsCompat
import androidx.webkit.WebViewFeature
import kr.co.vividnext.sodalive.R
import kr.co.vividnext.sodalive.databinding.ItemFaqBinding

class FaqAdapter : RecyclerView.Adapter<FaqAdapter.ViewHolder>() {

    val items = mutableListOf<Faq>()

    @SuppressLint("SetJavaScriptEnabled")
    inner class ViewHolder(
        private val binding: ItemFaqBinding
    ) : RecyclerView.ViewHolder(binding.root) {
        init {
            if (WebViewFeature.isFeatureSupported(WebViewFeature.FORCE_DARK)) {
                WebSettingsCompat.setForceDark(
                    binding.wvAnswer.settings,
                    WebSettingsCompat.FORCE_DARK_ON
                )
            }

            binding.wvAnswer.apply {
                setLayerType(View.LAYER_TYPE_HARDWARE, null)

                clearCache(true)
                settings.apply {
                    javaScriptEnabled = true
                    javaScriptCanOpenWindowsAutomatically = true
                    mixedContentMode = WebSettings.MIXED_CONTENT_ALWAYS_ALLOW
                    val cookieManager = CookieManager.getInstance()
                    cookieManager.setAcceptCookie(true)
                    cookieManager.setAcceptThirdPartyCookies(binding.wvAnswer, true)
                    cacheMode = WebSettings.LOAD_NO_CACHE
                    blockNetworkImage = false
                    loadsImagesAutomatically = true
                    useWideViewPort = false
                    loadWithOverviewMode = true
                    javaScriptCanOpenWindowsAutomatically = true

                    domStorageEnabled = true
                    loadWithOverviewMode = true
                    allowContentAccess = true

                    setSupportZoom(false)
                    displayZoomControls = false
                }
            }
        }

        fun bind(item: Faq) {
            binding.tvQuestion.text = item.question
            binding.wvAnswer.loadData(item.answer, "text/html", "UTF-8")
            binding.wvAnswer.setBackgroundColor(Color.BLACK)
            binding.root.setOnClickListener {
                binding.llAnswer.isVisible = !binding.llAnswer.isVisible
                binding.ivDropdown.setImageResource(
                    if (binding.llAnswer.isVisible) {
                        R.drawable.btn_dropdown_up
                    } else {
                        R.drawable.btn_dropdown_down
                    }
                )
            }
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        return ViewHolder(
            ItemFaqBinding.inflate(
                LayoutInflater.from(parent.context),
                parent,
                false
            )
        )
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.bind(items[position])
    }

    override fun getItemCount() = items.count()
}
