package net.pokeranalytics.android.filter

import net.pokeranalytics.android.model.filter.FilterManager
import net.pokeranalytics.android.model.filter.SessionFilterable
import net.pokeranalytics.android.model.realm.Bankroll
import net.pokeranalytics.android.model.realm.Session
import org.junit.Assert
import org.junit.Test
import java.util.*

class BlindFilterInstrumentedTest : BaseFilterInstrumentedUnitTest() {
    @Test
    fun testSingleBlindNoCurrencyFilter() {

        val realm = this.mockRealm
        realm.beginTransaction()

        val currency = realm.createObject(net.pokeranalytics.android.model.realm.Currency::class.java, "1")
        currency.code = "AUD"

        val b1 = realm.createObject(Bankroll::class.java, "1")
        val b2 = realm.createObject(Bankroll::class.java, "2")
        b2.currency = currency

        val s1 = Session.testInstance(100.0, false, Date(), 1, b1)
        s1.cgBigBlind = 1.0
        s1.cgSmallBlind = 0.5

        val s2 = Session.testInstance(100.0, false, Date(), 1, b1)
        s2.cgBigBlind = 1.0
        s2.cgSmallBlind = 0.5

        val s3 = Session.testInstance(100.0, false, Date(), 1, b1)
        s3.cgBigBlind = 2.0
        s3.cgSmallBlind = 1.0

        realm.commitTransaction()


        val filter = SessionFilterable.BLINDS
        filter.valueMap = mapOf("map" to arrayOf(mapOf(
            "sb" to 0.5,
            "bb" to 1.0,
            "code" to null
        )))

        val sessions = FilterManager().filter(
            realm,
            Session::class.java,
            arrayListOf(filter)
        )

        Assert.assertEquals(2, sessions.size)
        sessions.map {
            Assert.assertTrue(arrayListOf(s1.id, s2.id).contains((it as Session).id))
        }
    }

    @Test
    fun testSingleBlindNoSmallBlindNoCurrencyFilter() {

        val realm = this.mockRealm
        realm.beginTransaction()

        val currency = realm.createObject(net.pokeranalytics.android.model.realm.Currency::class.java, "1")
        currency.code = "AUD"

        val b1 = realm.createObject(Bankroll::class.java, "1")
        val b2 = realm.createObject(Bankroll::class.java, "2")
        b2.currency = currency

        val s1 = Session.testInstance(100.0, false, Date(), 1, b1)
        s1.cgBigBlind = 1.0
        s1.cgSmallBlind = 0.5

        val s2 = Session.testInstance(100.0, false, Date(), 1, b1)
        s2.cgBigBlind = 1.0
        s2.cgSmallBlind = 0.5

        val s3 = Session.testInstance(100.0, false, Date(), 1, b1)
        s3.cgBigBlind = 2.0
        s3.cgSmallBlind = 1.0

        realm.commitTransaction()


        val filter = SessionFilterable.BLINDS
        filter.valueMap = mapOf("map" to arrayOf(mapOf(
            "sb" to null,
            "bb" to 1.0,
            "code" to null
        )))

        val sessions = FilterManager().filter(
            realm,
            Session::class.java,
            arrayListOf(filter)
        )

        Assert.assertEquals(2, sessions.size)
        sessions.map {
            Assert.assertTrue(arrayListOf(s1.id, s2.id).contains((it as Session).id))
        }
    }

    @Test
    fun testSingleBlindCurrencyFilter() {

        val realm = this.mockRealm
        realm.beginTransaction()

        val currency = realm.createObject(net.pokeranalytics.android.model.realm.Currency::class.java, "1")
        currency.code = "AUD"

        val b1 = realm.createObject(Bankroll::class.java, "1")
        val b2 = realm.createObject(Bankroll::class.java, "2")
        b2.currency = currency

        val s1 = Session.testInstance(100.0, false, Date(), 1, b1)
        s1.cgBigBlind = 1.0
        s1.cgSmallBlind = 0.5

        val s2 = Session.testInstance(100.0, false, Date(), 1, b1)
        s2.cgBigBlind = 1.0
        s2.cgSmallBlind = 0.5

        val s3 = Session.testInstance(100.0, false, Date(), 1, b2)
        s3.cgBigBlind = 2.0
        s3.cgSmallBlind = 1.0

        realm.commitTransaction()


        val filter = SessionFilterable.BLINDS
        filter.valueMap = mapOf("map" to arrayOf(mapOf(
            "sb" to 1.0,
            "bb" to 2.0,
            "code" to "AUD"
        )))

        val sessions = FilterManager().filter(
            realm,
            Session::class.java,
            arrayListOf(filter)
        )

        Assert.assertEquals(1, sessions.size)
        sessions.map {
            Assert.assertEquals(s3.id, (it as Session).id)
        }
    }

    @Test
    fun testMultiBlindNoCurrencyFilter() {

        val realm = this.mockRealm
        realm.beginTransaction()

        val currency = realm.createObject(net.pokeranalytics.android.model.realm.Currency::class.java, "1")
        currency.code = "AUD"

        val b1 = realm.createObject(Bankroll::class.java, "1")
        val b2 = realm.createObject(Bankroll::class.java, "2")
        b2.currency = currency

        val s1 = Session.testInstance(100.0, false, Date(), 1, b1)
        s1.cgBigBlind = 1.0
        s1.cgSmallBlind = 0.5

        val s2 = Session.testInstance(100.0, false, Date(), 1, b1)
        s2.cgBigBlind = 2.0
        s2.cgSmallBlind = 1.0

        val s3 = Session.testInstance(100.0, false, Date(), 1, b2)
        s3.cgBigBlind = 2.0
        s3.cgSmallBlind = 1.0

        realm.commitTransaction()


        val filter = SessionFilterable.BLINDS
        filter.valueMap = mapOf("map" to arrayOf(
            mapOf(
                "sb" to 1.0,
                "bb" to 2.0,
                "code" to null
            ),
            mapOf(
                "sb" to 0.5,
                "bb" to 1.0,
                "code" to null
            )))

        val sessions = FilterManager().filter(
            realm,
            Session::class.java,
            arrayListOf(filter)
        )

        Assert.assertEquals(2, sessions.size)
        sessions.map {
            Assert.assertTrue(arrayListOf(s1.id, s2.id).contains((it as Session).id))
        }
    }
}