package net.pokeranalytics.android.ui.view

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import kotlinx.android.synthetic.main.row_title_value_action.view.*
import net.pokeranalytics.android.R
import net.pokeranalytics.android.ui.adapter.components.RowRepresentableDataSource

/**
 * An interface used to factor the configuration of RecyclerView.ViewHolder
 */
interface BindableHolder {

	fun bind(row: RowRepresentable, rowRepresentableDataSource: RowRepresentableDataSource? = null, listener: View.OnClickListener, actionListener: View.OnClickListener? = null) {}

}

enum class RowViewType {
	HEADER,
	EDIT_TEXT,
	TITLE,
	TITLE_VALUE,
	TITLE_VALUE_ACTION;

	inner class FakeViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView),
		BindableHolder {
		override fun bind(row: RowRepresentable, rowRepresentableDataSource: RowRepresentableDataSource?, listener: View.OnClickListener, actionListener: View.OnClickListener?) {
		}
	}

	inner class TitleViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView),
		BindableHolder {
		override fun bind(row: RowRepresentable, rowRepresentableDataSource: RowRepresentableDataSource?, listener: View.OnClickListener, actionListener: View.OnClickListener?) {
			itemView.title.text = row.localizedTitle(itemView.context)
			itemView.container.setOnClickListener(listener)
		}
	}

	inner class TitleValueViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView),
		BindableHolder {
		override fun bind(row: RowRepresentable, rowRepresentableDataSource: RowRepresentableDataSource?, listener: View.OnClickListener, actionListener: View.OnClickListener?) {
			itemView.title.text = row.localizedTitle(itemView.context)

			rowRepresentableDataSource?.let {
				itemView.value.text = it.stringForRow(row)
			}
			itemView.container.setOnClickListener(listener)
		}
	}

	inner class TitleValueActionViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView),
		BindableHolder {
		override fun bind(row: RowRepresentable, rowRepresentableDataSource: RowRepresentableDataSource?, listener: View.OnClickListener, actionListener: View.OnClickListener?) {
			itemView.title.text = row.localizedTitle(itemView.context)
			rowRepresentableDataSource?.let { rowDelegate ->

				val value = rowDelegate.stringForRow(row)
				itemView.value.text = value
				itemView.action.visibility = if (value == "--") View.GONE else View.VISIBLE
				rowDelegate.actionIconForRow(row)?.let {icon ->
					itemView.action.setImageResource(icon)
				}
			}
			itemView.container.setOnClickListener(listener)
			itemView.action.setOnClickListener(actionListener)
		}
	}


	fun viewHolder(parent: ViewGroup): RecyclerView.ViewHolder {
		return when (this) {
			TITLE -> TitleViewHolder(
				LayoutInflater.from(parent.context).inflate(
					R.layout.row_title,
					parent,
					false
				)
			)
			TITLE_VALUE -> TitleValueViewHolder(
				LayoutInflater.from(parent.context).inflate(
					R.layout.row_title_value,
					parent,
					false
				)
			)
			TITLE_VALUE_ACTION -> TitleValueActionViewHolder(
				LayoutInflater.from(parent.context).inflate(
					R.layout.row_title_value_action,
					parent,
					false
				)
			)

			else -> FakeViewHolder(parent)
		}
	}

}