package net.pokeranalytics.android.model.realm

import io.realm.RealmObject
import io.realm.annotations.Ignore
import io.realm.annotations.PrimaryKey
import net.pokeranalytics.android.util.UserDefaults
import java.util.*

open class Currency : RealmObject() {

	@Ignore
	val DEFAULTRATE: Double = 1.0

    @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? = DEFAULTRATE

	fun refreshRelatedRatedValues() {

		val rate = this.rate ?: DEFAULTRATE
		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
			}

            computable.session?.bankrollHasBeenUpdated()

		}

	}

	fun hasMainCurrencyCode() : Boolean {
		this.code?.let { return it == UserDefaults.currency.currencyCode }
		return false
	}

}