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.bean.EmailUIBean
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.ProductUIBean
import com.base.scanqr.bean.ScanBean
import com.base.scanqr.bean.TextUIBean
import com.base.scanqr.bean.WebsiteUIBean
import com.base.scanqr.bean.WifiUIBean
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

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
        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 -> {

            }

            KEY_CONTACT -> {

            }

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

            KEY_LOCATION -> {

            }

            KEY_TELEPHONE -> {

            }

            KEY_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 onCreateViewHolder(context: Context, parent: ViewGroup, viewType: Int): CommonViewHolder {
        return CommonViewHolder(R.layout.item_history.inflate(parent))
    }
}