package net.pokeranalytics.android.unitTests

import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import net.pokeranalytics.android.components.RealmInstrumentedUnitTest
import net.pokeranalytics.android.model.realm.Location
import net.pokeranalytics.android.model.realm.Session
import net.pokeranalytics.android.model.utils.FavoriteSessionFinder
import org.junit.Assert
import org.junit.Test
import org.junit.runner.RunWith
import java.util.*

@RunWith(AndroidJUnit4::class)

class FavoriteSessionUnitTest : RealmInstrumentedUnitTest() {


    @Test
    fun testFavoriteWithoutLocation() {

        val realm = this.mockRealm
        realm.beginTransaction()

        val s1 = newSessionInstance(realm)
        val s2 = newSessionInstance(realm)
        val s3 = newSessionInstance(realm)

        s1.endDate = Date()
        s2.endDate = Date()
        s3.endDate = Date()

        s1.cgBigBlind = 4.0
        s1.cgSmallBlind = 2.0
        s2.cgBigBlind = 4.0
        s2.cgSmallBlind = 2.0
        s3.cgBigBlind = 1.0
        s3.cgSmallBlind = 1.0

        realm.insert(s1)
        realm.insert(s2)
        realm.insert(s3)

        realm.commitTransaction()
        val favSession = FavoriteSessionFinder.favoriteSession(Session.Type.CASH_GAME.ordinal, null, realm, InstrumentationRegistry.getInstrumentation().targetContext)

        if (favSession != null) {
            Assert.assertEquals(4.0, favSession.cgBigBlind)
        } else {
            Assert.fail("session shouldn't be null")
        }

    }

    @Test
    fun testFavoriteWithLocation() {

        val realm = this.mockRealm
        realm.beginTransaction()

        val s1 = newSessionInstance(realm)
        val s2 = newSessionInstance(realm)
        val s3 = newSessionInstance(realm)

        val loc1 = realm.createObject(Location::class.java, "1")
        val loc2 = realm.createObject(Location::class.java, "2")

        s1.cgBigBlind = 4.0
        s2.cgBigBlind = 4.0
        s3.cgBigBlind = 1.0

        s1.location = loc1
        s2.location = loc1
        s3.location = loc2

        realm.insert(s1)
        realm.insert(s2)
        realm.insert(s3)

        realm.commitTransaction()
        val favSession = FavoriteSessionFinder.favoriteSession(Session.Type.CASH_GAME.ordinal, loc2, realm, InstrumentationRegistry.getInstrumentation().targetContext)

        if (favSession != null) {
            Assert.assertEquals(1.0, favSession.cgBigBlind)
        } else {
            Assert.fail("session shouldn't be null")
        }

    }
}