package net.pokeranalytics.android.model

import android.content.Context
import net.pokeranalytics.android.components.BaseFilterInstrumentedUnitTest
import net.pokeranalytics.android.model.filter.QueryCondition
import net.pokeranalytics.android.model.realm.Session
import org.junit.Assert.assertEquals
import org.junit.Test
import java.util.*
import androidx.test.platform.app.InstrumentationRegistry

class CriteriaTest : BaseFilterInstrumentedUnitTest() {
    @Test
    fun getQueryConditions() {

        val realm = this.mockRealm
        realm.beginTransaction()
        val cal = Calendar.getInstance()
        cal.time = Date()
        val s1 = Session.testInstance(100.0, false, cal.time)
        val firstValue = cal.get(Calendar.YEAR)
        cal.add(Calendar.YEAR, 1)
        Session.testInstance(100.0, true, cal.time)
        cal.add(Calendar.YEAR, -11)
        Session.testInstance(100.0, true, cal.time)
        cal.add(Calendar.YEAR, 7)
        Session.testInstance(100.0, true, cal.time)
        cal.add(Calendar.YEAR, -2)
        Session.testInstance(100.0, true, cal.time)
        cal.add(Calendar.YEAR, 10)
        Session.testInstance(100.0, true, cal.time)

        val lastValue = firstValue + 5

        realm.commitTransaction()

        val yearQueries = Criteria.Years.queryConditions

        assertEquals(16, yearQueries.size)

        val firstYearCondition = yearQueries.first().conditions.first() as QueryCondition.AnyYear
        assertEquals(firstValue - 10, firstYearCondition.listOfValues.first())

        val lastYearCondition = yearQueries.last().conditions.first() as QueryCondition.AnyYear
        assertEquals(lastValue, lastYearCondition.listOfValues.first())


//        val years = Criteria.Years.queryConditions as List<QueryCondition.AnyYear>
//        println("years = ${years.map { it.getDisplayName() }}")
//
//        assertEquals(16, years.size)
//        assertEquals(firstValue-10, years.first().listOfValues.first())
//        assertEquals(lastValue, years.last().listOfValues.first())
    }

    @Test
    fun combined() {

        val criterias = listOf(Criteria.MonthsOfYear, Criteria.DaysOfWeek)
        val combined = criterias.combined()
        val context = InstrumentationRegistry.getInstrumentation().context

        combined.forEach {
            it.conditions.forEach {qc->

                println(qc.getDisplayName(context))
            }
        }
    }

    @Test
    fun upToNow() {
        val realm = this.mockRealm
        realm.beginTransaction()
        val cal = Calendar.getInstance()
        cal.time = Date()
        val s1 = Session.testInstance(100.0, false, cal.time)
        cal.add(Calendar.YEAR, 1)
        Session.testInstance(100.0, true, cal.time)
        cal.add(Calendar.YEAR, -11)
        val firstValue = cal.get(Calendar.YEAR)
        Session.testInstance(100.0, true, cal.time)
        cal.add(Calendar.YEAR, 7)
        Session.testInstance(100.0, true, cal.time)
        cal.add(Calendar.YEAR, -2)
        Session.testInstance(100.0, true, cal.time)
        cal.add(Calendar.YEAR, 10)
        Session.testInstance(100.0, true, cal.time)

        val lastValue = firstValue + 10

        realm.commitTransaction()
        val context = InstrumentationRegistry.getInstrumentation().context

        val allMonths = Criteria.AllMonthsUpToNow.queries
        allMonths.forEach {
            it.conditions.forEach { qc->
                println("<<<<< ${qc.getDisplayName(context)}")
            }
        }
    }

}