package net.pokeranalytics.android.model.realm

import io.realm.RealmObject
import io.realm.annotations.PrimaryKey
import java.util.*


open class Currency : RealmObject() {

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

	/**
	 * The currency code of the currency, i.e. USD, EUR...
 	 */
    var code: String? = null

	/**
	 * The rate of the currency with the main currency
 	 */
    var rate: Double? = null
        set(value) {
            field = value

            val rate = value ?: 1.0

            // could be async

            val query = this.realm.where(ComputableResult::class.java)
	        query.`in`("session.bankroll.currency.id", arrayOf(this.id))
	        val cResults = query.findAll()

            cResults.forEach { computable ->

                computable.session?.result?.net?.let {
                    computable.ratedNet = it * rate
                }
                computable.session?.result?.buyin?.let {
                    computable.ratedBuyin = it * rate
                }

            }
        }

}