package net.pokeranalytics.android.calculus.bankroll

import io.realm.Realm
import net.pokeranalytics.android.model.realm.Session
import net.pokeranalytics.android.model.realm.Transaction

class BankrollCalculator {

	companion object {

		fun computeReport(setup: BankrollReportSetup) : BankrollReport {

			val realm = Realm.getDefaultInstance()

			val report = BankrollReport(setup)

			val sessionQuery = realm.where(Session::class.java)
					if (setup.bankroll != null) {
						sessionQuery.equalTo("bankroll.id", setup.bankroll.id)
					}
//			val sessions = sessionQuery.findAll()
			val transactionQuery = realm.where(Transaction::class.java)
			if (setup.bankroll != null) {
				transactionQuery.equalTo("bankroll.id", setup.bankroll.id).findAll()
			}
			val transactions = transactionQuery.findAll()

//			val sessionsNet = sessions.sum("result.net")
			val transactionsNet = transactions.sum("value")

			transactions.forEach {
				report.addTransaction(it)
			}

			realm.close()

			return report
		}

	}

}