package com.yidian.shenghuoquan.newscontent.adapter

import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.yidian.common.cardutils.adapter.OnItemClickListener
import com.yidian.common.utils.ViewHolderUtil.Companion.createItemView
import com.yidian.shenghuoquan.newscontent.R
import com.yidian.shenghuoquan.newscontent.bean.SharePlatformBean
import kotlinx.android.extensions.LayoutContainer
import kotlinx.android.synthetic.main.item_share_platform.*

class ShareAdapter: RecyclerView.Adapter<RecyclerView.ViewHolder>() {
    companion object{
        const val normalType = 0
        const val headType = -1
        const val footType = -2
    }
    private val dataList: ArrayList<SharePlatformBean> by lazy { ArrayList<SharePlatformBean>() }
    private lateinit var mOnItemClickListener: OnItemClickListener<SharePlatformBean>

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

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return when(viewType){
            headType -> ViewHolder(createItemView(parent, R.layout.item_share_card))
            footType -> ViewHolder(createItemView(parent, R.layout.item_share_link))
            else -> ViewHolder(createItemView(parent, R.layout.item_share_platform))
        }
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        holder as ViewHolder
        holder.setData(dataList[position])
        holder.itemView.setOnClickListener{
            mOnItemClickListener.onClick(it, position, dataList[position])
        }
    }

    fun update(newData: ArrayList<SharePlatformBean>){
        newData.let {
            dataList.clear()
            dataList.addAll(it)
            notifyDataSetChanged()
        }
    }

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

    fun setOnItemClickListener(onItemClickListener: OnItemClickListener<SharePlatformBean>) {
        mOnItemClickListener = onItemClickListener
    }

    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView), LayoutContainer {
        override val containerView: View
            get() = itemView

        fun setData(sharePlatformBean: SharePlatformBean) {
            iv_platform.setBackgroundResource(sharePlatformBean.imageId)
            tv_platform.text = sharePlatformBean.name
        }
    }
}
