package net.pokeranalytics.android.model.realm

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


open class TransactionType : RealmObject(), NameManageable, StaticRowRepresentableDataSource, RowRepresentable {

	enum class Value(val additive: Boolean) {
		WITHDRAWAL(false),
		DEPOSIT(true),
		BONUS(true)
	}

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

		fun getByValue(value: Value, realm: Realm): TransactionType {
			val type = realm.where(TransactionType::class.java).equalTo("kind", value.ordinal).findFirst()
			type?.let {
				return it
			}
			throw IllegalStateException("Transaction type ${value.name} should exist in database!")
		}

	}

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

	// The name of the transaction type
	override var name: String = ""

	// Whether or not the amount is added, or subtracted to the bankroll total
	var additive: Boolean = false

	// Whether or not the type can be deleted by the user
	var lock: Boolean = false

	// The predefined kind, if necessary, like: Withdrawal, deposit, or tips
	var kind: Int? = null

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

	override fun adapterRows(): List<RowRepresentable>? {
		return rowRepresentation
	}

	override fun stringForRow(row: RowRepresentable): String {
		return when (row) {
			SimpleRow.NAME -> this.name
			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 isValidForDelete(realm: Realm): Boolean {
		TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
	}

	override fun getFailedDeleteMessage(): Int {
		TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
	}
}

