package net.pokeranalytics.android.components

import io.realm.Realm
import io.realm.RealmConfiguration
import net.pokeranalytics.android.model.realm.Result
import net.pokeranalytics.android.model.realm.Session
import org.junit.After
import org.junit.Before
import java.util.*


open class RealmInstrumentedUnitTest {

    val EPSILON = 0.0001

    lateinit var mockRealm: Realm

    companion object {

        fun newSessionInstance(realm: Realm, isCashGame: Boolean = true) : Session {
            val session = realm.createObject(Session::class.java, UUID.randomUUID().toString())
	        session.startDate = Date()
            session.type = if (isCashGame) Session.Type.CASH_GAME.ordinal else Session.Type.TOURNAMENT.ordinal
            session.result = realm.createObject(Result::class.java)
            return session
        }

    }

    @Before
    fun setup() {

        val testConfig = RealmConfiguration.Builder().inMemory().name("test-realm").build()
        Realm.setDefaultConfiguration(testConfig)
        this.mockRealm = Realm.getDefaultInstance()
        this.mockRealm.beginTransaction()
        this.mockRealm.deleteAll()
        this.mockRealm.commitTransaction()
    }

    @After
    @Throws(Exception::class)
    fun tearDown() {
        this.mockRealm.close()
    }

}

/*

package net.pokeranalytics.android.components

import io.realm.Realm
import io.realm.RealmConfiguration
import net.pokeranalytics.android.model.realm.ComputableResult
import net.pokeranalytics.android.model.realm.Result
import net.pokeranalytics.android.model.realm.Session
import org.junit.AfterClass
import org.junit.Before
import java.util.*

interface RealmTestDataSource {
    val realmName : String
}

open class RealmInstrumentedUnitTest {

    companion object : RealmTestDataSource {
        lateinit var mockRealm: Realm
        override val realmName: String
            get() = "RealmInstrumentedUnitTest"

        init {
            val testConfig = RealmConfiguration.Builder().inMemory().name(realmName).build()
            Realm.setDefaultConfiguration(testConfig)
            mockRealm = Realm.getDefaultInstance()
        }

        fun newSessionInstance(realm: Realm) : Session {
            val session = realm.createObject(Session::class.java, UUID.randomUUID().toString())
            val computableResult = realm.createObject(ComputableResult::class.java)
            computableResult.session = session
            session.result = realm.createObject(Result::class.java)
            return session
        }

        @AfterClass
        @Throws(Exception::class)
        fun tearDown() {
            mockRealm.close()
        }

    }

    var mockRealm: Realm = Companion.mockRealm

    @Before
    fun setup() {
        this.mockRealm.beginTransaction()
        this.mockRealm.deleteAll()
        this.mockRealm.commitTransaction()
    }

}
 */