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.LiveDataDataSource
import net.pokeranalytics.android.ui.adapter.components.RowRepresentableDataSource
import net.pokeranalytics.android.ui.fragment.components.bottomsheet.BottomSheetData
import net.pokeranalytics.android.ui.view.GameRow
import net.pokeranalytics.android.ui.view.RowEditable
import net.pokeranalytics.android.ui.view.RowRepresentable
import net.pokeranalytics.android.ui.view.SimpleRow
import java.util.*

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

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

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

    // A shorter name for the game
    var shortName: String? = null

    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(GameRow.values())
        return rows
    }

    override fun stringForRow(row: RowRepresentable): String {
        return when (row) {
            SimpleRow.NAME -> this.name
            GameRow.SHORT_NAME -> this.shortName?:""
            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))
            GameRow.SHORT_NAME -> data.add(BottomSheetData(this.shortName, GameRow.SHORT_NAME.resId, InputType.TYPE_CLASS_TEXT))
        }
        return data
    }

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

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