package net.pokeranalytics.android.ui.helpers

import androidx.appcompat.app.AlertDialog
import net.pokeranalytics.android.ui.activity.components.PokerAnalyticsActivity
import net.pokeranalytics.android.ui.adapter.RowRepresentableDelegate
import net.pokeranalytics.android.ui.view.RowRepresentable

/**
 * Display a dialog with a list of places around the user
 */
class PlacePickerManager {

	companion object {

		fun create(
			activity: PokerAnalyticsActivity,
			row: RowRepresentable,
			delegate: RowRepresentableDelegate,
			maxResults: Int? = 3
		) {
			activity.askForPlacesRequest { success, places ->
				if (success && places.size > 0) {
					val placesArray = ArrayList<CharSequence>()
					for ((index, place) in places.withIndex()) {
						placesArray.add(place.place.name.toString())
						if (index == (maxResults ?: 3) - 1) {
							break
						}
					}
					val builder = AlertDialog.Builder(activity)
					builder.setItems(placesArray.toTypedArray()) { _, which ->
						delegate.onRowValueChanged(places[which].place, row)
					}.setOnCancelListener {
						delegate.onRowValueChanged(null, row)
					}
					builder.show()
				} else {
					delegate.onRowValueChanged(null, row)
				}
			}
		}
	}

}