package net.pokeranalytics.android.ui.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import io.realm.RealmResults
import kotlinx.android.synthetic.main.row_history_session.view.*
import net.pokeranalytics.android.R
import net.pokeranalytics.android.model.realm.Session
import timber.log.Timber

class HistoryAdapter(private var sessions: RealmResults<Session>) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

	companion object {
		const val ROW_SESSION: Int = 100
		const val ROW_TOURNAMENT: Int = 101
		const val ROW_HAND: Int = 102
		const val ROW_TRANSACTION: Int = 103
	}

	var onClickOnSession: ((position: Int, session: Session) -> Unit)? = null

	inner class RowSessionViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

		fun bind(session: Session?) {
			session?.let {
				itemView.sessionRow.setData(session)
				itemView.sessionRow.setOnClickListener {
					onClickOnSession?.invoke(adapterPosition, session)
				}
			}
		}
	}

	override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
		when (viewType) {
			ROW_SESSION -> return RowSessionViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.row_history_session, parent, false))
			else -> throw IllegalStateException("Need to implement type $viewType in HistoryAdapter")
		}
	}

	override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
		when (getItemViewType(position)) {
			ROW_SESSION -> (holder as HistoryAdapter.RowSessionViewHolder).bind(sessions.get(position))
		}
	}

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

	override fun getItemViewType(position: Int): Int {
		return ROW_SESSION
	}


}