package net.pokeranalytics.android.performanceTests

import android.content.Context
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import net.pokeranalytics.android.calculus.Calculator
import net.pokeranalytics.android.calculus.ComputableGroup
import net.pokeranalytics.android.calculus.ComputedResults
import net.pokeranalytics.android.calculus.Stat
import net.pokeranalytics.android.components.RealmInstrumentedUnitTest
import net.pokeranalytics.android.model.filter.Query
import net.pokeranalytics.android.model.realm.ComputableResult
import net.pokeranalytics.android.model.realm.Session
import net.pokeranalytics.android.model.realm.SessionSet
import net.pokeranalytics.android.model.utils.Seed
import net.pokeranalytics.android.util.FakeDataManager
import org.junit.Test
import org.junit.runner.RunWith
import timber.log.Timber

/**
 * Instrumented test, which will execute on an Android device.
 *
 * See [testing documentation](http://d.android.com/tools/testing).
 */
@RunWith(AndroidJUnit4::class)
class PerfsInstrumentedUnitTest : RealmInstrumentedUnitTest() {

	@Test
	fun testGlobalPerfs() {

		Timber.d("*** start global perfs ")


		val realm = mockRealm
		val app: Context = ApplicationProvider.getApplicationContext()

		realm.beginTransaction()
		Seed(app).execute(realm)
		realm.commitTransaction()


		FakeDataManager.createFakeSessions(5000) {success ->

			if (success) {

				val start = System.currentTimeMillis()

				val sessions = realm.where(Session::class.java).findAll()
				val computableResults = realm.where(ComputableResult::class.java).findAll()
				val sets = realm.where(SessionSet::class.java).findAll()

				Timber.d("sessions: ${sessions.size}")
				Timber.d("computableResults: ${computableResults.size}")
				Timber.d("sets: ${sets.size}")

				val stats: List<Stat> = listOf(Stat.NET_RESULT, Stat.AVERAGE)
				val group = ComputableGroup(Query(), stats)

				val options = Calculator.Options()
				options.stats = listOf(Stat.STANDARD_DEVIATION_BB_PER_100_HANDS, Stat.STANDARD_DEVIATION)

				val results: ComputedResults = Calculator.compute(realm, group, options)
				Timber.d("*** ended in ${System.currentTimeMillis() - start} milliseconds")

				val sum = results.computedStat(Stat.NET_RESULT)
				Timber.d("*** NET RESULT: ${sum?.value}")

				val average = results.computedStat(Stat.AVERAGE)
				Timber.d("*** AVERAGE: ${average?.value}")

				val duration = results.computedStat(Stat.HOURLY_DURATION)
				Timber.d("*** HOURLY_DURATION: ${duration?.value}")

			}
		}
	}

}