package com.base.scanqr.ui.main

import android.content.Context
import android.view.View
import android.view.ViewGroup
import com.base.scanqr.R
import com.base.scanqr.ads.AdsMgr
import com.base.scanqr.bean.AdScanBean
import com.base.scanqr.bean.ContractUIBean
import com.base.scanqr.bean.EmailUIBean
import com.base.scanqr.bean.EventUIBean
import com.base.scanqr.bean.FunctionUIBean.Companion.KEY_CONTACT
import com.base.scanqr.bean.FunctionUIBean.Companion.KEY_EMAIL
import com.base.scanqr.bean.FunctionUIBean.Companion.KEY_EVENT
import com.base.scanqr.bean.FunctionUIBean.Companion.KEY_LOCATION
import com.base.scanqr.bean.FunctionUIBean.Companion.KEY_MESSAGE
import com.base.scanqr.bean.FunctionUIBean.Companion.KEY_PRODUCT
import com.base.scanqr.bean.FunctionUIBean.Companion.KEY_TELEPHONE
import com.base.scanqr.bean.FunctionUIBean.Companion.KEY_TEXT
import com.base.scanqr.bean.FunctionUIBean.Companion.KEY_WEBSITE
import com.base.scanqr.bean.FunctionUIBean.Companion.KEY_WIFI
import com.base.scanqr.bean.MessageUIBean
import com.base.scanqr.bean.ProductUIBean
import com.base.scanqr.bean.ScanBean
import com.base.scanqr.bean.TelephoneUIBean
import com.base.scanqr.bean.TextUIBean
import com.base.scanqr.bean.WebsiteUIBean
import com.base.scanqr.bean.WifiUIBean
import com.base.scanqr.databinding.ItemAdBinding
import com.base.scanqr.databinding.ItemHistoryBinding
import com.base.scanqr.ui.adapter.CommonViewHolder
import com.base.scanqr.utils.DateUtils.formatTimeAgo
import com.base.scanqr.utils.LogEx
import com.base.scanqr.utils.XmlEx.inflate
import com.chad.library.adapter4.BaseQuickAdapter
import com.google.android.gms.ads.nativead.NativeAd

class HistoryAdapter : BaseQuickAdapter<ScanBean, CommonViewHolder>() {

    private val TAG = "HistoryAdapter"
    var moreAction: ((view: View, item: ScanBean) -> Unit)? = null
    var itemClick: ((item: ScanBean) -> Unit)? = null

    override fun onBindViewHolder(holder: CommonViewHolder, position: Int, item: ScanBean?) {
        item ?: return
        if (item.scanType == "AD") {
            val binding = ItemAdBinding.bind(holder.itemView)
            if (item is AdScanBean) {
                if (item.ad == null) {
                    AdsMgr.showNative(binding.flAd, R.layout.layout_admob_item, nativeCallBack = { ad ->
                        if (ad is NativeAd) {
                            LogEx.logDebug(TAG, "ad is NativeAd")
                            item.ad = ad
                            binding.shimmerLayout.visibility = View.INVISIBLE
                        }
                    })
                } else {
                    item.ad?.let { binding.flAd.setNativeAd(it, R.layout.layout_admob_item) }
                }
            }
        } else {
            val binding = ItemHistoryBinding.bind(holder.itemView)
            LogEx.logDebug(TAG, "item.scanType=${item.scanType}")
            when (item.scanType) {
                KEY_WIFI -> {
                    if (item is WifiUIBean) {
                        binding.ivIcon.setImageResource(R.mipmap.h_wifi)
                    }
                }

                KEY_TEXT -> {
                    if (item is TextUIBean) {
                        binding.ivIcon.setImageResource(R.mipmap.h_text)
                    }
                }

                KEY_EMAIL -> {
                    if (item is EmailUIBean) {
                        binding.ivIcon.setImageResource(R.mipmap.h_email)
                    }
                }

                KEY_EVENT -> {
                    if (item is EventUIBean) {
                        binding.ivIcon.setImageResource(R.mipmap.h_event)
                    }
                }

                KEY_CONTACT -> {
                    if (item is ContractUIBean) {
                        binding.ivIcon.setImageResource(R.mipmap.h_contact)
                    }
                }

                KEY_WEBSITE -> {
                    if (item is WebsiteUIBean) {
                        binding.ivIcon.setImageResource(R.mipmap.h_website)
                    }
                }

                KEY_LOCATION -> {

                }

                KEY_TELEPHONE -> {
                    if (item is TelephoneUIBean) {
                        binding.ivIcon.setImageResource(R.mipmap.h_telephone)
                    }
                }

                KEY_MESSAGE -> {
                    if (item is MessageUIBean) {
                        binding.ivIcon.setImageResource(R.mipmap.h_message)
                    }
                }

                KEY_PRODUCT -> {
                    if (item is ProductUIBean) {
                        binding.ivIcon.setImageResource(R.mipmap.h_product)
                    }
                }


            }
            binding.tvDesc.text = item.listContent
            binding.tvTime.text = formatTimeAgo(item.createTime)
            binding.flMore.setOnClickListener {
                moreAction?.invoke(it, item)
            }
            binding.root.setOnClickListener {
                itemClick?.invoke(item)
            }
        }
    }

    override fun getItemViewType(position: Int, list: List<ScanBean>): Int {
        val item = list[position]
        return if (item.scanType == "AD") 0 else 1
    }

    override fun onCreateViewHolder(context: Context, parent: ViewGroup, viewType: Int): CommonViewHolder {
        val id = if (viewType == 0) R.layout.item_ad else R.layout.item_history
        return CommonViewHolder(id.inflate(parent))
    }
}