/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package mozilla.components.feature.recentlyclosed

import android.content.Context
import androidx.room.Room
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.runBlocking
import mozilla.components.feature.recentlyclosed.db.RecentlyClosedTabDao
import mozilla.components.feature.recentlyclosed.db.RecentlyClosedTabEntity
import mozilla.components.feature.recentlyclosed.db.RecentlyClosedTabsDatabase
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import java.util.UUID

@RunWith(AndroidJUnit4::class)
class RecentlyClosedTabDaoTest {
    private val context: Context
        get() = ApplicationProvider.getApplicationContext()

    private lateinit var database: RecentlyClosedTabsDatabase
    private lateinit var tabDao: RecentlyClosedTabDao

    @Before
    fun setUp() {
        database =
            Room.inMemoryDatabaseBuilder(context, RecentlyClosedTabsDatabase::class.java).build()
        tabDao = database.recentlyClosedTabDao()
    }

    @Test
    fun testAddingTabs() = runBlocking(Dispatchers.IO) {
        val tab1 = RecentlyClosedTabEntity(
            title = "RecentlyClosedTab One",
            url = "https://www.mozilla.org",
            uuid = UUID.randomUUID().toString(),
            createdAt = 200
        ).also {
            tabDao.insertTab(it)
        }

        val tab2 = RecentlyClosedTabEntity(
            title = "RecentlyClosedTab Two",
            url = "https://www.firefox.com",
            uuid = UUID.randomUUID().toString(),
            createdAt = 100
        ).also {
            tabDao.insertTab(it)
        }

        tabDao.getTabs().first().apply {
            assertEquals(2, this.size)
            assertEquals(tab1, this[0])
            assertEquals(tab2, this[1])
        }
        Unit
    }

    @Test
    fun testRemovingTab() = runBlocking(Dispatchers.IO) {
        val tab1 = RecentlyClosedTabEntity(
            title = "RecentlyClosedTab One",
            url = "https://www.mozilla.org",
            uuid = UUID.randomUUID().toString(),
            createdAt = 200
        ).also {
            tabDao.insertTab(it)
        }

        val tab2 = RecentlyClosedTabEntity(
            title = "RecentlyClosedTab Two",
            url = "https://www.firefox.com",
            uuid = UUID.randomUUID().toString(),
            createdAt = 100
        ).also {
            tabDao.insertTab(it)
        }

        tabDao.deleteTab(tab1)

        tabDao.getTabs().first().apply {
            assertEquals(1, this.size)
            assertEquals(tab2, this[0])
        }
        Unit
    }

    @Test
    fun testRemovingAllTabs() = runBlocking(Dispatchers.IO) {
        RecentlyClosedTabEntity(
            title = "RecentlyClosedTab One",
            url = "https://www.mozilla.org",
            uuid = UUID.randomUUID().toString(),
            createdAt = 200
        ).also {
            tabDao.insertTab(it)
        }

        RecentlyClosedTabEntity(
            title = "RecentlyClosedTab Two",
            url = "https://www.firefox.com",
            uuid = UUID.randomUUID().toString(),
            createdAt = 100
        ).also {
            tabDao.insertTab(it)
        }

        tabDao.removeAllTabs()

        tabDao.getTabs().first().apply {
            assertEquals(0, this.size)
        }
        Unit
    }

    @After
    fun tearDown() {
        database.close()
    }
}
