package net.pokeranalytics.android.model.realm

import io.realm.RealmObject
import io.realm.annotations.PrimaryKey
import net.pokeranalytics.android.model.interfaces.CountableUsage
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.TournamentFeatureRow
import net.pokeranalytics.android.util.NULL_TEXT
import java.util.*
import kotlin.collections.ArrayList

open class TournamentFeature : RealmObject(), Manageable, StaticRowRepresentableDataSource, RowRepresentable,
    CountableUsage {

    companion object {
        val rowRepresentation : List<RowRepresentable> by lazy {
            val rows = ArrayList<RowRepresentable>()
            rows.add(SimpleRow.NAME)
            rows.addAll(TournamentFeatureRow.values())
            rows
        }
    }

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

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

    // CountableUsage
    override var useCount: Int = 0

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

    override fun adapterRows(): List<RowRepresentable>? {
        return TournamentFeature.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 updateValue(value: Any?, row: RowRepresentable) {
        when (row) {
            SimpleRow.NAME -> this.name = value as String? ?: ""
        }
    }

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

}