package net.pokeranalytics.android.model.realm

import io.realm.Realm
import io.realm.RealmList
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.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.RowViewType
import net.pokeranalytics.android.ui.view.rowrepresentable.BankrollRow
import net.pokeranalytics.android.ui.view.rowrepresentable.CustomizableRowRepresentable
import net.pokeranalytics.android.ui.view.rowrepresentable.SimpleRow
import net.pokeranalytics.android.util.NULL_TEXT
import java.util.*
import kotlin.collections.ArrayList

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

    companion object {
        val rowRepresentation : List<RowRepresentable> by lazy {
            val rows = ArrayList<RowRepresentable>()
            rows.add(SimpleRow.NAME)
            rows.add(BankrollRow.LIVE)
            rows.add(CustomizableRowRepresentable(customViewType = RowViewType.HEADER_TITLE, resId = R.string.currency))
	        rows.add(BankrollRow.CURRENCY)
            rows
        }
    }

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

    override var name: String = ""

    // Indicates whether the bankroll is live or online
    var live: Boolean = true

    // The list of transactions of the bankroll
    var transactions: RealmList<Transaction> = RealmList()

    // The currency of the bankroll
    var currency: Currency? = null

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

    // Row Representable Datasource
    override fun adapterRows(): List<RowRepresentable>? {
        return Bankroll.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 boolForRow(row: RowRepresentable): Boolean {
        return when (row) {
            BankrollRow.LIVE -> !this.live
            else -> super.boolForRow(row)
        }
    }

    override fun editDescriptors(row: RowRepresentable): ArrayList<RowRepresentableEditDescriptor>? {
        return when (row) {
            SimpleRow.NAME -> row.editingDescriptors(mapOf("defaultValue" to this.name))
            BankrollRow.RATE -> row.editingDescriptors(mapOf())
            else -> {
                row.editingDescriptors(mapOf())
            }
        }
    }

    override fun updateValue(value: Any?, row: RowRepresentable) {
        when (row) {
            SimpleRow.NAME -> this.name = value as String? ?: ""
            BankrollRow.LIVE -> {
                this.live = if (value is Boolean) !value else false
            }
	        BankrollRow.CURRENCY -> {
                //TODO handle a use default currency option
		        this.currency?.code = value as String?
	        }
	        BankrollRow.RATE -> {
                this.currency?.rate = value as Double?
	        }
        }
    }

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

    override fun getFailedDeleteMessage(): Int {
        return R.string.bankroll_relationship_error
    }

    override fun getFailedSaveMessage(status: SaveValidityStatus): Int {
        return when (status) {
            SaveValidityStatus.DATA_INVALID -> R.string.empty_name_for_br_error
            SaveValidityStatus.ALREADY_EXISTS -> R.string.duplicate_bankroll_name_error
            else -> super.getFailedSaveMessage(status)
        }
    }
}