package com.hanwha.morakmorak.ui.main.home

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.ViewDataBinding
import androidx.recyclerview.widget.RecyclerView
import com.hanwha.morakmorak.R
import com.hanwha.morakmorak.data.remote.model.Home
import com.hanwha.morakmorak.data.remote.model.HomeCategory.MEETING
import com.hanwha.morakmorak.data.remote.model.HomeCategory.TALKING
import com.hanwha.morakmorak.databinding.ItemHomeMeetingBinding
import com.hanwha.morakmorak.databinding.ItemHomeTalkingBinding
import com.hanwha.morakmorak.ui.BindingViewHolder
import timber.log.Timber

/**
 * Created by aaron on 09/09/2019
 */
class HomeAdapter(val vm: HomeViewModel, var items: List<Home>) : RecyclerView.Adapter<BindingViewHolder<ViewDataBinding>>() {

    override fun getItemCount() = items.size

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BindingViewHolder<ViewDataBinding> {
        Timber.i("HomeAdpater::onCreateViewHolder")
        return when (viewType) {
            MEETING.category -> HomeMeetingViewHolder(
                    LayoutInflater.from(parent.context).inflate(
                            R.layout.item_home_meeting,
                            parent,
                            false)
            )
            TALKING.category -> HomeTalkingViewHolder(
                    LayoutInflater.from(parent.context).inflate(
                            R.layout.item_home_talking,
                            parent,
                            false)
            )
            else -> HomeMeetingViewHolder(
                    LayoutInflater.from(parent.context).inflate(
                            R.layout.item_home_meeting,
                            parent,
                            false)
            )
        }
    }

    override fun getItemViewType(position: Int): Int {
        return items[position].category
    }

    override fun onBindViewHolder(holder: BindingViewHolder<ViewDataBinding>, position: Int) {
        Timber.i("HomeAdpater::onBindViewHolder")
        when (holder) {
            is HomeMeetingViewHolder -> {
                holder.binding.vm = vm
                holder.binding.item = items[position]
                holder.binding.itemHomeConstraintLayout.tag = items[position].id
                holder.binding.thumbImageView.visibility = View.GONE
            }
            is HomeTalkingViewHolder -> {
                holder.binding.vm = vm
                holder.binding.item = items[position]
                holder.binding.itemHomeConstraintLayout.tag = items[position].id
                holder.binding.thumbImageView.visibility = View.GONE
            }
        }
    }

    class HomeMeetingViewHolder(view: View) : BindingViewHolder<ItemHomeMeetingBinding>(view)

    class HomeTalkingViewHolder(view: View) : BindingViewHolder<ItemHomeTalkingBinding>(view)

//    companion object {
//        private val DIFF_CALLBACK = object : DiffUtil.ItemCallback<Home>() {
//            override fun areItemsTheSame(oldItem: Home, newItem: Home) = oldItem == newItem
//            override fun areContentsTheSame(oldItem: Home, newItem: Home) = oldItem == newItem
//        }
//    }

}