package net.pokeranalytics.android.model.realm

import android.text.InputType
import io.realm.RealmObject
import io.realm.annotations.PrimaryKey
import net.pokeranalytics.android.model.ObjectSavable
import net.pokeranalytics.android.ui.adapter.components.*
import net.pokeranalytics.android.ui.fragment.components.bottomsheet.BottomSheetData
import net.pokeranalytics.android.ui.view.RowEditable
import net.pokeranalytics.android.ui.view.RowRepresentable
import net.pokeranalytics.android.ui.view.SimpleRow
import net.pokeranalytics.android.ui.view.TournamentFeatureRow
import java.util.*

open class TournamentFeature : RealmObject(), RowRepresentableDataSource, LiveDataDataSource, RowEditable, ObjectSavable {

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

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

    override val title: String get() = this.name
    override val primaryKey: String get() = this.id

    override fun adapterRows(): ArrayList<RowRepresentable> {
        val rows = ArrayList<RowRepresentable>()
        rows.add(SimpleRow.NAME)
        rows.addAll(TournamentFeatureRow.values())
        return rows
    }

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


    override fun getBottomSheetData(row: RowRepresentable): ArrayList<BottomSheetData> {
        val data = java.util.ArrayList<BottomSheetData>()
        when (row) {
            SimpleRow.NAME -> data.add(BottomSheetData(this.name, SimpleRow.NAME.resId, InputType.TYPE_CLASS_TEXT))
        }
        return data
    }

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

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