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.TransactionTypeRow
import java.util.*


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

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

    // The name of the transaction type
    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 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(TransactionTypeRow.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()
    }
}

enum class TransactionKind {
    WITHDRAWAL,
    DEPOSIT
}
