package net.pokeranalytics.android.model.realm

import android.content.Context
import io.realm.Realm
import io.realm.RealmObject
import io.realm.annotations.PrimaryKey
import io.realm.kotlin.where
import net.pokeranalytics.android.R
import net.pokeranalytics.android.model.interfaces.CountableUsage
import net.pokeranalytics.android.model.interfaces.NameManageable
import net.pokeranalytics.android.model.interfaces.SaveValidityStatus
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(), NameManageable, 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
    override var name: String = ""

    // CountableUsage
    override var useCount: Int = 0

    override fun getDisplayName(context: Context): 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 getFailedSaveMessage(status: SaveValidityStatus): Int {
        return when (status) {
            SaveValidityStatus.DATA_INVALID -> R.string.tournament_feature_empty_field_error
            SaveValidityStatus.ALREADY_EXISTS -> R.string.duplicate_tournament_feature_error
            else -> super.getFailedSaveMessage(status)
        }
    }

    override fun isValidForDelete(realm: Realm): Boolean {
        return realm.where<Session>().equalTo("tournamentFeatures.id", id).findAll().isEmpty()
    }
}