package com.convenient.android.lib.ui.sample.ad.adapter

import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.convenient.android.common.base.viewbinding.BindingViewHolder
import com.convenient.android.common.utils.date.DateUtils
import com.convenient.android.common.utils.date.toDate
import com.convenient.android.lib.R
import com.convenient.android.lib.databinding.ViewRvAdNativeItemBinding
import com.convenient.android.lib.databinding.ViewRvAdNativeTemplateItemBinding
import com.convenient.android.lib.databinding.ViewRvMediaSampleItemBinding
import com.convenient.android.lib.ui.sample.ad.model.NativeListBean

/**
 * @classname:
 * @author: LiuXiaoLong
 * @date: 2022/9/8
 * description:
 */
class NativeAdSampleListAdapter() : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    companion object{
        const val ITEM_NORMAL = 0

        const val ITEM_NATIVE_AD = 1

        const val ITEM_NATIVE_TEMPLATE_AD = 2
    }

    private var lists : MutableList<NativeListBean> = mutableListOf()

    fun setData(list : MutableList<NativeListBean>){
        this.lists = list
        notifyDataSetChanged()
    }

    override fun getItemViewType(position: Int): Int {
        return lists[position].viewType
    }


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return when(viewType){
            ITEM_NORMAL-> BindingViewHolder(parent, ViewRvMediaSampleItemBinding::inflate)
            ITEM_NATIVE_AD->  BindingViewHolder(parent, ViewRvAdNativeItemBinding::inflate)
            else-> BindingViewHolder(parent, ViewRvAdNativeTemplateItemBinding::inflate)
        }
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        when(lists[position].viewType){
            ITEM_NORMAL->{
                val normalHolder = holder as BindingViewHolder<ViewRvMediaSampleItemBinding>
                val item = lists[position]
                item.media?.let {
                    normalHolder.binding.ivMediaItemIcon.setImageResource(if (item.media.isFile) R.drawable.ic_icons8_file else R.drawable.ic_icons8_folder)
                    normalHolder.binding.let {
                        it.tvMediaItemTitle.text = item.media.name
                        it.tvMediaItemDate.text = item.media.lastModified.toDate(DateUtils.DateFormat.DEFAULT_PATTERN.dateFormat)
                    }
                }
            }
            ITEM_NATIVE_AD->{
                (holder as BindingViewHolder<ViewRvAdNativeItemBinding>)
                    .binding.nativeAdView.populateNativeAd(lists[position].success!!)
            }
            else->{
                (holder as BindingViewHolder<ViewRvAdNativeTemplateItemBinding>)
                    .binding.nativeAdView.populateNativeAd(lists[position].success!!)
            }
        }
    }

    override fun getItemCount(): Int {
        return lists.size
    }



}