package net.pokeranalytics.android.unitTests.filter

import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import io.realm.RealmList
import io.realm.RealmResults
import net.pokeranalytics.android.R
import net.pokeranalytics.android.components.BaseFilterInstrumentedUnitTest
import net.pokeranalytics.android.model.filter.Query
import net.pokeranalytics.android.model.filter.QueryCondition
import net.pokeranalytics.android.model.realm.*
import net.pokeranalytics.android.ui.view.rowrepresentable.FilterSectionRow
import org.junit.Assert
import org.junit.Test
import org.junit.runner.RunWith
import java.util.*

@RunWith(AndroidJUnit4::class)
class TransactionFilterInstrumentedUnitTest : BaseFilterInstrumentedUnitTest() {

    @Test
    fun testTransactionTypeFilter() {
        val context = InstrumentationRegistry.getInstrumentation().context

        val realm = this.mockRealm
        realm.beginTransaction()
        TransactionType.Value.values().forEachIndexed { index, value ->
            val type = TransactionType()
            val name = "test"
            type.name = name
            type.additive = value.additive
            type.kind = index
            type.lock = true
            realm.insertOrUpdate(type)
        }

        val t1: Transaction = realm.createObject(Transaction::class.java, "1")
        t1.type = TransactionType.getByValue(TransactionType.Value.DEPOSIT, realm)
        val t2: Transaction = realm.createObject(Transaction::class.java, "2")
        t2.type = TransactionType.getByValue(TransactionType.Value.WITHDRAWAL, realm)

        val b1 = realm.createObject(Bankroll::class.java, "1")
        t1.bankroll = b1

        val b2 = realm.createObject(Bankroll::class.java, "2")
        t2.bankroll = b2

        realm.commitTransaction()

        val transactions = Filter.queryOn<Transaction>(realm, Query(QueryCondition.AnyTransactionType(t1.type!!)))

        Assert.assertEquals(1, transactions.size)
        transactions[0]?.run {
            Assert.assertEquals(t1.type!!.id, (this).type!!.id)
        }
    }
}