package net.pokeranalytics.android.model.realm

import com.google.android.libraries.places.api.model.Place
import io.realm.RealmObject
import io.realm.annotations.PrimaryKey
import net.pokeranalytics.android.R
import net.pokeranalytics.android.model.interfaces.Manageable
import net.pokeranalytics.android.ui.view.RowRepresentable
import net.pokeranalytics.android.ui.view.rowrepresentable.SimpleRow
import java.util.*


open class Location : RealmObject(), Manageable, RowRepresentable {

	@PrimaryKey
	override var id = UUID.randomUUID().toString()

	// The name of the location
	var name: String = ""

	// The readable address of the location
	var address: String = ""

	// the longitude of the location
	var longitude: Double? = null

	// the latitude of the location
	var latitude: Double? = null

	override fun getDisplayName(): String {
		return this.name
	}

	override fun updateValue(value: Any?, row: RowRepresentable) {
		when (row) {
			SimpleRow.NAME -> this.name = value as String? ?: ""
		}
	}

	override fun isValidForSave(): Boolean {
		return this.name.isNotEmpty()
	}

	override fun getFailedSaveMessage(): Int {
		return R.string.location_empty_field_error
	}

	/**
	 * Fill the location attributes with a place object
	 */
	fun setPlace(place: Place) {
		this.name = place.name ?: ""
		this.address = place.address ?: ""
		this.latitude = place.latLng?.latitude
		this.longitude = place.latLng?.longitude
	}

}
