package net.pokeranalytics.android.model.realm

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.adapter.StaticRowRepresentableDataSource
import net.pokeranalytics.android.ui.view.RowRepresentable
import net.pokeranalytics.android.ui.view.RowRepresentableEditDescriptor
import net.pokeranalytics.android.ui.view.rowrepresentable.SimpleRow
import net.pokeranalytics.android.ui.view.rowrepresentable.TournamentNameRow
import net.pokeranalytics.android.util.NULL_TEXT
import java.util.*
import kotlin.collections.ArrayList


open class TournamentName : RealmObject(), Manageable, StaticRowRepresentableDataSource, RowRepresentable {
	companion object {
		val rowRepresentation : List<RowRepresentable> by lazy {
			val rows = ArrayList<RowRepresentable>()
			rows.add(SimpleRow.NAME)
			rows.addAll(TournamentNameRow.values())
			rows
		}
	}

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

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

	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 adapterRows(): List<RowRepresentable>? {
		return TournamentName.rowRepresentation
	}

	override fun stringForRow(row: RowRepresentable): String {
		return when (row) {
			SimpleRow.NAME -> if (this.name.isNotEmpty()) this.name else NULL_TEXT
			else -> return super.stringForRow(row)
		}
	}

	override fun editDescriptors(row: RowRepresentable): ArrayList<RowRepresentableEditDescriptor>? {
		return row.editingDescriptors(mapOf("defaultValue" to this.name))
	}

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

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

}
