package com.gh.download.dialog

import android.graphics.Color
import android.webkit.WebView
import android.webkit.WebViewClient
import androidx.recyclerview.widget.RecyclerView
import com.gh.gamecenter.common.base.BaseRecyclerViewHolder
import com.gh.common.DefaultUrlHandler
import com.gh.gamecenter.common.utils.dip2px
import com.gh.gamecenter.common.utils.loadDataCompat
import com.gh.gamecenter.databinding.DownloadDialogInstructionItemBinding

class DownloadDialogInstructionItemViewHolder(val binding: DownloadDialogInstructionItemBinding) :
    BaseRecyclerViewHolder<Any>(binding.root) {

    fun bindItem(listData: List<DownloadDialogItemData>, position: Int, entrance: String) {
        val instruction = listData[position].instruction
        binding.webView.loadDataCompat(
            "<body style='margin:0;padding:0;color:#666666;font-size:12px;line-height:18px;'>$instruction</body>",
        )
        binding.webView.settings
        binding.webView.setBackgroundColor(Color.TRANSPARENT)
        binding.webView.webViewClient = object : WebViewClient() {
            override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
                return DefaultUrlHandler.interceptUrl(binding.root.context, url ?: "", entrance)
            }
        }

        val topOffset =
            if (position - 1 >= 0 && (listData[position - 1].normal?.url.isNullOrEmpty() || listData[position - 1].normal?.recommend != null)) 0 else 12f.dip2px()
        (binding.root.layoutParams as RecyclerView.LayoutParams).apply {
            topMargin = topOffset
            binding.root.layoutParams = this
        }
    }
}