package xyz.apiote.bimba.czwek.search

import android.annotation.SuppressLint
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.mancj.materialsearchbar.adapter.SuggestionsAdapter
import xyz.apiote.bimba.czwek.R
import xyz.apiote.bimba.czwek.api.Line
import xyz.apiote.bimba.czwek.api.QueryableV1
import xyz.apiote.bimba.czwek.api.StopV1

class BimbaViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
	val root: View = itemView.findViewById(R.id.suggestion)
	val icon: ImageView = itemView.findViewById(R.id.suggestion_image)
	val title: TextView = itemView.findViewById(R.id.suggestion_title)
	val description: TextView = itemView.findViewById(R.id.suggestion_description)

	companion object {
		fun bind(
			queryable: QueryableV1,
			holder: BimbaViewHolder?,
			context: Context?,
			onClickListener: (QueryableV1) -> Unit
		) {
			when (queryable) {
				is StopV1 -> bindStop(queryable, holder, context)
				//is Line -> bindLine(queryable, holder, context)
			}
			holder?.root?.setOnClickListener {
				onClickListener(queryable)
			}
		}

		private fun bindStop(stop: StopV1, holder: BimbaViewHolder?, context: Context?) {
			holder?.icon?.apply {
				setImageDrawable(stop.icon(context!!))
				contentDescription = context.getString(R.string.stop_content_description)
			}
			holder?.title?.text = context?.getString(R.string.stop_title, stop.name, stop.code)
			context?.let {
				stop.changeOptions(it).let { changeOptions ->
					holder?.description?.apply {
						text = changeOptions.first
						contentDescription = changeOptions.second
					}
				}
			}
		}

		private fun bindLine(line: Line, holder: BimbaViewHolder?, context: Context?) {
			holder?.icon?.apply {
				setImageBitmap(line.icon(context!!))
				contentDescription = line.type.name
				colorFilter = null
			}
			holder?.title?.text = line.name
			holder?.description?.text = context?.getString(
				R.string.line_headsigns,
				line.headsignsThere.joinToString { it },
				line.headsignsBack.joinToString { it })
			holder?.description?.contentDescription = context?.getString(
				R.string.line_headsigns_content_description,
				line.headsignsThere.joinToString { it },
				line.headsignsBack.joinToString { it })
		}
	}
}

interface Adapter {
	fun createViewHolder(
		inflater: LayoutInflater,
		layout: Int,
		parent: ViewGroup
	): BimbaViewHolder {
		val rowView = inflater.inflate(layout, parent, false)
		return BimbaViewHolder(rowView)
	}

	fun bindSuggestionHolder(
		queryable: QueryableV1,
		holder: BimbaViewHolder?,
		context: Context?,
		onClickListener: (QueryableV1) -> Unit
	) {
		BimbaViewHolder.bind(queryable, holder, context, onClickListener)
	}
}

class BimbaResultsAdapter(
	private val inflater: LayoutInflater,
	private val context: Context?,
	private var queryables: List<QueryableV1>,
	private val onClickListener: ((QueryableV1) -> Unit)
) :
	RecyclerView.Adapter<BimbaViewHolder>(), Adapter {
	override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BimbaViewHolder {
		return createViewHolder(inflater, R.layout.result, parent)
	}

	override fun onBindViewHolder(holder: BimbaViewHolder, position: Int) {
		bindSuggestionHolder(queryables[position], holder, context, onClickListener)
	}

	override fun getItemCount(): Int = queryables.size

	@SuppressLint("NotifyDataSetChanged") // todo [3.1] DiffUtil
	fun update(queryables: List<QueryableV1>) {
		this.queryables = queryables
		notifyDataSetChanged()
	}
}

class BimbaSuggestionsAdapter(
	inflater: LayoutInflater,
	private val context: Context?,
	private val onClickListener: ((QueryableV1) -> Unit)
) :
	SuggestionsAdapter<QueryableV1, BimbaViewHolder>(inflater), Adapter {
	override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BimbaViewHolder {
		return createViewHolder(layoutInflater, R.layout.suggestion, parent)
	}

	override fun getSingleViewHeight(): Int = 72

	override fun onBindSuggestionHolder(queryable: QueryableV1, holder: BimbaViewHolder?, pos: Int) {
		bindSuggestionHolder(queryable, holder, context, onClickListener)
	}
}